I am trying to get Endless RecyclerView with progress bar showing at bottom when you are loading data from web service. But faced different problems. May be some one needs this.
My StackOverflow answer. Plesae upvote my answer. if it is helpful in your project. http://stackoverflow.com/questions/31000964/how-to-implement-setonscrolllistener-in-recyclerview/31178493#31178493 I want to show complete example on this.
// use this setting to improve performance if you know that changes // in content do not change the layout size of the RecyclerView mRecyclerView.setHasFixedSize(true);
mLayoutManager =new LinearLayoutManager(this);
// use a linear layout manager mRecyclerView.setLayoutManager(mLayoutManager);
// create an Object for Adapter mAdapter =new DataAdapter(studentList, mRecyclerView);
// set the adapter object to the Recyclerview mRecyclerView.setAdapter(mAdapter); // mAdapter.notifyDataSetChanged();
mAdapter.setOnLoadMoreListener(new OnLoadMoreListener(){ @Override publicvoidonLoadMore(){ //add null , so the adapter will check view_type and show progress bar at bottom studentList.add(null); mAdapter.notifyItemInserted(studentList.size()-1);
handler.postDelayed(new Runnable(){ @Override publicvoidrun(){ // remove progress item studentList.remove(studentList.size()-1); mAdapter.notifyItemRemoved(studentList.size()); //add items one by one int start = studentList.size(); int end = start +20;
for(int i = start +1; i <= end; i++){ studentList.add(new Student("Student "+ i,"AndroidStudent"+ i +"@gmail.com")); mAdapter.notifyItemInserted(studentList.size()); } mAdapter.setLoaded(); //or you can add all at once but do not forget to call mAdapter.notifyDataSetChanged(); } },2000);
} });
}
// load initial data privatevoidloadData(){
for(int i =1; i <=20; i++){ studentList.add(new Student("Student "+ i,"androidstudent"+ i +"@gmail.com"));
// The minimum amount of items to have below your current scroll position // before loading more. privateint visibleThreshold =5; privateint lastVisibleItem, totalItemCount; privateboolean loading; private OnLoadMoreListener onLoadMoreListener;
// To set Margin for the child Views params =new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(5,5,5,5);
// LinearLayout acts as parent Layout , we will add child Views to this Layout dynamically layout1 =(LinearLayout) findViewById(R.id.layout1); // Sample Data for Spinner ArrayList<String> spinnerList =new ArrayList<String>(); spinnerList.add("Select"); spinnerList.add("Hyderabad"); spinnerList.add("Banglore"); spinnerList.add("Chennai"); spinnerList.add("Delhi"); spinnerList.add("Mumbai");
// create a button to show/save data , entered in the Form privatevoidsaveButton(){ Button saveButton =new Button(this); saveButton.setHeight(WRAP_CONTENT); saveButton.setText("Save"); saveButton.setOnClickListener(submitListener); layout1.addView(saveButton,params); }