如何将Recyclerview设置为仅显示4个图块

时间:2017-11-14 07:34:03

标签: android android-layout android-recyclerview xamarin.android

所以,我已经实现了一个有15个项目的RecyclerView,现在我要做的是当我点击一个按钮时将RecyclerView限制为4个项目,反之亦然,我不知道该怎么做,这就是什么我已经完成了,任何输入都会有所帮助

floatingButton.Click += delegate {
            if (clicked)
            {
                Toast.MakeText(this, "Has been clicked before", ToastLength.Short).Show();
                recyclerview_layoutmanger = new GridLayoutManager(this,, 2);
                myRecyclerview.SetLayoutManager(recyclerview_layoutmanger);
                recyclerview_adapter.NotifyDataSetChanged();
                clicked = false;
            }
            else {
                recyclerview_layoutmanger = new LinearLayoutManager(this, LinearLayoutManager.Vertical, false);
                Toast.MakeText(this, "Has not been clicked before", ToastLength.Short).Show();
                myRecyclerview.SetLayoutManager(recyclerview_layoutmanger);
                recyclerview_adapter.NotifyDataSetChanged();
                clicked = true;
            }
        };

适配器类

  public class RecyclerAdapter : RecyclerView.Adapter
    {
        public event EventHandler<int> ItemClick;

        private readonly List<MessageList> messages;

        public RecyclerAdapter(List<MessageList> messages)
        {
            this.messages = messages;
            NotifyDataSetChanged();
        }

        public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
        {
            var movieViewHolder = (MessageViewHolder)holder;
            movieViewHolder.messageView.Text = messages[position].Message;
            movieViewHolder.dateView.Text = messages[position].messageDate;
            // movieViewHolder.MovieImageView.SetImageResource(Resource.Drawable.starwarslogo);
        }

        public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
        {
            var layout = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.messages_list_layout, parent, false);

            return new MessageViewHolder(layout, OnItemClick);

        }

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

        void OnItemClick(int position)
        {
            if (ItemClick != null)
                ItemClick(this, position);
        }
    }

        /// <summary>
        /// Called after <c><see cref="M:Android.App.Activity.OnRestoreInstanceState(Android.OS.Bundle)" /></c>, <c><see cref="M:Android.App.Activity.OnRestart" /></c>, or
        /// <c><see cref="M:Android.App.Activity.OnPause" /></c>, for your activity to start interacting with the user.
        /// </summary>
        protected override void OnResume()
    {
        var toggle = new ActionBarDrawerToggle(this, this.DrawerLayout, this.AppToolbar, 0, 0);
        this.DrawerLayout.AddDrawerListener(toggle);
        toggle.SyncState();

        base.OnResume();
    }

但这只是将布局改变为并排的东西

3 个答案:

答案 0 :(得分:1)

您的RecyclerView由适配器供电。

它显示的项目数等于您在适配器中提供的>

您可以通过在适配器中添加公共方法来更改数据集来更改它:

ItemCount

您可以像这样使用此方法:

public void SetMessages(List<MessageList> newDataset) {
    messages = newDataset;
}

修改

在您的情况下,只显示15个项目列表中的4个项目,您可以像这样调用adapter.SetMessages(myMessages); // myMessages is a List<MessageList> containing 4 objects recyclerView.NotifyDataSetChanged();

SetMessages

答案 1 :(得分:0)

简单的解决方案是将您的arraylist限制为4,然后在回收站视图中对其进行充气,而不是在回收站视图中进行更改。

答案 2 :(得分:0)

您可以在getItemsCount()

中进行更改

<强>解决方案:

class Adapter<ViewHolder> extends ...{
boolean userClicked=false;
//your other method

 @Override
public int getItemCount() {
    if(userClicked)
    return yourlist.size();
    else
    return yourlist.size()>4?4:yourlist.size();
}

public void setUserClicked( boolean userClicked){
        this.userClicked=userClicked;
        notifyDatasetChanged();
 }
}