Forum Xamarin.Android

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Recycler View scroll Listener Does not work!

Hello
I implemented RecycleView With web Api DB,
I want load 4 items in the first page of RecyclerView and with scrolling up load more item.
Is there Any guide for implement xamarin android recycleview scroll listener ?
please help me.
thank you.
Sorry for my bad English!


public class RecyclerViewOnScrollListener : Android.Support.V7.Widget.RecyclerView.OnScrollListener
{
public delegate void LoadMoreEventHandler(object sender, EventArgs e);
public event LoadMoreEventHandler LoadMoreEvent;
public bool IsLoading { get; set; }
private LinearLayoutManager LayoutManager;

    public RecyclerViewOnScrollListener(LinearLayoutManager layoutManager)
    {
        LayoutManager = layoutManager;
    }

    public override void OnScrolled(Android.Support.V7.Widget.RecyclerView recyclerView, int dx, int dy)
    {
        base.OnScrolled(recyclerView, dx, dy);

        var visibleItemCount = recyclerView.ChildCount;
        var totalItemCount = recyclerView.GetAdapter().ItemCount;
        var pastVisiblesItems = LayoutManager.FindFirstVisibleItemPosition();

        if ((visibleItemCount + pastVisiblesItems) >= totalItemCount && !IsLoading)
        {
            IsLoading = true;
            LoadMoreEvent(this, null);

            // after it's done, set it back to false
            IsLoading = false;
        }
    }
}

MainActivity:

SetContentView(Resource.Layout.Main);

        PageSize = 4;
        PageIndex = 1;


        mRecyclerView = FindViewById<Android.Support.V7.Widget.RecyclerView>(Resource.Id.recyclerView);
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.SetLayoutManager(mLayoutManager);
        mRecyclerView.SetItemAnimator(new DefaultItemAnimator());

        GetAllProducts();

        var onScrollListener = new RecyclerViewOnScrollListener(mLayoutManager);
        mRecyclerView.AddOnScrollListener(onScrollListener);
        onScrollListener.LoadMoreEvent += (object sender, EventArgs e) =>
        {
            PageIndex++;
            GetAllProducts();

        };

        //#############################Toolbar####################################################
        topToolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
        SetSupportActionBar(topToolbar);
        SupportActionBar.Title = "بازارچه";
        //#############################Toolbar####################################################

        //Create layoutManager
        //####################################################################
        //mLayoutManager = new GridLayoutManager(this, 3);

        //اگر حالت گوشی به صورت معمولی باشد به صورت عمودی لیست میشود در غیر این صورت به صورت افقی
        //mLayoutManager = Resources.Configuration.Orientation == Android.Content.Res.Orientation.Portrait
        //    ? new LinearLayoutManager(this, LinearLayoutManager.Vertical, false)
        //    : new LinearLayoutManager(this, LinearLayoutManager.Horizontal, false);
        //set Adapter For RecyclerView

        //################################################################################
        #endregion



    #endregion
    #region RecyclerViewDivider

    private void RecyclerViewDivider()
    {
        DividerItemDecoration verticalDecoration =
            new DividerItemDecoration(this, DividerItemDecoration.Vertical);
        Drawable verticalDivider = ContextCompat.GetDrawable(this, Resource.Drawable.divider);
        verticalDecoration.SetDrawable(verticalDivider);
        mRecyclerView.AddItemDecoration(verticalDecoration);
    }
    #endregion
    #region GetAllProducts Method
    //###################################################################################################
    private void GetAllProducts()
    {
        mAdapter = new RecyclerViewAdapter(api.GetAllProducts().OrderByDescending(n => n.CreateDateTime).Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList(), this);
        //mAdapter = new RecyclerViewAdapter(api.GetAllProducts().OrderByDescending(n => n.CreateDateTime).ToList(),
        //    this);
        mAdapter.ItemClick += MAdapter_ItemClick;
        //مشخص کردن دیوایدر
        RecyclerViewDivider();

        //ست کردن آداپتر
        mRecyclerView.SetAdapter(mAdapter);

        //از کار انداختن پروگرس دیالوگ
        //mProgressDialog.Dismiss();
    }
    #region RecyclerView Item Click

    //################################################################################
    private void MAdapter_ItemClick(object sender, int position)
    {
        if (intent == null)
        {
            intent = new Intent(this, typeof(ShowProductActivity));
            intent.PutExtra("ProductId", position);
            StartActivity(intent);
        }
    }
    protected override void OnResume()
    {
        intent = null;
        base.OnResume();
    }
    //#############################################################################################################
    #endregion
    //###################################################################################################

    #endregion
    #region Create Option Menu and Item Selected

    //################################################################################
    public override bool OnCreateOptionsMenu(IMenu menu)
    {
        MenuInflater.Inflate(Resource.Menu.main_item, menu);
        return base.OnCreateOptionsMenu(menu);

    }
    public override bool OnOptionsItemSelected(IMenuItem item)
    {
        switch (item.ItemId)
        {
            case Resource.Id.refresh:
                GetAllProducts();
                break;
        }
        return base.OnOptionsItemSelected(item);
    }
    //################################################################################
    #endregion




}

Answers

Sign In or Register to comment.