Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Items to top of recyclerview

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();
}
like image 428
Arya Avatar asked Aug 14 '15 06:08

Arya


3 Answers

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); 
like image 76
Lalit Poptani Avatar answered Oct 21 '22 09:10

Lalit Poptani


Add following line when you set your recyclerview

recyclerView.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,true));
like image 25
CleverChuk Avatar answered Oct 21 '22 09:10

CleverChuk


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?

like image 31
kejmil01 Avatar answered Oct 21 '22 09:10

kejmil01