0

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

        }


    }

}
Ravindra Kushwaha
  • 7,846
  • 14
  • 53
  • 103
  • you can use this link : http://stackoverflow.com/questions/8664935/android-loading-data-like-facebook-and-twitter – Amit Ranjan Jan 30 '17 at 06:56
  • @AmitRanjan ..I have tried above link, it is also not working in case of stragged gridview – Ravindra Kushwaha Jan 30 '17 at 07:10
  • can you see this code : http://stackoverflow.com/questions/16851934/android-onscrolllistener-for-staggered-grid-view – Amit Ranjan Jan 30 '17 at 07:15
  • Still not working :( – Ravindra Kushwaha Jan 30 '17 at 10:05
  • why don't you try `getCount()-1 == position`. If I remember correctly getcount returns total number of items in the `RecyclerView` `position` would never reach that count. Let me know if it worked. I will update my original answer. – Abbas Jan 31 '17 at 04:48
  • ok @Abbas i am trying.. I have used such logic in past...but did not work..Let try one more time..I will inform soon.. – Ravindra Kushwaha Jan 31 '17 at 05:36
  • @Abbas ..It is also not woking dost :( ...What i want that when i come to the bottom of the list than i get want an event that i have reached at the bottom...Yet now it is not working at the bottom of the list – Ravindra Kushwaha Jan 31 '17 at 06:16
  • @RavindraKushwaha that is strange I have used this technique for so long, it is still working in one of the apps I have on PlayStore. By the way what is `getCount()` are you sure it returns the correct number of items. In the Documentation they only have [getItemCount()](https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#getItemCount()). – Abbas Jan 31 '17 at 06:30
  • i have used code like === if(getItemCount()==position) inside the onBindViewHolder method of my adapter.... it is getting print first time as the adapter set...What i want that when i will reach at the bottom of the list than i get inform about it. @Abbas – Ravindra Kushwaha Jan 31 '17 at 06:59
  • @RavindraKushwaha I just created my own little app just to see if it works or not: it works. I only hit the condition `(getItemCount() - 1 == position)` whenever scrolled to bottom. You must be doing something wrong. Post your Adapter's code. – Abbas Jan 31 '17 at 09:41
  • @RavindraKushwaha change to `if(getItemCount() - 1==position)`. – Abbas Jan 31 '17 at 10:58
  • @Abbas ...Thanks for your comments .. I have solved the problem, check my solution below – Ravindra Kushwaha Jan 31 '17 at 12:00

1 Answers1

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");
        }
   }
});
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Ravindra Kushwaha
  • 7,846
  • 14
  • 53
  • 103