[英]Add Items to top of recyclerview
我的活動中有一個 Recyclerview。 當我下拉時,它會加載新項目以回收視圖。 現在我需要實現 pull 來刷新我的 recyclerview 的概念。 我已經這樣做了。 但是當我調用 pull 刷新時,我得到了新項目並添加到回收視圖底部。 我需要在回收視圖的頂部添加新項目。 如何將新加載的項目添加到回收站視圖的頂部位置。
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();
}
我會堅持你在0th
位置添加項目,它來自下拉刷新,如下所示,
mArrayList.add(position, item);
notifyItemInserted(position);
我還需要將項目添加到 recyclerview 的前面(和底部),但我需要將滾動焦點保持在前一個頂部項目上。
所以我將 recyclerview 滾動到上一個頂部項目:
mAdapter.pushFront(items);
mAdapter.notifyItemRangeInserted(0, items.size());
recyclerView.scrollToPosition(items.size() - 1);
有更好的解決方案嗎?
設置 recyclerview 時添加以下行
recyclerView.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,true));
您可以使用 Collections 反轉整個列表:
Collections.reverse(historyitems);
嘗試使用適配器:
adapter.insert(yourItem, 0);
嘗試使用列表:
list.add(0,listItem);
在 Recyclerview 中,您還可以設置列表順序。 您只需將適配器設置為反轉 true 或 false。
RecyclerView.LayoutManager layoutManager=newLinearLayoutManager(this,LinearLayoutManager.VERTICAL,true);
recyclerView.setLayoutManager(layoutManager);
將布局管理器設置為 Recyclerview Adapter 后。 如果你想在你的 recyclerview 上執行刪除一些項目視圖,你還需要添加
layoutManager.setStackFromEnd(true);
使用list.add(0,items);
它將在 recyclerview 頂部添加新項目
在 xml 中執行此操作,您不再需要LinearLayoutManager
代碼
<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"/>
回收者視圖與物品的排序無關。 從上面的代碼中,您正在刷新內容並簡單地顯示您從服務器獲得的內容。 可能是從服務器返回的項目按它們顯示的順序排列。 因此,請檢查您從服務器獲得的訂單。
為了完美的控制位置系統,在arraylist中添加每個項目的位置。 新項目的位置將為 0。使用 Comparator 並使用 Collection Class 對項目進行排序。
Collections.sort(arrayOfPosts, new SortPostByPosition());
listAdapter.notifyDataSetChanged();
這是比較每個項目位置的SortPostByPosition類。
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
}
}
當您拉動刷新數據時,您可以簡單地反轉您的列表,如下所示:
Collections.reverse(yourModelList);
notifyDataSetChanged();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.