本文介绍了交错式Gridview(Recycleview),在NestedScrollView内加载更多功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的交错gridview中实现更多加载功能.我已经尝试了一些代码行,例如使用addOnScrollListener,但是当我排在列表底部时却没有调用.
请找到我尝试实现更多功能但未获得预期结果的代码.

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;
                        }
                    }
                });

在我上面的代码中, System.out 没有得到打印...

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

我为setOnScrollChangeListener的分层网格视图尝试了另一个侦听器,但它也无法正常工作

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 ");

            }
        });

此侦听器出现相同的问题,而无法打印我的 System.out

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

关于我在onBindViewHolder方法中的adapter类中尝试过的其他事情,是

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 ,请在下面检查我的适配器代码



@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);

        }


    }

}

推荐答案

实际上我使用的是NestedScrollView,它是我的Staggered recycleview的父级view.因此,addOnScrollListener侦听器和setOnScrollChangeListener无法正常工作在里面..
我在NestedScrollView中使用了setOnScrollChangeListener,它工作正常.检查我下面的解决方案:-

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");
        }
   }
});

这篇关于交错式Gridview(Recycleview),在NestedScrollView内加载更多功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 03:31