android – 将项目添加到recyclerview的顶部

前端之家收集整理的这篇文章主要介绍了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();
}

解决方法

我会坚持要在第0个位置添加项目,这个位置来自拉动刷新,如下所示,
mArrayList.add(position,item);
notifyItemInserted(position);

猜你在找的Android相关文章