Harshad Pansuriya
Harshad Pansuriya

Reputation: 20910

LinearLayoutManager is not work in RecycleView in Android (Xamarin)

I am creating customAdapter for my RecyclerView for the purpose of Loading More data after the Scroll. So I have to AddScrollListner on RecycleView. But in my public Constructor it not found.

Android code :

public UserAdapter() {
            final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();
            mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);

                    totalItemCount = linearLayoutManager.getItemCount();
                    lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();

                    if (!isLoading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
                        if (mOnLoadMoreListener != null) {
                            mOnLoadMoreListener.onLoadMore();
                        }
                        isLoading = true;
                    }
                }
            });
        }

But When I translate the LinearLayoutManager is not work in Xamarin.

Xamarin Code :

LinearLayoutManager linearLayoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();

in the upper code my RecycleView not found.

UpDate :

I have set this way

mRecyclerView. = FindViewById<RecyclerView>(Resource.Id.inventory_recycleView);
var manager = new LinearLayoutManager(this);
mRecyclerView. SetLayoutManager(manager);
mInventoryAdapter = new InventoryAdapter();
this.mRecyclerView.SetAdapter(mInventoryAdapter);

but then Also my Adapter class that can not use.

class InventoryAdapter : RecyclerView.Adapter
        {

            private const int VIEW_TYPE_ITEM = 0;
            private const int VIEW_TYPE_LOADING = 1;

            private OnLoadMoreListener mOnLoadMoreListener;

            private bool isLoading;
            private int visibleThresold = 5;
            private int lastVisibleItem, totalItemCount;

            public override int ItemCount
            {
                get
                {
                    throw new NotImplementedException();
                }
            }

            public InventoryAdapter()
            {
                var layoutmanger = (LinearLayoutManager) mRecyclerView..GetLayoutManager();

            }
}

Any Help be Appreciated.

Upvotes: 0

Views: 991

Answers (1)

Cheesebaron
Cheesebaron

Reputation: 24460

getLayoutManger(), since it is a method with no arguments is converted into a property LayoutManager on the RecyclerView.

So the code will look like:

var layoutManager = (LinearLayoutManager)mRecyclerView.LayoutManager;

Usually you would set the LayoutManager in your Activity or Fragment like:

var manager = new LinearLayoutManager(Activity); // or just this if inside Activity
mRecyclerview.SetLayoutManager(manager);

It is unclear how you pass in your RecyclerView to the Adapter. It appears as if mRecyclerView is a field somewhere. Normally I would pass it in as an agument in the constructor if I need it in that class:

public MyAdapter(RecyclerView recycler)
{
    mRecyclerView = recycler;
    // do more stuff here...
}

Upvotes: 1

Related Questions