Add Items to top of recyclerview

家住魔仙堡 提交于 2019-12-18 21:18:42

问题


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();
}

回答1:


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); 



回答2:


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?




回答3:


use list.add(0,items); it will add new item to top of recyclerview




回答4:


Add following line when you set your recyclerview

recyclerView.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,true));



回答5:


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);



回答6:


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.




回答7:


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);


来源:https://stackoverflow.com/questions/32003402/add-items-to-top-of-recyclerview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!