我的活动中有一个Recyclerview.当我下拉它将加载新项目回收视图.
现在我需要实现pull来将概念刷新到我的recyclerview.我做到了.但是当我调用pull来刷新时,我正在获取新项目并添加到回收视图底部.我需要在循环视图的顶部添加新项目.如何将新加载的项目添加到回收站视图的顶部位置.
现在我需要实现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();
}
解决方法
我会坚持要在第0个位置添加项目,这个位置来自拉动刷新,如下所示,
mArrayList.add(position,item); notifyItemInserted(position);