Android recycler view on scroll - android

I want to load more contents from my adapter while scrolling the recycler view.I implemented it, but its not working. my recycler view works perfectly, but I am not able to load rest of the contents from my list while scrolling. Presently there is no error in my code. but I couldn't load more items while scrolling. please help.
This is my main activity.
public class Contact_school extends AppCompatActivity implements SearchView.OnQueryTextListener {
private List<Contact> Listcontact = new ArrayList<>();
Contacts_Adapter cadapter;
private RecyclerView rv;
public String GET_CONTACTS = "http://xyzx.com/Publicpages_mob/brief_school_details";
public Contact contact;
public String school_id;
String tag_json_obj = "json_obj_req";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_school);
rv = (RecyclerView) findViewById(R.id.recycler_view);
preparelist();
cadapter = new Contacts_Adapter(Listcontact, rv);
final LinearLayoutManager layoutManager = new LinearLayoutManager(Contact_school.this);
rv.setLayoutManager(layoutManager);
rv.setItemAnimator(new DefaultItemAnimator());
rv.setAdapter(cadapter);
rv.setHasFixedSize(true);
cadapter.setOnLoadMoreListener(new OnLoadMoreListener() {
#Override
public void onLoadMore() {
Log.v(" KILILII ", "KITTTI");
//add null , so the adapter will check view_type and show progress bar at bottom
//// Listcontact.add(null);
//// cadapter.notifyItemInserted(Listcontact.size() - 1);
// remove progress item
//// Listcontact.remove(Listcontact.size() - 1);
//// cadapter.notifyItemRemoved(Listcontact.size());
// //add items one by one
int start = Listcontact.size();
int end = start + 20;
for (int i = start + 1; i <= end; i++) {
Listcontact.add(contact);
cadapter.notifyItemInserted(Listcontact.size());
}
cadapter.setLoaded();
//or you can add all at once but do not forget to call mAdapter.notifyDataSetChanged();
}
});
}
private void preparelist() {
final StringRequest strReq = new StringRequest(Request.Method.POST, GET_CONTACTS, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONObject jObj = new JSONObject(response);
Log.e("RESPONSE TEST", "" + jObj);
JSONArray jsonArray = jObj.getJSONArray("school_details");
Log.e("RESPONSE ARRAY", "" + jsonArray);
Log.d("SUCESS ", response);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonobject = jsonArray.getJSONObject(i);
contact = new Contact();
contact.setAddress(jsonobject.getString("school_address"));
contact.setRating(jsonobject.getString("school_rating"));
Listcontact.add(contact);
}
cadapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("Error", "Registration Error: " + error.getMessage());
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
}
})
{
#Override
protected Map<String, String> getParams() {
// Posting params to register url
Map<String, String> params = new HashMap<String, String>();
return params;
}
};
AppController.getInstance().addToRequestQueue(strReq, tag_json_obj);
}
this is my adapter class
public class Contacts_Adapter extends RecyclerView.Adapter<Contacts_Adapter.MyViewHolder> {
private List<Contact> List1;
private final int VIEW_ITEM = 1;
private final int VIEW_PROG = 0;
private int visibleThreshold = 5;
private int lastVisibleItem, totalItemCount;
private boolean loading;
private OnLoadMoreListener onLoadMoreListener;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView text_address,text_rating;
public MyViewHolder(View view) {
super(view);
text_address = (TextView)view.findViewById(R.id.textaddress);
text_rating = (TextView)view.findViewById(R.id.textrating);
}
}
public Contacts_Adapter(List<Contact> List1,RecyclerView recyclerView)
{
this.List1 = List1;
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 List1.get(position) != null ? VIEW_ITEM : VIEW_PROG;
}
#Override
public Contacts_Adapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycler, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
Contact c = List1.get(position);
holder.text_rating.setText("Rating "+c.getRating());
holder.text_address.setText(c.getAddress());
}
public void setLoaded() {
loading = false;
}
public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
this.onLoadMoreListener = onLoadMoreListener;
}
this is my model class
public class Contact implements Serializable {
private static final long serialVersionUID = 1L;
private String address,rating;
public Contact(){
}
public Contact (String address, String rating){
this.address = address;
this.rating= rating;
}
public String getAddress(){ return address;}
public String getRating( ){return rating;}
public void setAddress(String ad){ this.address= ad;}
public void setRating(String ad){ this.rating= ad;}
}
and I have this interface
public interface OnLoadMoreListener {
void onLoadMore();
}
please help

You don't need to track the scroll to know when to search more itens.
You can call the Volley request from the onBindViewHolder, for example:
int itemsBeforeUpdate = 10;
public void onBindViewHolder(MyViewHolder holder, final int position) {
Contact c = List1.get(position);
holder.text_rating.setText("Rating "+c.getRating());
holder.text_address.setText(c.getAddress());
if(List1.size() - position <10){
// Do your Volley Request, add response to the list and notify the adapter that the data changed
}
}

Instead of calling OnLoadMoreListener in the following block
if (!loading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
// End has been reached
// Do something
if (onLoadMoreListener != null) {
onLoadMoreListener.onLoadMore();
}
loading = true;
}
Just perform your load more request in this block only. Like:
if (!loading&& totalItemCount <= (lastVisibleItem + visibleThreshold)) {
// Load your volley request or load more request her
loading = true;
}
and after that notify the adapter that data has been changed.

Instead of adding the values to the list in the activity class, try it by adding it to the adapter class. Say for example add an method in adapter like below.
public void addContact(Contact contact) {
list.add(contact);
notifyItemInserted(list.size());
}
And in the activity instead of this,
YOUR CODE:
cadapter.setOnLoadMoreListener(new OnLoadMoreListener() {
#Override
public void onLoadMore() {
Log.v(" KILILII ", "KITTTI");
//add null , so the adapter will check view_type and show progress bar at bottom
//// Listcontact.add(null);
//// cadapter.notifyItemInserted(Listcontact.size() - 1);
// remove progress item
//// Listcontact.remove(Listcontact.size() - 1);
//// cadapter.notifyItemRemoved(Listcontact.size());
// //add items one by one
int start = Listcontact.size();
int end = start + 20;
for (int i = start + 1; i <= end; i++) {
Listcontact.add(contact);
cadapter.notifyItemInserted(Listcontact.size());
}
cadapter.setLoaded();
//or you can add all at once but do not forget to call mAdapter.notifyDataSetChanged();
}
});
TRY THIS:
cadapter.setOnLoadMoreListener(new OnLoadMoreListener() {
#Override
public void onLoadMore() {
Log.v(" KILILII ", "KITTTI");
int start = Listcontact.size();
int end = start + 20;
for (int i = start + 1; i <= end; i++) {
cadapter.addContact(contact);
}
cadapter.setLoaded();
}
});
Hope this helpful :)

Related

When i scroll my recyclerview, image change it's position continuously and repeating images

Image before scroll - Image After Scroll
I'm trying to develop application like wallpaper app when i select any category then open it and when i scroll this RecyclerView that time load page 2 images from api and so on but when i scroll RecyclerView image change it's position and load duplicate of it. plz help me to solved this.
My Adapter :
public class ImageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context mCtx;
private ImageView link;
private boolean isLoading;
private List<Image> imageList;
private int visibleThreshold = 4;
private final int VIEW_TYPE_ITEM = 0;
private final int VIEW_TYPE_LOADING = 1;
private int lastVisibleItem, totalItemCount;
private OnLoadMoreListener onLoadMoreListener;
public ImageAdapter(RecyclerView recyclerView, List<Image> imageList, Context mCtx) {
this.mCtx = mCtx;
this.imageList = imageList;
if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {
final LinearLayoutManager gridLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
totalItemCount = gridLayoutManager.getItemCount();
lastVisibleItem = gridLayoutManager.findLastVisibleItemPosition();
if (!isLoading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
if (onLoadMoreListener != null) {
onLoadMoreListener.onLoadMore();
}
isLoading = true;
}
}
// }
});
}
}
public void setOnLoadMoreListener(OnLoadMoreListener mOnLoadMoreListener) {
this.onLoadMoreListener = mOnLoadMoreListener;
}
#Override
public int getItemViewType(int position) {
return imageList.get(position) == null ? VIEW_TYPE_LOADING : VIEW_TYPE_ITEM;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == VIEW_TYPE_ITEM) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View v = inflater.inflate(R.layout.list_items, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
} else if (viewType == VIEW_TYPE_LOADING) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_loading, parent, false);
LoadingViewHolder vh1 = new LoadingViewHolder(view);
return vh1;
}
return null;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof ViewHolder) {
final Image image = imageList.get(position);
final String imgUrl = image.getThumb();
link.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), ViewImage.class);
intent.putExtra("URL", imgUrl);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
v.getContext().startActivity(intent);
}
});
Glide.with(mCtx).load(imgUrl).into(link);
} else if (holder instanceof LoadingViewHolder) {
LoadingViewHolder loadingViewHolder = (LoadingViewHolder) holder;
loadingViewHolder.progressBar.setIndeterminate(true);
}
}
#Override
public int getItemCount() {
return imageList.size();
}
public void setLoaded() {
isLoading = false;
}
private class LoadingViewHolder extends RecyclerView.ViewHolder {
public ProgressBar progressBar;
LoadingViewHolder(View view) {
super(view);
progressBar = view.findViewById(R.id.progressBar1);
}
}
class ViewHolder extends RecyclerView.ViewHolder {
ViewHolder(View v) {
super(v);
link = v.findViewById(R.id.link);
}
}
}
My Activity :
public class MainActivity extends AppCompatActivity {
int i = 1;
String query;
List<Image> imageList;
RecyclerView listView;
ImageAdapter adapter;
private static String JSON_URL;
private static final String TAG = "Tj";
ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//initializing listview and hero list
listView = findViewById(R.id.listView);
imageList = new ArrayList<>();
Intent intent = getIntent();
query = intent.getStringExtra("category");
JSON_URL = "https://api.unsplash.com/search/photos?query=" + query +
"&client_id=xxxxx&page=" + i;
Log.d(TAG, "Query " + JSON_URL);
loadHeroList();
}
private void loadHeroList() {
//getting the progressbar
progressBar = findViewById(R.id.progressBar);
//making the progressbar visible
progressBar.setVisibility(View.VISIBLE);
//creating a string request to send request to the url
StringRequest jsonArrayRequest = new StringRequest(Request.Method.GET, JSON_URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
//hiding the progressbar after completion
progressBar.setVisibility(View.INVISIBLE);
try {
//getting the whole json object from the response
JSONObject obj = new JSONObject(response);
//we have the array named hero inside the object
//so here we are getting that json array
JSONArray heroArray = obj.getJSONArray("results");
//now looping through all the elements of the json array
for (int i = 0; i < heroArray.length(); i++) {
//getting the json object of the particular index inside the array
JSONObject jsonObject = heroArray.getJSONObject(i);
JSONObject jsonObject1 = jsonObject.getJSONObject("urls");
//creating a hero object and giving them the values from json object
Image hero = new Image(jsonObject.getString("id"),
jsonObject.getString("color"),
jsonObject1.getString("full"));
//adding the hero to herolist
imageList.add(hero);
listView.setHasFixedSize(true);
// use a grid layout manager
listView.setLayoutManager(new GridLayoutManager(MainActivity.this, 2));
}
//creating custom adapter object
adapter = new ImageAdapter(listView, imageList, getApplicationContext());
//adding the adapter to listview
listView.setAdapter(adapter);
//Load More Pages Start Here
adapter.setOnLoadMoreListener(new OnLoadMoreListener() {
#Override
public void onLoadMore() {
loadMoreData();
}
});
//Complete for Load More Pages
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//displaying the error in toast if occurrs
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
//creating a request queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//adding the string request to request queue
requestQueue.add(jsonArrayRequest);
}
private void loadMoreData() {
imageList.add(null);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
adapter.notifyItemInserted(imageList.size() - 1);
imageList.remove(imageList.size() - 1);
adapter.notifyItemRemoved(imageList.size());
i++;
Log.d(TAG, "ILoadMore " + i);
String JSON_URL_LoadMore = "https://api.unsplash.com/search/photos?query=" + query + "&client_id=xxxxx&page=" + i;
Log.d(TAG, "QueryLoadMore " + JSON_URL_LoadMore);
StringRequest jsonArrayRequestLoadMore = new StringRequest(Request.Method.GET, JSON_URL_LoadMore,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
//hiding the progressbar after completion
progressBar.setVisibility(View.INVISIBLE);
try {
//getting the whole json object from the response
JSONObject obj = new JSONObject(response);
//we have the array named hero inside the object
//so here we are getting that json array
JSONArray heroArray = obj.getJSONArray("results");
//now looping through all the elements of the json array
for (int i = 0; i < heroArray.length(); i++) {
//getting the json object of the particular index inside the array
JSONObject jsonObject = heroArray.getJSONObject(i);
JSONObject jsonObject1 = jsonObject.getJSONObject("urls");
//creating a hero object and giving them the values from json object
Image hero = new Image(jsonObject.getString("id"),
jsonObject.getString("color"),
jsonObject1.getString("full"));
//adding the hero to herolist
imageList.add(hero);
adapter.notifyItemInserted(imageList.size());
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//displaying the error in toast if occurrs
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
//creating a request queue
RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
//adding the string request to request queue
requestQueue.add(jsonArrayRequestLoadMore);
adapter.notifyDataSetChanged();
adapter.setLoaded();
}
}, 5000);
}
}
Override
#Override
public long getItemId(int position) {
return position;
}
And use
Picasso.with(context)
.load(getImgUrl)
.placeholder(R.drawable.user_placeholder)
.fit()
.into(imageView);
Or else
To retain and restore recyclerview position on scrolling, please try below link.
https://panavtec.me/retain-restore-recycler-view-scroll-position

How to populate RecyclerView with large json data [duplicate]

This question already has answers here:
How to implement endless list with RecyclerView?
(36 answers)
Closed 5 years ago.
I have a REST method that returns JSON object,the JSON file size almost weighs 7MB and has almost 4600 JSON object. I am unable to parse the entire data at a time into recyclerView as it causes OutOfMemory Exception.
I tried to implement it in 2 ways. One is using http://loopj.com/android-async-http/ but this method is not feasible to large chunk of data. The second method was done using GSON and Volley. Using this method i am able to get the response much faster from the REST method but i'm unable to populate it into recyclerView. Please do check out the code that i used.
The problem that i'm facing here is that recyclerview shows null values for ROLL_NUM, CURRENT_CLASS, STUDENT_NAME
sample JSON response: {"RestResult":[{""ROLL_NUM":7071,"CURRENT_CLASS":"Grade LKG C","STUDENT_NAME":"A. VEDANJALI AKULA VEDANJALI"}
ListItem.class
String STUDENT_NAME;
String CURRENT_CLASS;
String ROLL_NUMBER;
public String getSTUDENT_NAME() {
return STUDENT_NAME;
}
public String getCURRENT_CLASS() {
return CURRENT_CLASS;
}
public String getROLL_NUMBER() {
return ROLL_NUMBER;
}
public GetSet(String STUDENT_NAME, String CURRENT_CLASS, String ROLL_NUMBER) {
this.STUDENT_NAME = STUDENT_NAME;
this.CURRENT_CLASS = CURRENT_CLASS;
this.ROLL_NUMBER = ROLL_NUMBER;
}
RecyclerAdapter.class
private List<GetSet> itemList;
private Context context;
public RecyclerViewAdapter(Context context, List<GetSet> itemList) {
this.itemList = itemList;
this.context = context;
}
#Override
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, null);
RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView);
return rcv;
}
#Override
public void onBindViewHolder(RecyclerViewHolders holder, int position) {
holder.songTitle.setText("Student Name: " + itemList.get(position).getSTUDENT_NAME());
holder.songYear.setText("Current Class: " + itemList.get(position).getCURRENT_CLASS());
holder.songAuthor.setText("Roll Number: " + itemList.get(position).getROLL_NUMBER());
}
#Override
public int getItemCount() {
return this.itemList.size();
}
ActivityMain.class
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://webservices.educatemax.com/vidyaMandir.svc/RetrieveStudentsList?iGroupID=1&iSchoolID=1&iBoardID=1&iClassID=1";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
progressDialog.hide();
Log.d(TAG, "Response " + response);
GsonBuilder builder = new GsonBuilder();
Gson mGson = builder.create();
List<GetSet> posts = new ArrayList<>();
posts = Arrays.asList(mGson.fromJson(response, GetSet.class));
adapter = new RecyclerViewAdapter(ActivityMain.this, posts);
recyclerView.setAdapter(adapter);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "Error " + error.getMessage());
}
});
queue.add(stringRequest);
you can use an scroll listener like this :
public abstract class RecyclerViewOnScrollListener extends RecyclerView.OnScrollListener {
private final int mVisibleThreshold = 1;
private int mPreviousTotal = 0;
private boolean isLoading = true;
private int mFirstVisibleItem;
private int mVisibleItemsCount;
private int mTotalItemsCount;
private int mCurrentPage = 0;
private LinearLayoutManager mLayoutManager;
public RecyclerViewOnScrollListener(LinearLayoutManager manager) {
this.mLayoutManager = manager;
}
public void init() {
this.mPreviousTotal = 0;
this.isLoading = true;
this.mCurrentPage = 0;
}
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
mVisibleItemsCount = recyclerView.getChildCount();
mTotalItemsCount = mLayoutManager.getItemCount();
mFirstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
if (isLoading && mTotalItemsCount > mPreviousTotal) {
isLoading = false;
mPreviousTotal = mTotalItemsCount;
}
if (!isLoading && (mTotalItemsCount - mVisibleItemsCount) <= (mFirstVisibleItem + mVisibleThreshold)) {
mCurrentPage = mCurrentPage + 10;
onLoadMore(mCurrentPage);
isLoading = true;
}
}
public abstract void onLoadMore(int currentPage);
}
and in your Activity of Fragment(anyWhere you have your recyclerView):
mRecyclerOnScrollListener = new RecyclerViewOnScrollListener(new GridLayoutManager(MyActivity.this, getResources().getInteger(R.integer.num_columns))) {
#Override
public void onLoadMore(int currentPage) {
//here you can manage content and add to your adaprer
}
};

SetOnScrollListener in RecyclerView [duplicate]

How Do I show progress bar at bottom when user reached to items those are visible in a list.
I have written a code in which i am getting data using web service, now i would like to populate partial records, because i have around 630 records in my JSON.
Here is my whole code which i am using to get data from JSON and to populate into RecyclerView.
Here is how my JSON looks, but real JSON contains over 600 records:
http://walldroidhd.com/api.php
Can someone guide me where i have to make changes in my code ?
I want to populate more records whenever user do scroll to bottom using progressbar, still i am showing all the records.
RecyclerViewFragment.java:
public class RecyclerViewFragment extends Fragment {
RecyclerView mRecyclerView;
LinearLayoutManager mLayoutManager;
RecyclerView.Adapter mAdapter;
ArrayList<NatureItem> actorsList;
private int previousTotal = 0;
private boolean loading = true;
private int visibleThreshold = 5;
int firstVisibleItem, visibleItemCount, totalItemCount;
public static RecyclerViewFragment newInstance() {
return new RecyclerViewFragment();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_recyclerview_advance, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
actorsList = new ArrayList<NatureItem>();
new JSONAsyncTask().execute("my JSON url");
mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new GridLayoutManager(getActivity(), 2);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
visibleItemCount = mRecyclerView.getChildCount();
totalItemCount = mLayoutManager.getItemCount();
firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
if (loading) {
if (totalItemCount > previousTotal) {
loading = false;
previousTotal = totalItemCount;
}
}
if (!loading && (totalItemCount - visibleItemCount)
<= (firstVisibleItem + visibleThreshold)) {
// End has been reached
Log.i("...", "end called");
// Do something
loading = true;
}
}
});
// mAdapter = new CardAdapter();
mAdapter = new RecyclerViewMaterialAdapter(new CardAdapter(getActivity(), actorsList), 2);
mRecyclerView.setAdapter(mAdapter);
MaterialViewPagerHelper.registerRecyclerView(getActivity(), mRecyclerView, null);
mRecyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
Toast.makeText(getActivity(), String.valueOf(position), Toast.LENGTH_LONG).show();
}
})
);
}
class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {
ProgressDialog dialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(getActivity());
dialog.setMessage("Loading, please wait");
dialog.setTitle("Connecting server");
dialog.show();
dialog.setCancelable(false);
}
#Override
protected Boolean doInBackground(String... urls) {
try {
//------------------>>
HttpGet httppost = new HttpGet(urls[0]);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
// StatusLine stat = response.getStatusLine();
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = null;
try {
data = EntityUtils.toString(entity);
} catch (IOException e) {
e.printStackTrace();
}
JSONObject jsono = null;
try {
jsono = new JSONObject(data);
} catch (JSONException e) {
e.printStackTrace();
}
JSONArray jarray = jsono.getJSONArray("wallpapers");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
NatureItem actor = new NatureItem();
actor.setName(object.getString("id"));
actor.setThumbnail(object.getString("thumb_url"));
actorsList.add(actor);
}
return true;
}
//------------------>>
} catch (ParseException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
protected void onPostExecute(Boolean result) {
dialog.cancel();
mAdapter.notifyDataSetChanged();
if (result == false) {
Toast.makeText(getActivity(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
}
else {
}
}
}
}
CardAdapter.java:
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {
private ArrayList<NatureItem> mItems;
private Context mContext;
public CardAdapter(Context context, ArrayList<NatureItem> feedItemList) {
this.mItems = feedItemList;
this.mContext = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.recycler_view_card_item, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
NatureItem nature = mItems.get(i);
viewHolder.tvNature.setText(nature.getName());
Picasso.with(mContext).load(nature.getThumbnail())
.error(R.mipmap.ic_launcher)
.placeholder(R.mipmap.ic_launcher)
.into(viewHolder.imgThumbnail);
}
#Override
public int getItemCount() {
return mItems.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public ImageView imgThumbnail;
public TextView tvNature;
public ViewHolder(View itemView) {
super(itemView);
imgThumbnail = (ImageView)itemView.findViewById(R.id.img_thumbnail);
tvNature = (TextView)itemView.findViewById(R.id.tv_nature);
}
}
}
Here is how i have implemented Endless as well, using this:
mRecyclerView.setOnScrollListener(new EndlessRecyclerOnScrollListener(linearLayoutManager) {
#Override
public void onLoadMore(int current_page) {
// do something...
}
});
Note: I would personally prefer RecyclerView onScroll functionality to get my work done.
Activity Class with recylcerview in xml layout file
public class WallpaperActivity extends AppCompatActivity implements OnTaskCompleted {
private static final String TAG = "WallpaperActivity";
private Toolbar toolbar;
private RecyclerView mRecyclerView;
private WallPaperDataAdapter mAdapter;
private LinearLayoutManager mLayoutManager;
// to keep track which pages loaded and next pages to load
public static int pageNumber;
private List<WallPaper> wallpaperImagesList;
protected Handler handler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wallpaper_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
pageNumber = 1;
wallpaperImagesList = new ArrayList<WallPaper>();
handler = new Handler();
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("WallPapers");
}
// 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 WallPaperDataAdapter(wallpaperImagesList, mRecyclerView);
// set the adapter object to the Recyclerview
mRecyclerView.setAdapter(mAdapter);
getWebServiceData();
mAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
#Override
public void onLoadMore() {
//add null , so the adapter will check view_type and show progress bar at bottom
wallpaperImagesList.add(null);
mAdapter.notifyItemInserted(wallpaperImagesList.size() - 1);
++pageNumber;
getWebServiceData();
}
});
}
public void getWebServiceData() {
BackGroundTask backGroundTask = new BackGroundTask(this, this, pageNumber);
backGroundTask.execute();
}
#Override
public void onTaskCompleted(String response) {
parsejosnData(response);
}
public void parsejosnData(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
// String json = jsonObject.toString();
JSONArray jsonArray = jsonObject.getJSONArray("wallpapers");
if (jsonArray != null) {
// looping through All albums
if (pageNumber > 1) {
wallpaperImagesList.remove(wallpaperImagesList.size() - 1);
mAdapter.notifyItemRemoved(wallpaperImagesList.size());
}
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject c = jsonArray.getJSONObject(i);
// Storing each json item values in variable
String id = c.getString("id");
String orig_url = c.getString("orig_url");
String thumb_url = c.getString("thumb_url");
String downloads = c.getString("downloads");
String fav = c.getString("fav");
// Creating object for each product
WallPaper singleWall = new WallPaper(id, orig_url, thumb_url, downloads, fav);
// adding HashList to ArrayList
wallpaperImagesList.add(singleWall);
handler.post(new Runnable() {
#Override
public void run() {
mAdapter.notifyItemInserted(wallpaperImagesList.size());
}
});
}
mAdapter.setLoaded();
} else {
Log.d("Wallpapers: ", "null");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
Adapter Class
public class WallPaperDataAdapter extends RecyclerView.Adapter {
private final int VIEW_ITEM = 1;
private final int VIEW_PROG = 0;
private List<WallPaper> imagesList;
// The minimum amount of items to have below your current scroll position
// before loading more.
private int visibleThreshold = 5;
private int lastVisibleItem, totalItemCount;
private boolean loading;
private OnLoadMoreListener onLoadMoreListener;
public WallPaperDataAdapter(List<WallPaper> imagesList1, RecyclerView recyclerView) {
imagesList = imagesList1;
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 imagesList.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(
R.layout.wallpaper_row, parent, false);
vh = new WallPaperViewHolder(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 WallPaperViewHolder) {
WallPaper singleWallPaper = (WallPaper) imagesList.get(position);
Glide.with(((WallPaperViewHolder) holder).thumbIcon.getContext())
.load(singleWallPaper.getThumbUrl())
.centerCrop()
.placeholder(R.drawable.bg)
.crossFade()
.into(((WallPaperViewHolder) holder).thumbIcon);
} else {
((ProgressViewHolder) holder).progressBar.setIndeterminate(true);
}
}
public void setLoaded() {
loading = false;
}
#Override
public int getItemCount() {
return imagesList.size();
}
public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
this.onLoadMoreListener = onLoadMoreListener;
}
//
public static class WallPaperViewHolder extends RecyclerView.ViewHolder {
public ImageView thumbIcon;
public WallPaperViewHolder(View v) {
super(v);
thumbIcon = (ImageView) v.findViewById(R.id.thumbIcon);
}
}
public static class ProgressViewHolder extends RecyclerView.ViewHolder {
public ProgressBar progressBar;
public ProgressViewHolder(View v) {
super(v);
progressBar = (ProgressBar) v.findViewById(R.id.progressBar1);
}
}
}
wallpaper_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/thumbIcon"
android:layout_width="160dp"
android:layout_height="160dp"
android:layout_centerInParent="true"
android:layout_margin="2dp"
android:gravity="center" />
</RelativeLayout>
progress_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ProgressBar
android:id="#+id/progressBar1"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_height="wrap_content" />
</LinearLayout>
Separate BackGroundTask.java
public class BackGroundTask extends AsyncTask<Object, Void, String> {
private ProgressDialog pDialog;
public OnTaskCompleted listener = null;//Call back interface
Context context;
int pageNumber;
public BackGroundTask(Context context1, OnTaskCompleted listener1, int pageNumber) {
context = context1;
listener = listener1; //Assigning call back interface through constructor
this.pageNumber = pageNumber;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected String doInBackground(Object... params) {
//My Background tasks are written here
synchronized (this) {
String url = Const.URL_WALLPAPERS_HD + pageNumber;
String jsonStr = ServiceHandler.makeServiceCall(url, ServiceHandler.GET);
Log.i("Url: ", "> " + url);
Log.i("Response: ", "> " + jsonStr);
return jsonStr;
}
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
listener.onTaskCompleted(result);
}
}
ServiceHanlder.java
public class ServiceHandler {
static String response = null;
public final static int GET = 1;
public final static int POST = 2;
public ServiceHandler() {
}
/**
* Making service call
*
* #url - url to make request
* #method - http request method
*/
public static String makeServiceCall(String url, int method) {
return makeServiceCall(url, method, null);
}
/**
* Making service call
*
* #url - url to make request
* #method - http request method
* #params - http request params
*/
public static String makeServiceCall(String url, int method,
List<NameValuePair> params) {
try {
// http client
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
// Checking http request method type
if (method == POST) {
HttpPost httpPost = new HttpPost(url);
// adding post params
if (params != null) {
httpPost.setEntity(new UrlEncodedFormEntity(params));
}
Log.e("Selltis Request URL", url);
httpResponse = httpClient.execute(httpPost);
} else if (method == GET) {
// appending params to url
if (params != null) {
String paramString = URLEncodedUtils
.format(params, "utf-8");
url += paramString;
Log.i("Request URL", url);
}
HttpGet httpGet = new HttpGet(url);
httpResponse = httpClient.execute(httpGet);
}
httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return "Fail";
} catch (ClientProtocolException e) {
e.printStackTrace();
return "Fail";
} catch (IOException e) {
e.printStackTrace();
return "Fail";
}
return response;
}
}
Interface for Load More
public interface OnLoadMoreListener {
void onLoadMore();
}
Interface to know web service data loaded from asynctask
public interface OnTaskCompleted{
void onTaskCompleted(String response);
}
Please let me know if this works or any issues for you.
Better to use Volley or okHttp Libraries for Networking.
For ImageLoading i used Glide Library.
This is how I detect whether RecyclerView should refresh by OnScrollListener, take a look at it:
recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
int ydy = 0;
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int offset = dy - ydy;
ydy = dy;
boolean shouldRefresh = (linearLayoutManager.findFirstCompletelyVisibleItemPosition() == 0)
&& (recyclerView.getScrollState() == RecyclerView.SCROLL_STATE_DRAGGING) && offset > 30;
if (shouldRefresh) {
//swipeRefreshLayout.setRefreshing(true);
//Refresh to load data here.
return;
}
boolean shouldPullUpRefresh = linearLayoutManager.findLastCompletelyVisibleItemPosition() == linearLayoutManager.getChildCount() - 1
&& recyclerView.getScrollState() == RecyclerView.SCROLL_STATE_DRAGGING && offset < -30;
if (shouldPullUpRefresh) {
//swipeRefreshLayout.setRefreshing(true);
//refresh to load data here.
return;
}
swipeRefreshLayout.setRefreshing(false);
}
});
Hope you'll be inspired. Good luck~
To implement OnScrollListener in Kotlin for RecyclerView, you can use
recyclerViewChat.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
Log.d("scroll", "idle")
} else if (newState == RecyclerView.SCROLL_STATE_SETTLING) {
Log.d("scroll", "settling")
} else if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
Log.d("scroll", "dragging")
}
}
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
Log.d("scroll", "scrolling")
}
})
Another example. Set you progress bar at bottom and change its visibility according to scrolling/loading and your records. Note: you need to call notifyDataSetChanged(); method to add/refresh data to adapter
recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int total = linearLayoutManager.getItemCount();
int firstVisibleItemCount = linearLayoutManager.findFirstVisibleItemPosition();
int lastVisibleItemCount = linearLayoutManager.findLastVisibleItemPosition();
//to avoid multiple calls to loadMore() method
//maintain a boolean value (isLoading). if loadMore() task started set to true and completes set to false
if (!isLoading) {
if (total > 0)
if ((total - 1) == lastVisibleItemCount){
loadMore();//your HTTP stuff goes in this method
loadingProgress.setVisibility(View.VISIBLE);
}else
loadingProgress.setVisibility(View.GONE);
}
}
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
});
The idea to implement load-more is :
Get all records in one collection. Take another collection which will populate partial records lets say 10 records. Now, when u reach at bottom, populate next 10 records in the collection and notify the list.
So your code will be something like this :
ArrayList<NatureItem> tempList; // which holds partial records
tempList = getList(limit, 10);
mAdapter = new RecyclerViewMaterialAdapter(new CardAdapter(getActivity(), tempList), 2);
mRecyclerView.setAdapter(mAdapter);
Now, code to load next records :
limit += 10;
tempList.addAll(getList(limit, 10));
mAdapter.notifyDataSetChanged();
To show he loader/progress, better to use load-more library
In the onScroll method
if (!loading && (totalItemCount - visibleItemCount)
<= (firstVisibleItem + visibleThreshold)) {
// End has been reached
Log.i("...", "end called");
// Do something
new JSONAsyncTask().execute("http://walldroidhd.com/api.php");
loading = true;
}
Now accepted answer as #SilentKnight suggested will not work as setOnscroll is deprecated by Android SDK.
You should use
recyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
int ydy = 0;
#Override
public void onScrollStateChanged(#NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
#Override
public void onScrolled(#NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int offset = dy - ydy;
ydy = dy;
boolean shouldRefresh = (linearLayoutManager.findFirstCompletelyVisibleItemPosition() == 0)
&& (recyclerView.getScrollState() == RecyclerView.SCROLL_STATE_DRAGGING) && offset > 30;
if (shouldRefresh) {
//swipeRefreshLayout.setRefreshing(true);
//Refresh to load data here.
return;
}
boolean shouldPullUpRefresh = linearLayoutManager.findLastCompletelyVisibleItemPosition() == linearLayoutManager.getChildCount() - 1
&& recyclerView.getScrollState() == RecyclerView.SCROLL_STATE_DRAGGING && offset < -30;
if (shouldPullUpRefresh) {
//swipeRefreshLayout.setRefreshing(true);
//refresh to load data here.
return;
}
swipeRefreshLayout.setRefreshing(false);
}
});

Set last position when scroll load more recycleview

I am trying to implement load more recycleview It's working fine but when scroll recycleview at that time always first item is top of the view, I want next scrollable item at the top, How can I solve this problem, Please help me
MainActivity.java
public class MainActivity extends AppCompatActivity {
private int page_no = 0;
private RecyclerView mRecyclerView;
private ArrayList<NotificationBean> mUsers = new ArrayList<>();
private UserAdapter mUserAdapter;
private NotificationBean mNotificationBean;
private int loadMoreArrayListSize;
public int arrayListSize;
// private LinearLayoutManager mLayoutManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mUsers = new ArrayList<>();
mRecyclerView = (RecyclerView) findViewById(R.id.recycleView);
// mLayoutManager = new LinearLayoutManager(this);
// mRecyclerView.setLayoutManager(mLayoutManager);
getData(MainActivity.this, page_no, "4");
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mUserAdapter = new UserAdapter();
mUserAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
#Override
public void onLoadMore() {
Log.e("haint", "Load More");
mUsers.add(null);
mUserAdapter.notifyItemInserted(mUsers.size() - 1);
//Load more data for reyclerview
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Log.e("haint", "Load More 2");
//Remove loading item
mUsers.remove(mUsers.size() - 1);
mUserAdapter.notifyItemRemoved(mUsers.size());
//Load data
page_no++;
//callAPI(1);
// Log.d("arrTemp Position: : ", String.valueOf(arrTemp));
getData(MainActivity.this, page_no, "4");
// mLayoutManager.scrollToPositionWithOffset(mUsers.size() - 10, 0);
Log.v("LoadMoreListener arsize", Integer.toString(mUsers.size()));
loadMoreArrayListSize = arrayListSize - mUsers.size();
Log.v("loadMoreArrayListSize", Integer.toString(loadMoreArrayListSize));
}
}, 5000);
}
});
}
static class UserViewHolder extends RecyclerView.ViewHolder {
public ImageView imgView;
public TextView txtComment, txtParamLink, txtTitle, txtVideoId;
public UserViewHolder(View itemView) {
super(itemView);
txtComment = (TextView) itemView.findViewById(R.id.txtComment);
txtParamLink = (TextView) itemView.findViewById(R.id.txtParamLink);
txtTitle = (TextView) itemView.findViewById(R.id.txtTitle);
txtVideoId = (TextView) itemView.findViewById(R.id.txtVideoId);
imgView = (ImageView) itemView.findViewById(R.id.imgView);
}
}
static class LoadingViewHolder extends RecyclerView.ViewHolder {
public ProgressBar progressBar;
public LoadingViewHolder(View itemView) {
super(itemView);
progressBar = (ProgressBar) itemView.findViewById(R.id.progressBar1);
}
}
class UserAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final int VIEW_TYPE_ITEM = 0;
private final int VIEW_TYPE_LOADING = 1;
private OnLoadMoreListener mOnLoadMoreListener;
private boolean isLoading;
private int visibleThreshold = 5;
private int lastVisibleItem, totalItemCount;
public UserAdapter() {
final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();
mRecyclerView.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 (!isLoading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
if (mOnLoadMoreListener != null) {
mOnLoadMoreListener.onLoadMore();
}
isLoading = true;
}
}
});
}
public void setOnLoadMoreListener(OnLoadMoreListener mOnLoadMoreListener) {
this.mOnLoadMoreListener = mOnLoadMoreListener;
}
#Override
public int getItemViewType(int position) {
return mUsers.get(position) == null ? VIEW_TYPE_LOADING : VIEW_TYPE_ITEM;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == VIEW_TYPE_ITEM) {
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.notification_item, parent, false);
return new UserViewHolder(view);
} else if (viewType == VIEW_TYPE_LOADING) {
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_loading_item, parent, false);
return new LoadingViewHolder(view);
}
return null;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof UserViewHolder) {
NotificationBean user = mUsers.get(position);
UserViewHolder userViewHolder = (UserViewHolder) holder;
// userViewHolder.tvName.setText(user.getName());
// userViewHolder.tvEmailId.setText(user.getEmail());
userViewHolder.txtComment.setText(Integer.toString(user.getComment()));
userViewHolder.txtParamLink.setText(user.getPermalink());
userViewHolder.txtTitle.setText(user.getTitle());
userViewHolder.txtVideoId.setText(user.getVideoid());
if (user.getImage() != null) {
Glide.with(MainActivity.this).load(user.getImage()).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).dontAnimate().into(userViewHolder.imgView);
}
} else if (holder instanceof LoadingViewHolder) {
LoadingViewHolder loadingViewHolder = (LoadingViewHolder) holder;
loadingViewHolder.progressBar.setIndeterminate(true);
}
}
#Override
public int getItemCount() {
return mUsers == null ? 0 : mUsers.size();
}
public void setLoaded() {
isLoading = false;
}
}
private void getData(Context context, final int posts_per_page, final String type) {
// final ProgressDialog pDialog = new ProgressDialog(this);
// pDialog.setMessage("Loading...");
// pDialog.show();
RequestQueue queue = Volley.newRequestQueue(context);
// StringRequest sr = new StringRequest(Request.Method.POST, "http://asiatube.info/sgtube/api/ws.php", new Response.Listener<String>() {
StringRequest sr = new StringRequest(Request.Method.POST, "http://steanrewards.com/api/ws.php", new Response.Listener<String>() {
#Override
public void onResponse(String response) {
JSONArray jArray;
JSONArray jsonArray = null;
JSONObject jsonObject = null;
try {
jArray = new JSONArray(response);
JSONObject jObj = jArray.getJSONObject(0);
int code = jObj.getInt("code");
if (code == 0) {
Log.d("allcount:: :: ::", String.valueOf(jObj.optInt("allcount")));
if (jObj.has("result")) {
jsonArray = jObj.getJSONArray("result");
Log.d("EVENTLIST ARRAY=", jsonArray.length() + "");
if (jsonArray != null && jsonArray.length() > 0) {
// arrTemp = new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
jsonObject = jsonArray.getJSONObject(i);
mNotificationBean = new NotificationBean();
mNotificationBean.id = jsonObject.getString("id");
mNotificationBean.permalink = jsonObject.getString("permalink");
mNotificationBean.image = jsonObject.getString("image");
mNotificationBean.title = jsonObject.getString("title");
mNotificationBean.videotype = jsonObject.getString("videotype");
mNotificationBean.videoid = jsonObject.getString("videoid");
mNotificationBean.desc = jsonObject.getString("desc");
mNotificationBean.author_url = jsonObject.getString("author_url");
mNotificationBean.like = jsonObject.getString("like");
mNotificationBean.unlike = jsonObject.getString("unlike");
mNotificationBean.comment = jsonObject.getInt("comment");
//arrTemp.add(mNotificationBean);
mUsers.add(mNotificationBean);
Log.v("ArrayList Size:: :: ", Integer.toString(mUsers.size()));
mRecyclerView.setAdapter(mUserAdapter);
mUserAdapter.notifyDataSetChanged();
mUserAdapter.setLoaded();
}
Log.v("mUsers Size:: :: ", Integer.toString(mUsers.size()));
arrayListSize = mUsers.size();
}
//mAdapter.notifyDataSetChanged();
// mAdapter = new NotificationAdapter(mArrayList);
// pDialog.dismiss();
}
} else {
// pDialog.dismiss();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "Check internet connection", Toast.LENGTH_LONG).show();
}
}) {
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("posts_per_page", Integer.toString(posts_per_page));
params.put("type", type);
// params.put("fromsite", fromsite);
return params;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/x-www-form-urlencoded");
return params;
}
};
sr.setRetryPolicy(new DefaultRetryPolicy(
15000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
queue.add(sr);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycleView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp" />
</LinearLayout>
When you are loading you must be knowing the position of top element of new loaded items. just call:
recyclerView.scrollToPosition(position);
And you can use it whenever you want to show specific item on current view, just call this function with their list position. For example you are loading new item after every 80 items so, position of newly loaded item will be 80 so you can call:
recyclerView.scrollToPosition(80);
Just keep on updating this position as you load more and more items.
although,recyclerView.scrollToPosition(position); is a quick fix i would suggest you don't use it, genrally this happens when you do setAdapter() again after the pagination, you need to call notifyDataSetChanged after the new data is got

RecyclerView Endless Infinite Scrolling Issue

I am trying to implement Endless Infinite Scrolling with RecyclerView, but I am only getting first 10 records, not getting next 10 records and even not getting any progress while trying to scroll at bottom.
Whereas I was suppose to get next 10 records on scroll and so on... But getting only first 10 records
Here I have uploaded copy of my JSON - But i am not able to fetch data from same url, that's why i am using client's url and local host.
I am following this tutorial
Here is my complete code, May I know where I am doing mistake ?
JSON :
{
"names": [
{
"name": "Name 1"
},
{
"name": "Name 2"
},
....
{
"name": "Name 60"
}
]
}
Log:
D/name -(13759): Name 1
D/name -(13759): Name 2
.......................
D/name -(13759): Name 60
Here is my updated code, which i am using to parse JSON data
MainActivity.java: UPDATED
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TextView tvEmptyView;
private RecyclerView mRecyclerView;
private DataAdapter mAdapter;
private LinearLayoutManager mLayoutManager;
private ArrayList<Student> studentList;
protected Handler handler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
tvEmptyView = (TextView) findViewById(R.id.empty_view);
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
studentList = new ArrayList<Student>();
handler = new Handler();
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Android Students");
}
loadData();
}
// load initial data
private void loadData() {
new Parser().execute("http://10.0.2.2/jsons/mytest.txt");
}
class Parser extends AsyncTask<String, Void, Boolean> {
ProgressDialog dialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(MainActivity.this);
dialog.show();
dialog.setCancelable(false);
}
#Override
protected Boolean doInBackground(String... urls) {
try {
//------------------>>
HttpGet httppost = new HttpGet(urls[0]);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
// StatusLine stat = response.getStatusLine();
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jsono = new JSONObject(data);
JSONArray jarray = jsono.getJSONArray("names");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
Student actor = new Student();
actor.setName(object.getString("name"));
Log.d("name - ", object.getString("name"));
studentList.add(actor);
}
Log.d("MainActivity:StudentList ", "The size "+studentList.size());
return true;
}
//------------------>>
} catch (ParseException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
protected void onPostExecute(Boolean result) {
dialog.cancel();
Log.d("MainActivity:StudentList ", "The size "+studentList.size());
ArrayList< Student > temArray =
new ArrayList< Student >(studentList.subList(0, 10));
mAdapter = new DataAdapter(temArray, mRecyclerView);
Log.d("MainActivity:TempList ", "The size "+temArray.size());
// set the adapter object to the Recyclerview
mRecyclerView.setAdapter(mAdapter);
// 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(MainActivity.this);
// use a linear layout manager
mRecyclerView.setLayoutManager(mLayoutManager);
if (studentList.isEmpty()) {
mRecyclerView.setVisibility(View.GONE);
tvEmptyView.setVisibility(View.VISIBLE);
} else {
mRecyclerView.setVisibility(View.VISIBLE);
tvEmptyView.setVisibility(View.GONE);
}
mAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
#Override
public void onLoadMore() {
//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
public void run() {
// remove progress item
studentList.remove(studentList.size() - 1);
mAdapter.notifyItemRemoved(studentList.size());
//add items one by one
int start = studentList.size();
int end = start + 10;
for (int i = start + 1; i < end; i++) {
mAdapter.notifyItemInserted(studentList.size());
}
mAdapter.setLoaded();
//or you can add all at once but do not forget to call mAdapter.notifyDataSetChanged();
}
}, 2000);
}
});
}
}
}
DataAdapter.java:
public class DataAdapter extends RecyclerView.Adapter {
private final int VIEW_ITEM = 1;
private final int VIEW_PROG = 0;
private List<Student> studentList;
// The minimum amount of items to have below your current scroll position
// before loading more.
private int visibleThreshold = 5;
private int lastVisibleItem, totalItemCount;
private boolean loading;
private OnLoadMoreListener onLoadMoreListener;
public DataAdapter(List<Student> students, RecyclerView recyclerView) {
studentList = students;
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 studentList.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(
R.layout.list_row, parent, false);
vh = new StudentViewHolder(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 StudentViewHolder) {
Student singleStudent= (Student) studentList.get(position);
((StudentViewHolder) holder).tvName.setText(singleStudent.getName());
((StudentViewHolder) holder).student= singleStudent;
} else {
((ProgressViewHolder) holder).progressBar.setIndeterminate(true);
}
}
public void setLoaded() {
loading = false;
}
#Override
public int getItemCount() {
return studentList.size();
}
public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
this.onLoadMoreListener = onLoadMoreListener;
}
//
public static class StudentViewHolder extends RecyclerView.ViewHolder {
public TextView tvName;
public Student student;
public StudentViewHolder(View v) {
super(v);
tvName = (TextView) v.findViewById(R.id.tvName);
v.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(),
"OnClick :" + student.getName(),
Toast.LENGTH_SHORT).show();
}
});
}
}
public static class ProgressViewHolder extends RecyclerView.ViewHolder {
public ProgressBar progressBar;
public ProgressViewHolder(View v) {
super(v);
progressBar = (ProgressBar) v.findViewById(R.id.progressBar1);
}
}
}
OnLoadMoreListener.java:
public interface OnLoadMoreListener {
void onLoadMore();
}
I had the same issue once an I solve it using this code ...
First .. create this class
public abstract class EndlessOnScrollListener extends OnScrollListener {
public static String TAG = EndlessOnScrollListener.class.getSimpleName();
// use your LayoutManager instead
private LinearLayoutManager llm;
public EndlessOnScrollListener(LinearLayoutManager sglm) {
this.lm = llm;
}
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (!recyclerView.canScrollVertically(1)) {
onScrolledToEnd();
}
}
public abstract void onScrolledToEnd();
}
Second .. in you activity use this
recyclerView.addOnScrollListener(new EndlessOnScrollListener() {
#Override
public void onScrolledToEnd() {
if (!loading) {
loading = true;
// add 10 by 10 to tempList then notify changing in data
}
loading = false;
}
});
This works for me .... I hope it works for you to.
Try notifyItemRangeChanged
yourCurrentList.addAll(newData);
mAdapter.notifyItemRangeChanged(yourCurretList.size() + 1, newDataSize);
I think this'll help you.
replace
private List<Student> studentList;
with
private List<Object> list;
also replace
#Override
public int getItemViewType(int position) {
return studentList.get(position) != null ? VIEW_ITEM : VIEW_PROG;
}
with
#Override
public int getItemViewType(int position) {
return list.get(position) instanceof Student ? VIEW_ITEM : VIEW_PROG;
}
for detect end of List you can using
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
// When went to the end of the list, load more posts
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
if (linearLayoutManager.findLastVisibleItemPosition() >= linearLayoutManager.getItemCount() - 1) {
// Grow List
}
}
}
Also for Add Loading Item. add this code in adapter
public void addLoadingView(){
list.add(new Object());
notifyDataSetChanged();
}
1.
Getting first 11 recyclerview Items blank (And showing progress bar
continuously), see below screenshot:
Change loadData method as:
private void loadData() {
new Parser().execute("http://clienturl.com/jsons/mytest.txt");
}
2.
Whereas I was suppose to get first 10 records, and on scroll next 10
records and so on...
Change onPostExecute method of Parser as:
protected void onPostExecute(Boolean result) {
dialog.cancel();
ArrayList< Student > temArray =
new ArrayList< Student >(studentList.subList(0, 10));
mAdapter = new DataAdapter(temArray, mRecyclerView);
// set the adapter object to the Recyclerview
mRecyclerView.setAdapter(mAdapter);
}
And also remove following lines from onCreate method:
mAdapter = new DataAdapter(studentList, mRecyclerView);
// set the adapter object to the Recyclerview
mRecyclerView.setAdapter(mAdapter);
Try changing the
int i = start + 1; i <= end; i++
in the for loop to
int i = start + 1; i < end; i++
The <= validation adds an extra item.
Issue-1: You have created a new instance of mAdapter before setting the LayoutManager for RecyclerView. Thereby the constructor code in the DataAdapter to add ScrollListener is not executed since recyclerView.getLayoutManager() returns null:
if (recyclerView.getLayoutManager() instanceof LinearLayoutManager){
// code to add ScrollListener is never executed
}
Fix: First set the LayoutManager for the Recyclerview and then create the adapter like below:
// use a linear layout manager
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new DataAdapter(temArray, mRecyclerView);
// set the adapter object to the Recyclerview
mRecyclerView.setAdapter(mAdapter);
Issue-2: You have used temArray to create DataAdapter but in onLoadMore() you are using the studentList to add/remove new items, since studentList is not binded with mAdapter your changes doesn't reflect in the UI.
Fix: Declare temArray as a class level variable and use temArray to manipulate the items.
//class variable
private ArrayList<Student> temArray = new ArrayList<Student>();
handler.postDelayed(new Runnable() {
#Override public void run() {
// remove progress item
temArray.remove(temArray.size() - 1);
mAdapter.notifyItemRemoved(temArray.size());
//add items one by one
int start = temArray.size();
int end = start + 10;
if(end<=studentList.size()){
temArray.addAll(studentList.subList(start,end));
}
mAdapter.setLoaded();
}
}, 2000);
for (int i = start + 1; i < end; i++) {
studentList.add(add data here) ;
mAdapter.notifyItemInserted(studentList.size());
}
mAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
#Override
public void onLoadMore() {
//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
public void run() {
// remove progress item
studentList.remove(studentList.size() - 1);
mAdapter.notifyItemRemoved(studentList.size());
//add items one by one
int start = studentList.size();
int end = start + 10;
for (int i = start + 1; i < end; i++) {
// studentList.add();
mAdapter.notifyItemInserted(studentList.size());
}
mAdapter.setLoaded();
//or you can add all at once but do not forget to call mAdapter.notifyDataSetChanged();
}
}, 2000);
}
});
ok so you inserted progress bar, and then you've removed it as well but you never inserted the next student to show...
something like studentList.add();
I hope that solved your problem... good luck..

Categories

Resources