StaggeredView android无限滚动问题

时间:2015-11-01 17:38:41

标签: android android-studio android-recyclerview

我'我试图用交错的视图实现无限滚动。当我开始时,我使用LinearLayoutManager和此https://gist.github.com/ssinss/e06f12ef66c51252563e进行无限滚动。现在我改为StaggeredGridLayoutManager,但我找不到实现无限滚动的方法。这是我到目前为止所尝试的:

mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
    //mRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity()).color(Color.BLACK).build());


    final StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);

    mRecyclerView.setLayoutManager(staggeredGridLayoutManager);

    updateList("aww");

    mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

            visibleItemCount = staggeredGridLayoutManager.getChildCount();
            totalItemCount = staggeredGridLayoutManager.getItemCount();
            int[] firstVisibleItems = null;
            firstVisibleItems = staggeredGridLayoutManager.findFirstVisibleItemPositions(firstVisibleItems);
            if (firstVisibleItems != null && firstVisibleItems.length > 0) {
                pastVisibleItems = firstVisibleItems[0];
            }

            if (loading) {
                if ((visibleItemCount + pastVisibleItems) >= totalItemCount) {
                    loading = false;
                    Log.d("tag", "LOAD NEXT ITEM");

                    loadMore("aww");
                }
            }
        }
    });

调用loadMore函数,但这些项目不会添加到屏幕上。

loadMore:

public void loadMore(String subreddit) {

        counter = counter + 25;
        count = String.valueOf(counter);
        subreddit = jsonSubreddit;

        Log.d(TAG, after_id);

        subreddit = subredditUrl + subreddit + jsonEnd + "?after" + after_id + "=" + qCount + count + after + "=" + after_id;


        Log.d(TAG, subreddit);
        adapter = new MyRecyclerAdapter(getActivity(), listItemsList);
        mRecyclerView.setAdapter(adapter);

        RequestQueue queue = Volley.newRequestQueue(getActivity());

        showPD();

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, subreddit, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Log.d(TAG, response.toString());

                hidePD();

                try {
                    JSONObject data = response.getJSONObject("data");
                    after_id = data.getString("after");
                    JSONArray children = data.getJSONArray("children");

                    for (int i = 0; i < children.length(); i++) {

                        JSONObject post = children.getJSONObject(i).getJSONObject("data");
                        ListItems item = new ListItems();
                        item.setTitle(post.getString("title"));
                        item.setThumbnail(post.getString("thumbnail"));
                        item.setUrl(post.getString("url"));
                        item.setSubreddit(post.getString("subreddit"));
                        item.setAuthor(post.getString("author"));

                        jsonSubreddit = post.getString("subreddit");

                        listItemsList.add(item);
                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                }
                adapter.notifyDataSetChanged();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {


                VolleyLog.d(TAG, "Error" + error.getMessage());

                hidePD();
            }
        });

        queue.add(jsonObjectRequest);
    }

问题是我一启动程序就会执行loadMore()函数。 visibleItemCount的起始值已经= to totalItemCount。

0 个答案:

没有答案