how to get LinearLayoutManager from RecyclerView..?

时间秒杀一切 提交于 2020-01-07 02:26:27

问题


so I had my RecyclerView In fragment tab, as usual using LinearLayoutManager and I also want to use for Endless scroll ScrollListener,the problem is if I make a variable LinearLayoutManager in OnCreate method as below

    public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            mLayoutManager = new LinearLayoutManager(Context);
        }
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            View row = inflater.Inflate(Resource.Layout.Fragment3, container, false);
            RecyclerView mRecyclerView = row.FindViewById<RecyclerView>(Resource.Id.recyclerView);
            mRecyclerView.SetLayoutManager(mLayoutManager);
            var scrollListener = new scroll(mLayoutManager);
            mRecyclerView.AddOnScrollListener(scrollListener);
            mRecyclerView.SetAdapter(mAdapter);
        }

scrip at the top running properly but if I shift the tab or moving fragment will appear error, but I find a solution is replace mRecyclerView.SetLayoutManager(mLayoutManager);

to : mRecyclerView.SetLayoutManager(new LinearLayoutManager(Context));

but endless scroll does not run, my question is LinearLayout which one I should use in

var scrollListener = new scroll(mLayoutManager);

because it does not work...??

My full fragment code : `

        downloadProcess.DoWork += DownloadProcess_DoWork;
        downloadProcess.RunWorkerCompleted += DownloadProcess_RunWorkerCompleted;
        downloadProcess.RunWorkerAsync();

        mRootItem = new List<RowItems>();
        mAdapter = new RecyclerAdapter(mRootItem, Context, mRecyclerView);





    }

    private void DownloadProcess_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {

        mAdapter.NotifyDataSetChanged();
        progress.Dismiss();
        Console.WriteLine("3333333333333");

    }

    private void DownloadProcess_DoWork(object sender, DoWorkEventArgs e)
    {

        urlT = net.DownloadString("http://articee.livescore31.com/api/json_load10.php");
        jsonRootNews = JsonConvert.DeserializeObject<List<JsonRootNews>>(urlT);
        for (int i = 0; i < jsonRootNews[0].item.Count; i++)
        {
            mRootItem.Add(new RowItems() { Name = jsonRootNews[0].item[i].judul, Image = jsonRootNews[0].item[i].img_Url, ShortTitle = jsonRootNews[0].item[i].short_title });

        }
        //SetUpRecyclerView(e.Argument as RecyclerView);
        Console.WriteLine("2222222222222222");
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View row = inflater.Inflate(Resource.Layout.Fragment3, container, false);
        RecyclerView mRecyclerView = row.FindViewById<RecyclerView>(Resource.Id.recyclerView);



        mLayoutManager = new LinearLayoutManager(Context);



        mRecyclerView.SetLayoutManager(new LinearLayoutManager(Context));
        var scrollListener = new scroll(new LinearLayoutManager(Context));
        mRecyclerView.AddOnScrollListener(scrollListener);
        mRecyclerView.SetAdapter(mAdapter);

        scrollListener.mDoText += () =>
        {
            if (isReload)
            {
                BackgroundWorker doitSomeThing = new BackgroundWorker();
                doitSomeThing.DoWork += DoitSomeThing_DoWork;
                doitSomeThing.RunWorkerCompleted += DoitSomeThing_RunWorkerCompleted;
                doitSomeThing.RunWorkerAsync();
                isReload = false;
            }


        };



        return row;

    }



    private void  SetUpRecyclerView(RecyclerView mRecyclerView)
    {
        //LinearLayoutManager mLinear = new LinearLayoutManager(Context);
        //mLayoutManager = new LinearLayoutManager(Context);
        //mLayoutManager.Orientation = LinearLayoutManager.Horizontal;
        //var ss = new LinearLayoutManager(Context);
        i++;

        Console.WriteLine("ON VIEW ------------------"+i);
        mRootItem = new List<RowItems>();
        mAdapter = new RecyclerAdapter(mRootItem, Context, mRecyclerView);
        var scrollListener = new scroll(mLayoutManager);
        mRecyclerView.SetLayoutManager(new LinearLayoutManager(Context));
        mRecyclerView.AddOnScrollListener(scrollListener);
        mRecyclerView.SetAdapter(mAdapter);
        mAdapter.NotifyDataSetChanged();



        scrollListener.mDoText += () =>
        {
            if (isReload)
            {
                BackgroundWorker doitSomeThing = new BackgroundWorker();
                doitSomeThing.DoWork += DoitSomeThing_DoWork;
                doitSomeThing.RunWorkerCompleted += DoitSomeThing_RunWorkerCompleted;
                doitSomeThing.RunWorkerAsync();
                isReload = false;
            }


        };
    }
    private void DoitSomeThing_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        isReload = true;
    }
    private void DoitSomeThing_DoWork(object sender, DoWorkEventArgs e)
    {
        Random r = new Random();
        int random = r.Next(0, 1000);
        urlT = net.DownloadString("http://articee.livescore31.com/api/json_load5.php?limit=" + loadNow + "&id=" + random);
        jsonRootNews = JsonConvert.DeserializeObject<List<JsonRootNews>>(urlT);
        for (int i = 0; i < jsonRootNews[0].item.Count; i++)
        {
            mRootItem.Add(new RowItems() { Name = jsonRootNews[0].item[i].judul, Image = jsonRootNews[0].item[i].img_Url, ShortTitle = jsonRootNews[0].item[i].short_title });
            Console.WriteLine("COMPLETE");
        }
        mAdapter.NotifyDataSetChanged();
        loadNow += 2;
        Console.WriteLine("-----------------------------" + loadNow.ToString());
    }

}
public class RecyclerAdapter : RecyclerView.Adapter
{
    public List<RowItems> mItems;
    public Context mContext;
    public RecyclerView mRecyclerView;
    public RecyclerAdapter(List<RowItems> items, Context context, RecyclerView recyclerView)
    {
        mItems = items;
        mContext = context;
        mRecyclerView = recyclerView;
    }

    public class MyView : RecyclerView.ViewHolder
    {
        public View mMainView { get; set; }
        public TextView mName { get; set; }
        public ImageView mImage { get; set; }
        public TextView mShortTitle { get; set; }
        public Button button;
        public MyView(View view) : base(view)
        {
            mMainView = view;
        }
    }

    public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
    {
        View row = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.row_news, parent, false);
        //mRecyclerView = (RecyclerView)row.FindViewById(Resource.Id.btnReadMore);

        TextView txtName = row.FindViewById<TextView>(Resource.Id.txtJudul);
        ImageView ImageView = row.FindViewById<ImageView>(Resource.Id.imgThumbnail);
        TextView txtShortMessage = row.FindViewById<TextView>(Resource.Id.txtShortTitle);
        Button mButton1 = row.FindViewById<Button>(Resource.Id.btnReadMore);

        MyView view = new MyView(row) { mName = txtName, mImage = ImageView, mShortTitle = txtShortMessage, button = mButton1 };
        return view;
    }
    public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
        MyView myHolder = holder as MyView;
        myHolder.mMainView.Click += MMainView_Click;


        myHolder.button.Click += (sender, e) => { MyHandler(sender, e, position); };




        myHolder.mName.Text = mItems[position].Name;
        Picasso.With(mContext).Load(mItems[position].Image).Into(myHolder.mImage);
        myHolder.mShortTitle.Text = mItems[position].ShortTitle;
    }

    //BUTTON READ MORE ==========================================================================================================
    void MyHandler(object sender, EventArgs e, int pos)
    {

        try
        {
            Console.WriteLine(mItems[pos].Name);
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message);
        }


    }
    //=============================================================================================================================


    private void MMainView_Click(object sender, EventArgs e)
    {
        try
        {
            int position = mRecyclerView.GetChildPosition((View)sender);
            //int indexPosition = (mEmails.Count - 1) - position;
            //Console.WriteLine(mEmails[indexPosition].Name);
            Console.WriteLine(mItems[2].Name);
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message);
            Console.WriteLine(mItems.Count);
        }

    }

    public override int ItemCount
    {
        get
        {
            return mItems.Count;
        }
    }


}
public class scroll : RecyclerView.OnScrollListener
{
    public delegate void DoSomething();
    public DoSomething mDoText;
    public LinearLayoutManager mLayoutManager;

    public scroll(LinearLayoutManager layoutManager)
    {
        mLayoutManager = layoutManager;
    }
    public override void OnScrolled(RecyclerView recyclerView, int dx, int dy)
    {
        base.OnScrolled(recyclerView, dx, dy);

        var visibleItemCount = recyclerView.ChildCount;
        var totalItemCount = recyclerView.GetAdapter().ItemCount;
        var latVisible = mLayoutManager.FindLastCompletelyVisibleItemPosition();
        var pasVisibleItem = mLayoutManager.FindFirstVisibleItemPosition();
        if ((visibleItemCount + pasVisibleItem) >= (totalItemCount))
        {

            mDoText.Invoke();

        }
        else
        {


            Console.WriteLine("visibleItemCount + pasVisibleItem =  " + (visibleItemCount + pasVisibleItem));
        }



    }

}

}`


回答1:


You can get the LinearLayoutManager from the RecyclerView by calling var layoutManager = (LinearLayoutManager)recyclerView.GetLayoutManager();

Therefore, you could use this in your ScrollListener instead of passing the created LinearLayoutManager:

public class scroll : RecyclerView.OnScrollListener
{
    public delegate void DoSomething();
    public DoSomething mDoText;

    public scroll()
    {
    }
    public override void OnScrolled(RecyclerView recyclerView, int dx, int dy)
    {
        base.OnScrolled(recyclerView, dx, dy);

        var visibleItemCount = recyclerView.ChildCount;
        var totalItemCount = recyclerView.GetAdapter().ItemCount;
        // GET LAYOUT MANAGER
        var mLayoutManager = (LinearLayoutManager)recyclerView.GetLayoutManager();
        var latVisible = mLayoutManager.FindLastCompletelyVisibleItemPosition();
        var pasVisibleItem = mLayoutManager.FindFirstVisibleItemPosition();
        if ((visibleItemCount + pasVisibleItem) >= (totalItemCount))
        {

            mDoText.Invoke();

        }
        else
        {


            Console.WriteLine("visibleItemCount + pasVisibleItem =  " + (visibleItemCount + pasVisibleItem));
        }



    }

}


来源:https://stackoverflow.com/questions/41354809/how-to-get-linearlayoutmanager-from-recyclerview

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