0

だから私はRecyclerViewいつものようにInfragmentタブを持っていて、 LinearLayoutManager Endless scrollにも使いたいのですが、問題は以下のようにメソッドScrollListenerで変数を作る場合ですLinearLayoutManagerOnCreate

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

上部のスクリプトは正常に実行されていますが、タブをシフトしたり、フラグメントを移動するとエラーが表示されますが、解決策は置き換えですmRecyclerView.SetLayoutManager(mLayoutManager);

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

しかし、エンドレススクロールは実行されません。私の質問は、 LinearLayoutどちらを使用する必要があるかです

var scrollListener = new scroll(mLayoutManager);

効かないから…??

私の完全なフラグメントコード: `

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



    }

}

}`

4

1 に答える 1

0

を呼び出すと 、LinearLayoutManagerからを取得できます。RecyclerViewvar layoutManager = (LinearLayoutManager)recyclerView.GetLayoutManager();

ScrollListenerしたがって、 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));
        }



    }

}
于 2016-12-28T14:59:11.923 に答える