Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 将项目添加到recyclerview的顶部大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我的活动中有一个Recyclerview.当我下拉它将加载新项目回收视图. @H_616_2@现在我需要实现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个位置添加项目,这个位置来自拉动刷新,如下所示,@H_627_3@mArrayList.add(position,item); notifyItemInserted(position);

大佬总结

以上是大佬教程为你收集整理的android – 将项目添加到recyclerview的顶部全部内容,希望文章能够帮你解决android – 将项目添加到recyclerview的顶部所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。