在recyclerview结束时添加进度条

时间:2017-03-01 04:57:59

标签: java android android-recyclerview

在我的应用中,我发送了一个一个一个一个地取出列表项的排球请求,而不是一次全部。我想在获取数据时在recyclerview结束时实现进度条。类'updateAdapter'更新适配器,我正在考虑在recyclerview滚动侦听器中显示进度条。但我不知道如何在我的代码中执行此操作。

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();
                                }
                            });
                        }

                }
            }
        });

    }

}

我要显示的进度条位于此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>

3 个答案:

答案 0 :(得分:5)

将进度条修复到回收器视图下方,而不是卡片视图。 调用Web服务时设置可见进度条,并在完成服务调用后设置可见性

//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();
  }
});

答案 1 :(得分:1)

有两种方式:

1。将进度条放在屏幕底部,当您位于RecyclerView旁边时int last = layoutManager.findLastVisibleItemPosition();它只显示,当您收到回复时隐藏它;

2。在您的适配器中:

  • 在列表的最后位置添加一个虚拟行。
  • 当位置为最后时检入适配器,然后显示进度条:

    if(position == list.size - 1) {
        // create a layout which only have progress bar
        View row = LayoutInflater.from(viewGroup.getContext()).inflate(
                       R.layout.progressbar, viewGroup, false);
    } else {
        View row = LayoutInflater.from(viewGroup.getContext()).inflate(
                       R.layout.layout_list, viewGroup, false);
    }
    

答案 2 :(得分:1)

我认为您可以按照此link进行正面添加页眉或页脚Recyclerview。并添加页脚作为进度条以加载更多数据,并在完成加载数据时删除页脚。