0

I have implemented a recyclerView . How do I add footer progressBar at the bottom of my recyclerView with my existing code .I know that the same can be achieved in a listview using addFooterView(View v) method.How can I achieve the same in this existing code .I have looked at examples like - Android 5.0 - Add header/footer to a RecyclerView on the web,but did not understand them properly and the procedure to use it with my existing code.I am looking for a simpler explaination. I want to achieve this functionality in my existing code.Please edit it and tell me what changes are need to be made.

My Adapter

public class PostAdapter extends RecyclerView.Adapter<PostAdapter.PostHandler> {

private LayoutInflater inflater;
Context context;

public PostAdapter(Context con) {
    context = con;
    inflater = LayoutInflater.from(context);
}

@Override
public PostHandler onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = inflater.inflate(R.layout.post_row, parent, false);
    PostHandler postHandler = new PostHandler(v);
    return postHandler;
}

@Override
public void onBindViewHolder(final PostHandler holder, final int position) {
    //The bind actions set text and others happen here...
}

@Override
public int getItemCount() {
    return FreshPostContainer.getInstance().getPosts().size();
}

class PostHandler extends RecyclerView.ViewHolder {

    SimpleDraweeView mprofilePic;
    TextView mpostAuthor;
    ;

    public PostHandler(View itemView) {
        super(itemView);
        mprofilePic = (SimpleDraweeView) itemView.findViewById(R.id.post_profilePic);
        mpostAuthor = (TextView) itemView.findViewById(R.id.post_authorName);         
    }
}

}

MyEndless Scroller

public abstract class EndlessOnScrollRecycler extends RecyclerView.OnScrollListener {

boolean mloading;
private final int AUTO_LOAD_THRESHOLD = 5;
private LinearLayoutManager mlinearLayoutManager;
int firstVisibleItem, visibleItemCount, totalItemCount;

public EndlessOnScrollRecycler(LinearLayoutManager linearLayoutManager) {
    this.mlinearLayoutManager = linearLayoutManager;
}

public void setLoading(boolean loading) {
    this.mloading = loading;
}

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);
    visibleItemCount = recyclerView.getChildCount();
    totalItemCount = mlinearLayoutManager.getItemCount();
    firstVisibleItem = mlinearLayoutManager.findLastVisibleItemPosition();
    if (!mloading) {
        if (totalItemCount - AUTO_LOAD_THRESHOLD <= firstVisibleItem + visibleItemCount) {
            loading();
            setLoading(true);
        }
    }
}

protected abstract void loading();

}
Community
  • 1
  • 1
mik dass
  • 49
  • 7
  • possible duplicate of [Android 5.0 - Add header/footer to a RecyclerView](http://stackoverflow.com/questions/26448717/android-5-0-add-header-footer-to-a-recyclerview) – Rami Sep 25 '15 at 09:00
  • @Rami I went through that example and did not understand it and the procedure to use it with my existing code . I looking for a much simpler explaination which would fit the code I was already using in my project. – mik dass Sep 25 '15 at 09:15

1 Answers1

0

You can override getItemViewType():

@Override
public int getItemViewType(int position) {
   // you can define your view type as your requirement

   return super.getItemViewType(position);
}

@Override
public PostHandler onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = inflater.inflate(R.layout.post_row, parent, false);
    if(viewType == yourviewtype) // inflate your layout accordingly
    PostHandler postHandler = new PostHandler(v);
    return postHandler;
}

And update your posthander constructor:

     public PostHandler(View itemView, int viewType) {
           super(itemView);
           if(viewType == yourrequiredview) {

           }else {
               mprofilePic = (SimpleDraweeView) itemView.findViewById(R.id.post_profilePic);
               mpostAuthor = (TextView) itemView.findViewById(R.id.post_authorName);
           }
     }
    //final adapter class is as

    public class PostAdapter extends RecyclerView.Adapter<PostAdapter.PostHandler> {

        private LayoutInflater inflater;
        Context context;

        public PostAdapter(Context con) {
            context = con;
            inflater = LayoutInflater.from(con);
        }

        @Override
        public PostHandler onCreateViewHolder(ViewGroup parent, int viewType) {
            View v;
            if(viewType == 2){
                v = inflater.inflate(R.layout.post_row, parent, false);
            }else{
                v = inflater.inflate(R.layout.progressbar_layout, parent, false);
            }


            PostHandler postHandler = new PostHandler(v, viewType);

            return postHandler;
        }

        @Override
        public void onBindViewHolder(final PostHandler holder, final int position)
         {if (getItemViewType(position)==2)
              {          //The bind actions set text and others happen here...

        }
        }

        @Override
        public int getItemCount() {
            return FreshPostContainer.getInstance().getPosts().size();
        }

        class PostHandler extends RecyclerView.ViewHolder {

            SimpleDraweeView mprofilePic;
            TextView mpostAuthor;
            ProgressBar mProgressBar;
            ;

            public PostHandler(View itemView) {
                super(itemView);
                mprofilePic = (SimpleDraweeView) itemView.findViewById(R.id.post_profilePic);
                mpostAuthor = (TextView) itemView.findViewById(R.id.post_authorName);
            }
            public PostHandler(View itemView, int viewType) {
                super(itemView);
                if(viewType == 2) {
                    mprofilePic = (SimpleDraweeView) itemView.findViewById(R.id.post_profilePic);
                    mpostAuthor = (TextView) itemView.findViewById(R.id.post_authorName);
                }else {
                    mProgressBar = (ProgressBar) itemView.findViewById(R.id.progressbar);
                }
            }
        }

        @Override
        public int getItemViewType(int position) {
            if(position == (getItemCount()-1)){
                return 1;
            }else {
                return 2;
            }
        }
    }
mik dass
  • 49
  • 7
USKMobility
  • 5,721
  • 2
  • 27
  • 34
  • Thanks for the reply . But I did not get the implemention properly.I am not the getting getItemViewType() and onCreateViewHolder() and how to inflate the views here.Suppose the progress bar is like - progessBar=(ProgressBar)itemview.findViewById(...); where I am I suppose to inflate . Please provide me a complete code with the progressbar variable .Thank you for the reply. – mik dass Sep 25 '15 at 05:11
  • @ USKMobility Thanks it worked with some modification.The changes that I made `@Override public void onBindViewHolder(final PostHandler holder, final int position) { if (getItemViewType(position)==2) {//The bind actions set text and others happen here...} } `. I edited your answer according to the changes otherwise I was getting nullpointerexception. – mik dass Sep 25 '15 at 09:28
  • Can you tell me a method to make the progressBar invisible when the list has reached the end.I tried but could not implement it properly. – mik dass Sep 25 '15 at 10:16