Put an Indeterminate Progressbar as Footer in a Recyclerview Grid

Put an indeterminate progressbar as footer in a RecyclerView grid

It is very simple to do that.

The solution is to use the same approach of the LinearLayoutManager with a GridLayoutManager and then use the method setSpanSizeLookup on the LayoutManager like this:

mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
switch(myAdapter.getItemViewType(position)){
case MyAdapter.VIEW_TYPES.Product:
return 1;
case MyAdapter.VIEW_TYPES.Progress:
return 2; //number of columns of the grid
default:
return -1;
}
}
});

This will automatically make the item cover a full row of the grid (if the row is not totally empty this item goes to the next row).

How to add a Footer in my RecyclerView

Change from this

@Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}

to this:

@Override
public int getItemViewType(int position) {
if (position < mDataSet.size() - 1) {
// content
return VIEWS_TYPES.Normal;
} else {
// footer
return VIEWS_TYPES.Footer;
}
}

and since you have mDataSet.size() + 1 (your footer row)

@Override
public int getItemCount() {
return mDataSet.size() + 1;
}

Update the onBindViewHolder

 @Override
public void onBindViewHolder(MainAdapter.ViewHolder holder, int position) {
if(mDataSet.size()<position)
holder.mTitle.setText(mDataSet.get(position));
else
// it's code for footer

}

You have to take two different ViewHolder for Content and Footer.

How to add footer ProgressBar after the last item of GridView

Here the logic i did to add load more item in GridView
1. Create a fake item at the last of Adapter's input data

public class MediaGridAdapter extends BaseAdapter {

private ArrayList<Media> list;
private final Media special = new Media("-1", "", "", "", "");

public MediaGridAdapter(Context context, int imageID, ArrayList<Media> array, int type) {

list = array;

if(list != null) {
list.add(special);
}
}

public void appendDataList(ArrayList<Media> appendedList, boolean isEnd) { //called in postExecute to append new data

//remove special element in original list
list.remove(list.size() - 1);
//append collection of media to list
list.addAll(appendedList);
//check to add special element
if(!isEnd) {

list.add(special);
}
}
}

2. In getView method : Check if it's the last position (is our fake item) return special layout( progress bar...) for this.

    if(position == (list.size() - 1)) {

Context context = parent.getContext();
item = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.item_special_more, null);
item.setTag(MORE_BUTTON);
return item;
}

The last in onItemClick check tag to start getMoreAsyncTask

if (v.getTag().equals(MediaGridAdapter.MORE_BUTTON)) {    

GetMoreItems task = new GetMoreItems();
task.execute(url);
return;
}

Endless RecyclerView with ProgressBar for pagination

HERE IS SIMPLER AND CLEANER APPROACH.

Implement Endless Scrolling from this Codepath Guide and then follow the following steps.

1. Add progress bar under the RecyclerView.

    <android.support.v7.widget.RecyclerView
android:id="@+id/rv_movie_grid"
android:layout_width="0dp"
android:layout_height="0dp"
android:paddingBottom="50dp"
android:clipToPadding="false"
android:background="@android:color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

</android.support.v7.widget.RecyclerView>

<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
android:background="@android:color/transparent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

Here android:paddingBottom="50dp" and android:clipToPadding="false" are very important.

2. Get a reference to the progress bar.

progressBar = findViewById(R.id.progressBar);

3. Define methods to show and hide progress bar.

void showProgressView() {
progressBar.setVisibility(View.VISIBLE);
}

void hideProgressView() {
progressBar.setVisibility(View.INVISIBLE);
}

Adding items to Endless Scroll RecyclerView with ProgressBar at bottom

The problem is that when you add new item internal EndlessRecyclerOnScrollListener doesn't know about it and counters breaking.
As a matter of fact answer with EndlessRecyclerOnScrollListener has some limitations and possible problems, e.g. if you load 1 item at a time it will not work. So here is an enhanced version.

  1. Get rid of EndlessRecyclerOnScrollListener we don't need it anymore
  2. Change your adapter to this which contains scroll listener

    public class MyAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final int VIEW_ITEM = 1;
    private final int VIEW_PROG = 0;

    private List<T> mDataset;

    // The minimum amount of items to have below your current scroll position before loading more.
    private int visibleThreshold = 2;
    private int lastVisibleItem, totalItemCount;
    private boolean loading;
    private OnLoadMoreListener onLoadMoreListener;

    public MyAdapter(List<T> myDataSet, RecyclerView recyclerView) {
    mDataset = myDataSet;

    if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {

    final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
    recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);

    totalItemCount = linearLayoutManager.getItemCount();
    lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
    if (!loading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
    // End has been reached
    // Do something
    if (onLoadMoreListener != null) {
    onLoadMoreListener.onLoadMore();
    }
    loading = true;
    }
    }
    });
    }
    }

    @Override
    public int getItemViewType(int position) {
    return mDataset.get(position) != null ? VIEW_ITEM : VIEW_PROG;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    RecyclerView.ViewHolder vh;
    if (viewType == VIEW_ITEM) {
    View v = LayoutInflater.from(parent.getContext())
    .inflate(android.R.layout.simple_list_item_1, parent, false);

    vh = new TextViewHolder(v);
    } else {
    View v = LayoutInflater.from(parent.getContext())
    .inflate(R.layout.progress_item, parent, false);

    vh = new ProgressViewHolder(v);
    }
    return vh;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    if (holder instanceof TextViewHolder) {
    ((TextViewHolder) holder).mTextView.setText(mDataset.get(position).toString());
    } else {
    ((ProgressViewHolder) holder).progressBar.setIndeterminate(true);
    }
    }

    public void setLoaded() {
    loading = false;
    }

    @Override
    public int getItemCount() {
    return mDataset.size();
    }

    public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
    this.onLoadMoreListener = onLoadMoreListener;
    }

    public interface OnLoadMoreListener {
    void onLoadMore();
    }

    public static class TextViewHolder extends RecyclerView.ViewHolder {
    public TextView mTextView;

    public TextViewHolder(View v) {
    super(v);
    mTextView = (TextView) v.findViewById(android.R.id.text1);
    }
    }

    public static class ProgressViewHolder extends RecyclerView.ViewHolder {
    public ProgressBar progressBar;

    public ProgressViewHolder(View v) {
    super(v);
    progressBar = (ProgressBar) v.findViewById(R.id.progressBar);
    }
    }
    }
  3. Change code in Activity class

    mAdapter = new MyAdapter<String>(myDataset, mRecyclerView);
    mRecyclerView.setAdapter(mAdapter);

    mAdapter.setOnLoadMoreListener(new MyAdapter.OnLoadMoreListener() {
    @Override
    public void onLoadMore() {
    //add progress item
    myDataset.add(null);
    mAdapter.notifyItemInserted(myDataset.size() - 1);

    handler.postDelayed(new Runnable() {
    @Override
    public void run() {
    //remove progress item
    myDataset.remove(myDataset.size() - 1);
    mAdapter.notifyItemRemoved(myDataset.size());
    //add items one by one
    for (int i = 0; i < 15; i++) {
    myDataset.add("Item" + (myDataset.size() + 1));
    mAdapter.notifyItemInserted(myDataset.size());
    }
    mAdapter.setLoaded();
    //or you can add all at once but do not forget to call mAdapter.notifyDataSetChanged();
    }
    }, 2000);
    System.out.println("load");
    }
    });

The rest remains unchanged, let me know if this works for you.

ProgressBar is not visible after RecyclerView

If you change to your root layout to RelativeLayout then it's fairly simple:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25dp"
android:minHeight="25dp">

<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/latestnews_swipe_refresh_layout"
android:layout_above="@id/loadmore_progressBar"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView_latestnews"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>

<ProgressBar
android:id="@+id/loadmore_progressBar"
android:layout_width="45dp"
android:layout_height="45dp"
android:indeterminate="true"
android:visibility="visible"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"/>
</RelativeLayout>

In my opinion though, I'd put the ProgressBar in the RecyclerView though the adapter, it looks much nicer but the chocie is yours.



Related Topics



Leave a reply



Submit