Reputation: 1747
I have a Recyclerview in my activity. when I pull down it will load new items to recycle view. Now I need to implement pull to refresh the concept to my recyclerview. I have done that. But when I call pull to refresh I am getting new items and added to recycle view bottom. I required to add new items to top of my recycle view. How can I add new loaded items to the top position of recycler view.
public void refreshing() throws IllegalStateException{
new AsyncTask<String, Void, Void>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected Void doInBackground(String... arg0) {
final List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("id", sPreferences.getString("ID", "")));
final HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 30000);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
HttpPost httpPost = new HttpPost(Config.requestpatienthistory);
httpPost.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
try {
httpPost.setEntity(new UrlEncodedFormEntity(list));
} catch (IOException e) {
e.printStackTrace();
}
try {
HttpResponse response = httpClient.execute(httpPost);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String json = reader.readLine();
JSONObject jsonObj = new JSONObject(json);
if (jsonObj.has("control")) {
JSONArray feedArray = jsonObj.getJSONArray("control");
for (int i = 0; i < feedArray.length(); i++) {
JSONObject feedObj = (JSONObject) feedArray.get(i);
final Historyitem item = new Historyitem();
if (feedObj.has("Reported_Time")) {
item.setReported_Time(feedObj.getString("Reported_Time"));
}
historyitems.add(item);
}
} else {
System.out.println("" + "no patients");
}
} catch (SocketException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
progressBar.setVisibility(View.GONE);
historyadapter = new HistoryRecycleListAdapter(getActivity(), getActivity(), historyitems);
hisrecyclerview.setAdapter(historyadapter);
}
}.execute();
}
Upvotes: 17
Views: 38326
Reputation: 357
When you pull to refresh your data then you can simply reverse your list like this:
Collections.reverse(yourModelList);
notifyDataSetChanged();
Upvotes: 2
Reputation: 625
Do it in xml you don't need LinearLayoutManager
code anymore
<android.support.v7.widget.RecyclerView
android:id="@+id/recordItemList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:clipToPadding="false"
android:scrollbars="none"
app:layoutManager="LinearLayoutManager"
app:stackFromEnd="true"
app:reverseLayout="true"/>
Upvotes: 3
Reputation: 415
For perfect control position system, add each item's position in arraylist. For new item position will be 0. Use Comparator and sort items by using Collection Class.
Collections.sort(arrayOfPosts, new SortPostByPosition());
listAdapter.notifyDataSetChanged();
Here is SortPostByPosition class that compare position of each item.
class SortPostByPosition implements Comparator<TimelineListData> {
public int compare(TimelineListData a, TimelineListData b) {
return Integer.compare(a.position, b.position);
// you can compare by your own complex formula too
}
}
Upvotes: 0
Reputation: 399
in Recyclerview you also set your list order. you just set your adapter to reverse true or false.
RecyclerView.LayoutManager layoutManager=newLinearLayoutManager(this,LinearLayoutManager.VERTICAL,true);
recyclerView.setLayoutManager(layoutManager);
after set layout manager to your Recyclerview Adapter. And if you want to perform delete some item view on your recyclerview also you need to add
layoutManager.setStackFromEnd(true);
Upvotes: 3
Reputation: 141
Add following line when you set your recyclerview
recyclerView.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,true));
Upvotes: 8
Reputation: 1898
use list.add(0,items);
it will add new item to top of recyclerview
Upvotes: 3
Reputation: 95
I also need to add items to the front of recyclerview(and to bottom), but i need to keep scroll focused at the previous top item.
So i'm scrolling recyclerview to previous top item:
mAdapter.pushFront(items);
mAdapter.notifyItemRangeInserted(0, items.size());
recyclerView.scrollToPosition(items.size() - 1);
Is there a better solution?
Upvotes: 6
Reputation: 9267
You can reverse your whole list using Collections :
Collections.reverse(historyitems);
Try using adapter :
adapter.insert(yourItem, 0);
Try using List :
list.add(0,listItem);
Upvotes: 3
Reputation: 67286
I would insist you to add item at 0th
position which is coming from pull to refresh as below,
mArrayList.add(position, item);
notifyItemInserted(position);
Upvotes: 30
Reputation: 943
Recycler view has nothing to with ordering of items. From the above code you are refreshing contents and simply displaying what you are getting from server. May be the items returned from server are in the order they get displayed. So check the order from server what you are getting.
Upvotes: 0