首页 文章

将项目添加到recyclerview的顶部

提问于
浏览
7

我的活动中有一个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();
}

6 回答

  • 2

    我会坚持要在 0th 位置添加项目,该项目来自拉动刷新,如下所示,

    mArrayList.add(position, item);
    notifyItemInserted(position);
    
  • -1

    我还需要在recyclerview(和底部)的前面添加项目,但我需要将滚动重点放在前一个顶部项目上 .

    所以我将recyclerview滚动到上一个顶级项目:

    mAdapter.pushFront(items);
    mAdapter.notifyItemRangeInserted(0, items.size());
    recyclerView.scrollToPosition(items.size() - 1);
    

    有更好的解决方案吗?

  • 0

    Recycler视图与项目的排序无关 . 从上面的代码中,您可以刷新内容并只显示从服务器获取的内容 . 可能是服务器返回的项目按其显示的顺序排列 . 因此,请检查服务器的订单 .

  • 0

    使用 list.add(0,items); 它会将新项目添加到recyclerview的顶部

  • 20

    设置recyclerview时添加以下行

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

    您可以使用集合来反转整个列表:

    Collections.reverse(historyitems);
    

    尝试使用适配器:

    adapter.insert(yourItem, 0);
    

    尝试使用List:

    list.add(0,listItem);
    

相关问题