在recyclerview的末尾添加进度条 [英] Adding progressbar at the end of recyclerview

查看:91
本文介绍了在recyclerview的末尾添加进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我发送了一个齐射请求,该请求一次抓取列表项,而不是一次抓取所有列表项.我想在获取数据时在recyclerview的末尾实现一个进度条.类'updateAdapter'更新适配器,我正在考虑使进度条在recyclerview滚动侦听器中可见.但是我不知道如何在我的代码中做到这一点.

In my app, I send a volley request which fetches list items one-by-one, not all at once. I want to implement a progressbar at the end of the recyclerview when the data is being fetched. The class 'updateAdapter' updates the adapter and I was thinking of making the progress bar visible in the recyclerview scroll listener. But I have no idea how to do this in my code.

updateAdapter.java

  //THIS CLASS UPDATES THE RECYCLER VIEW

    public class updateAdapter{

    private FetchWpApi fetch;

    private int totalItemCount;
    private int prevLastvisible=0;
    private int fpage=1;
    private Context context;
    private RecyclerView recyclerView;
    private ArrayList<sItem> list= new ArrayList<>();
    private LinearLayoutManager manager;
    private ProgressBar progressBar;

    //CONSTRUCTOR FOR updateAdapter
    public updateAdapter(RecyclerView recyclerView, final Context context, String url, LinearLayoutManager manager){
        this.context=context;
        this.recyclerView=recyclerView;
        fetch=new FetchWpApi(url,context);
        this.manager=manager;
    }

    public void fetchAndPut()
    {
        if(recyclerView.getAdapter()==null) {
            fetch.fetchApiData(fpage, new FetchWpApi.Callback() {
                @Override
                public void onSuccess(sItem sitem) {
                    list.add(sitem);
                    if (list.size() == 1 || recyclerView.getAdapter() == null) {
                        ItemAdapter adapter = new ItemAdapter(context, list);
                        recyclerView.setAdapter(adapter);

                    } else if (list.size() > 1 && recyclerView.getAdapter() != null) {
                        recyclerView.getAdapter().notifyDataSetChanged();
                        recyclerView.getAdapter().notifyItemRangeChanged(0, recyclerView.getAdapter().getItemCount());
                    }
                }
                @Override
                public void onFail(String msg) {
                    Toast.makeText(context, "FAILED PRIMARY LOAD", Toast.LENGTH_LONG).show();
                }
            });
        }

        recyclerView.addOnItemTouchListener(
                        //Not important
        );

        //SCROLL LISTENER ATTACHED TO THE RECYCLER VIEW
        //SHOW PROGRESS BAR HERE?
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener()
        {
            @Override
            public void onScrolled(final RecyclerView recyclerView, int dx, int dy)
            {
                if(dy > 0) //check for scroll down
                {
                   totalItemCount = manager.getItemCount();
                    int lastVisibleItemPosition = ((LinearLayoutManager) manager).findLastVisibleItemPosition();
                        if( (lastVisibleItemPosition+1)==totalItemCount && totalItemCount%10==0 && lastVisibleItemPosition>prevLastvisible)

                        {

                            //SET VISIBILITY OF THE PROGRESS BAR IN THE LAST CARD ITEM TO VISIBLE ??

                            fpage++;
                            //loading = false;
                            Log.v("...", "Last Item !");
                            //Do pagination.. i.e. fetch new data
                            prevLastvisible=lastVisibleItemPosition;
                            fetch.fetchApiData(fpage,new FetchWpApi.Callback(){
                                @Override
                                public void onSuccess(sItem sitem){
                                    list.add(sitem);
                                    recyclerView.getAdapter().notifyDataSetChanged();
                                    recyclerView.getAdapter().notifyItemRangeChanged(0, recyclerView.getAdapter().getItemCount());

                                }
                                @Override
                                public void onFail(String msg){
                                    Toast.makeText(context,"FAILED ONLOAD",Toast.LENGTH_LONG).show();
                                }
                            });
                        }

                }
            }
        });

    }

}

我要显示的进度条在此卡片视图中:

The progressbar which I want to display is in this cardview:

finalcard.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/cvItem"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"

>

<!--<LinearLayout-->
    <!--android:id="@+id/linearlayout"-->
    <!--android:layout_width="match_parent"-->
    <!--android:layout_height="wrap_content"-->
    <!--android:layout_margin="10dp"-->
    <!--&gt;-->


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView
        android:id="@+id/tvTitle"
        android:textColor="#000"
        android:text="Sample text Sampletext sample text sample text sample text"
        android:layout_alignParentStart="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toStartOf="@+id/ivMainImage"
        android:layout_marginTop="15dp"
        android:paddingStart="16dp"
        android:paddingEnd="2dp"
        android:maxLines="4"
        android:textSize="20sp"
        android:layout_gravity="start"
        />


    <TextView
        android:id="@+id/dateText"
        android:text="DATE"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingStart="16dp"
        android:paddingEnd="0dp"
        android:layout_gravity="start"
        android:layout_below="@id/tvTitle"
        android:ellipsize="marquee"/>

    <ImageView
        android:id="@+id/ivMainImage"
        android:layout_width="140dp"
        android:layout_height="130dp"
        android:layout_gravity="end"
        android:layout_alignParentEnd="true"
        android:layout_margin="15dp"
        android:src="@android:drawable/btn_star"
        android:scaleType="centerCrop"
        />

    <View
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#0C000000"
        android:layout_below="@id/ivMainImage"
        />

    <ProgressBar
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar2"
        android:layout_below="@id/view"
        android:visibility="gone"
        />

</RelativeLayout>

推荐答案

将进度条固定在回收者"视图而不是卡片视图下方. 致电网络服务时设置可见的进度栏,并在完成服务呼叫后设置可见性消失

Fix your progress bar below your recycler view instead of card view. Set visible progress bar when call web service, and set visibility Gone after complete service call

//Progress bar setVisibility - Visible
progressbar.setVisibility(View.VISIBLE);
fetch.fetchApiData(fpage, new FetchWpApi.Callback() {
  @Override
  public void onSuccess(sItem sitem) {
    progressbar.setVisibility(View.GONE);
    list.add(sitem);
    recyclerView.getAdapter().notifyDataSetChanged();
    recyclerView.getAdapter().notifyItemRangeChanged(0, recyclerView.getAdapter().getItemCount());

  }
  @Override
  public void onFail(String msg) {
    progressbar.setVisibility(View.GONE);
    Toast.makeText(context, "FAILED ONLOAD", Toast.LENGTH_LONG).show();
  }
});

这篇关于在recyclerview的末尾添加进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆