Staggered Gridview (Recycleview) with load more functionality inside the NestedScrollView

半城伤御伤魂 提交于 2019-12-17 19:56:46

问题


I want to implement the load more functionality inside my Staggered gridview. I have tried some lines of code for it like using addOnScrollListener but did not call when i come to the bottom the list.
Please find my code which i have tried to implement the load more functionality but not getting the expected result.

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

                        totalItemCount = staggeredGridLayoutManager.getItemCount();
                        lastVisibleItem = staggeredGridLayoutManager
                                .findLastCompletelyVisibleItemPositions(null)[0];

                        if (!loading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
                            // End has been reached
                            // Do something
                            System.out.println("I amm here dd ");

                            loading = true;
                        }
                    }
                });

In my above code, System.out is not getting print...

I have tried another listener for the stragged gridview that is setOnScrollChangeListener, but it is also not working

MY_STRAGGED_RECYCLIVIEW.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                System.out.println("I amm here fffffffffff ");

            }
        });

Same problem arises with this listener , not getting to print my System.out

On more thing which i have tried in my adapter class inside the onBindViewHolder method that is

if(getCount()==position)
{
////for getting the last item of the recycleview
}

Above code is also not working..Please help me to short out from this problem..Thanks :)



@Abbas please check my adapter code below

public class StraggredView extends RecyclerView.Adapter<StraggredView.ViewHolder> {

    private List<Content> mDataSet;
    private Context ctx;

    public StraggredView(Context context, List<Content> arrList) {
        ctx = context;
        mDataSet = arrList;

    }

    @Override
    public StraggredView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_grid, parent, false);
        return new ViewHolder(view);

    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {


        if(getItemCount()==position)
        {
            //// I AM NOT GETTING IT AT THE BOTTOM... IT INVOKED AS THE ADAPTER IS CALLED FIRST TIME...LOOK AT IT
        }

        if (mDataSet.get(position).getContentImage() == null || mDataSet.get(position).getContentImage().isEmpty()) {

            Glide.with(ctx).load(R.drawable.no_content)
                    // .override(screenWidth / 2, Utils.dpToPx(height))
                    .into(holder.imgContent);
        } else {
            Glide.with(ctx).load(mDataSet.get(position).getContentImage())
                    //  .override(screenWidth / 2, Utils.dpToPx(height))
                    //    .centerCrop()
                    //.transform(new CircleTransform())
                    .into(holder.imgContent);
        }

        holder.imgContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mDataSet.get(position).getContentImage() != null
                        || !mDataSet.get(position).getContentImage().isEmpty()) {
                    FragmentActivity activity = (FragmentActivity) (ctx);
                    FragmentManager fm = activity.getSupportFragmentManager();
                    FullScreenFragment dialog = FullScreenFragment.newInstance(
                            mDataSet.get(position).getContentImage());
                    dialog.show(fm, "dialog");
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return this.mDataSet.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView imgContent;

        public ViewHolder(View itemView) {
            super(itemView);

            imgContent = (ImageView) itemView.findViewById(R.id.imgContent);

        }


    }

}

回答1:


Actually i was using the NestedScrollView which was the parent view of my Staggered recycleview .Therefore addOnScrollListener listener and setOnScrollChangeListener was not working in it..
I have used setOnScrollChangeListener in a NestedScrollView and it worked fine. Check my below solution for it:-

NestedScrollView myNestedScroll= (NestedScrollView) findViewById(R.id.myNestedScroll);

myNestedScroll.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
    @Override
    public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {

        if (scrollY > oldScrollY) {
            Log.i(TAG, "Scroll DOWN");
        }
        if (scrollY < oldScrollY) {
            Log.i(TAG, "Scroll UP");
        }

        if (scrollY == 0) {
            Log.i(TAG, "TOP SCROLL");
        }

        if (scrollY == (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight())) {
            Log.i(TAG, "BOTTOM SCROLL");
        }
   }
});


来源:https://stackoverflow.com/questions/41930139/staggered-gridview-recycleview-with-load-more-functionality-inside-the-nesteds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!