Ary*_*rya 11 android android-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();
}
Run Code Online (Sandbox Code Playgroud)
Lal*_*ani 24
我会坚持要在0th下面的拉动刷新位置添加项目,
mArrayList.add(position, item);
notifyItemInserted(position);
Run Code Online (Sandbox Code Playgroud)
小智 6
我还需要将项目添加到 recyclerview 的前面(和底部),但我需要将滚动焦点保持在前一个顶部项目上。
所以我将 recyclerview 滚动到上一个顶部项目:
mAdapter.pushFront(items);
mAdapter.notifyItemRangeInserted(0, items.size());
recyclerView.scrollToPosition(items.size() - 1);
Run Code Online (Sandbox Code Playgroud)
有更好的解决方案吗?
小智 6
设置 recyclerview 时添加以下行
recyclerView.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,true));
Run Code Online (Sandbox Code Playgroud)
您可以使用 Collections 反转整个列表:
Collections.reverse(historyitems);
Run Code Online (Sandbox Code Playgroud)
尝试使用适配器:
adapter.insert(yourItem, 0);
Run Code Online (Sandbox Code Playgroud)
尝试使用列表:
list.add(0,listItem);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22035 次 |
| 最近记录: |