在片段中滞后于recyclerview

时间:2016-03-31 22:12:08

标签: android android-recyclerview

我正在尝试使用标签布局创建应用,并且在其中一个标签中,我想要创建2个recyclerviews,但一次只会在屏幕上显示1个。在应用程序启动时从Web下载1个recyclerview的数据,在屏幕上单击按钮后下载第二个数据。下载第二个recyclerView的数据时,会显示recyclerView,但这是滞后的。当我隐藏第二个recyclerView(一个下载的按钮点击按钮),然后再次显示它而没有下载数据它正常工作 - 没有任何滞后。
这个活动中有很多代码,所以我只添加几行,如果需要更多,我会添加更多。
第二个RecyclerView的适配器:

private static class CommentsAdapter extends RecyclerView.Adapter<CommentsAdapter.ViewHolder> {
        public CommentsAdapter() {}

        @Override
        public void onAttachedToRecyclerView(RecyclerView recyclerView) {
            super.onAttachedToRecyclerView(recyclerView);
        }

        public static class ViewHolder extends RecyclerView.ViewHolder {

            public TextView commentTitle;
            public TextView commentText;
            public TextView deleteComment;
            public TextView commentRating;
            public ImageView upVote;
            public ImageView downVote;

            public ViewHolder(View view) {
                super(view);
                ...
            }
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.comment, parent, false);
            ViewHolder vh = new ViewHolder(v);
            return vh;
        }

        @Override
        public void onBindViewHolder(final ViewHolder holder, final int position) {
            ... (a lot of code, but it's not the reason why recycler view is laggy)
            });
        }

        @Override
        public int getItemCount() {
            return titles.length;
        }
    }

编辑评级和删除评论我正在使用凌空和类似这样的结构不会落后于其他任何东西,所以我想这不是问题。
下载评论的代码:

private void ReadComments() {

        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Please wait, loading comments...");
        pDialog.setCancelable(true);
        pDialog.setIndeterminate(false);
        pDialog.show();
        Map<String, String> params = new Hashtable<String, String>();
        params.put("tag", tag + "");
        CustomJsonRequest request = new CustomJsonRequest(Request.Method.POST, url_get_comments, params,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d(TAG, response.toString());
                        try {
                            jsonParser = response.toString();
                            Log.d("Comments ", response.toString());
                            news = response.getJSONArray("comments");
                            if (!response.toString().contains("No products found")) {
                                titles = new String[news.length()];
                                descs = new String[news.length()];
                                ratings = new int[news.length()];
                                pids = new int[news.length()];
                                users = new String[news.length()];

                                for (int i = 0; i < news.length(); i++) {
                                    JSONObject c = news.getJSONObject(i);
                                    titles[i] = c.getString("title");
                                    descs[i] = c.getString("desc");
                                    ratings[i] = c.getInt("rating");
                                    pids[i] = c.getInt("pid");
                                    users[i] = c.getString("user");
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        if (titles != null) {
                            if (titles.length == 0) {
                                noComments.setVisibility(View.VISIBLE);
                            } else {
                                CommentsAdapter commentsAdapter = new CommentsAdapter();
                                commentsView.setAdapter(commentsAdapter);
                            }
                        } else {
                            noComments.setVisibility(View.VISIBLE);
                        }
                        pDialog.hide();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                pDialog.hide();
            }
        });
        request.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        AppController.getInstance().getRequestQueue().add(request);
    }

这就是使用这个rec​​yclerView所做的一切。
您可以在我的GitHub上找到整个代码:https://github.com/LisSkis/HowItTastes
使用RecyclerViews的文件是:HomeContent.java,recycler_view.xml

1 个答案:

答案 0 :(得分:2)

将此设置为您的recyclerView:

recyclerView.setNestedScrollingEnabled(false);