Xamarin Android:为什么在recyclerview上用三个键按组划分元素不起作用?

问题描述 投票:0回答:1

大家好,我叫谷口

我实现了一个recyclerview,并将recyclerview项划分为一组。当我尝试分为两组时,没有显示代码错误,但是出现在回收站上的所有项目仅显示了viewholder textview名称

如下图所示:

https://drive.google.com/open?id=1TX5R0o44v-nWcTduYsqmH7KCFSDraZ24

第一类清单:

public class DateNow : ListItem
{
    public string dateNow{ set; get; }
    public override int getType()
    {
        return TYPE_HEADER_NOW;
    }

}

第一位观看者:

public class RecyclerViewNowHolder : RecyclerView.ViewHolder
{

    public TextView mDateNow;

    public RecyclerViewNowHolder(View itemView) : base(itemView)
    {
        mDateNow = itemView.FindViewById<TextView>(Resource.Id.mDateNow);


    }

}

}

第二列表类别:

 public class DateYesterday : ListItem
{
    public string dateYesterday { set; get; }
    public override int getType()
    {
        return TYPE_HEADER_YESTERDAY;
    }
}

第二个观看者:

 public class RecyclerViewYesterdayHolder : RecyclerView.ViewHolder
{
    public TextView mDateYesterday;

    public RecyclerViewYesterdayHolder(View itemView) : base(itemView)
    {
        mDateYesterday = itemView.FindViewById<TextView>(Resource.Id.mDateYesterday);

    }
}

清单项目类别:

public abstract class ListItem
{

    public static int TYPE_HEADER = 0;
    public static int TYPE_CONTENT = 1;
    public static int TYPE_HEADER_NOW = 2;
    public static int TYPE_HEADER_YESTERDAY = 3;

    abstract public int getType();
}

我的recyclerview适配器:

  public class RecyclerAdapter : RecyclerView.Adapter
{

    private Activity mActivity;
    private List<ListItem> mEmails;
    private Context context;

    public RecyclerAdapter(List<ListItem> emails, Context context)
    {
        mEmails = emails;
        this.context = context;
    }

    public RecyclerAdapter(List<ListItem> emails, Activity activity)
    {
        mEmails = emails;
        mActivity = activity;
    }


    public class MyView : RecyclerView.ViewHolder
    {
        public View mMainView { get; set; }
        public TextView mName { get; set; }
        public TextView mSubject { get; set; }
        public TextView mMessage { get; set; }

        public MyView(View view) : base(view)
        {
            mMainView = view;
        }

    }

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

    public override int GetItemViewType(int position)
    {
        return mEmails[position].getType();
    }


    public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
    {
        if (viewType == ListItem.TYPE_HEADER_NOW)
        {
            View headNow = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.recyclerviewNow, parent, false);

            RecyclerViewNowHolder view = new RecyclerViewNowHolder(headNow);
            return view;
        }
        else if (viewType == ListItem.TYPE_HEADER_YESTERDAY)
        {
            View headYesterday = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.recyclerviewYesterday, parent, false);

            RecyclerViewYesterdayHolder view = new RecyclerViewYesterdayHolder(headYesterday);
            return view;
        }
        else
        {
            View row = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.row, parent, false);
            RecyclerViewHolder view = new RecyclerViewHolder(row);
            return view;
        }

    }
    public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
        int type = GetItemViewType(position);
        if (type == ListItem.TYPE_HEADER_NOW)
        {
            RecyclerViewNowHolder myHolder = holder as RecyclerViewNowHolder;
            myHolder.mDateNow.Text = "Hoje";

        }
        else if (type == ListItem.TYPE_HEADER_YESTERDAY)
        {
            RecyclerViewYesterdayHolder myHolder = holder as RecyclerViewYesterdayHolder;
            myHolder.mDateYesterday.Text = "Ontem";

        }
        else
        {
            RecyclerViewHolder myHolder = holder as RecyclerViewHolder;
            myHolder.mDate.Text = ((Email)mEmails[position]).date;
            myHolder.mSubject.Text = ((Email)mEmails[position]).Subject;
            myHolder.mMessage.Text = ((Email)mEmails[position]).Message;
        }

    }

}   

我的主要活动类别:

[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
    public static RecyclerView mRecyclerView;
    public static RecyclerView.LayoutManager mLayoutManager;
    public RecyclerView.ViewHolder holder;
    public static BottomNavigationView bottomnavigationview1;
    public static RecyclerView.Adapter mAdapter;
    public static List<Email> mEmails;
    private List<ListItem> mItems;


    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        Xamarin.Essentials.Platform.Init(this, bundle);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);

        mRecyclerView = FindViewById<RecyclerView>(Resource.Id.recyclerViwer);
        mRecyclerView.AddItemDecoration(new DividerItemDecoration(mRecyclerView.Context, DividerItemDecoration.Vertical));
        mRecyclerView.HasFixedSize = true;
        SetupList();

        //Create our layout Manager
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.SetLayoutManager(mLayoutManager);
        RecyclerAdapter mAdapter = new RecyclerAdapter(mItems, this);
        mRecyclerView.SetAdapter(mAdapter);

    }

    private void SetupList()
    {
        mEmails = new List<Email>();
        mEmails.Add(new Email() { date = "9/24/2019", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
        mEmails.Add(new Email() { date = "9/23/2019", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
        mEmails.Add(new Email() { date = "9/19/2019", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
        mEmails.Add(new Email() { date = "9/19/2019", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
        mEmails.Add(new Email() { date = "9/19/2019", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
        mEmails.Add(new Email() { date = "8/19/2019", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
        mEmails.Add(new Email() { date = "8/19/2019", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
        mEmails.Add(new Email() { date = "7/19/2019", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
        mEmails.Add(new Email() { date = "7/19/2019", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
        mEmails.Add(new Email() { date = "6/19/2019", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
        mEmails.Add(new Email() { date = "6/19/2019", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
        mEmails.Add(new Email() { date = "5/19/2019", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
        mEmails.Add(new Email() { date = "5/19/2019", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
        mEmails.Add(new Email() { date = "4/19/2019", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
        mEmails.Add(new Email() { date = "4/19/2019", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
        mEmails.Add(new Email() { date = "9/19/2018", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });
        mEmails.Add(new Email() { date = "9/19/2018", Subject = "Wanna Hang Out?", Message = "I ' ll   be around  tomorrow!!" });

        List<string> dateList = new List<string>();
            List<DateNow> dateItems = new List<DateNow>();
            List<DateYesterday> dateYesterday = new List<DateYesterday>();
        mItems = new List<ListItem>();
            foreach (var email in mEmails)
            {
                if (!dateList.Contains(email.date))
                {
                    dateList.Add(email.date);
                    dateItems.Add(new DateNow() { dateNow = email.date });
                    dateYesterday.Add(new DateYesterday() { dateYesterday = email.date });

                }

            }

            DateTime today = DateTime.Today;
            string sub = today.ToString().Substring(0, 9);

            int yesterday = Convert.ToInt32(today.Day.ToString()) - 1;

            foreach (var date in dateItems)
            {
                mItems.Add(date);
                foreach (var email in mEmails)
                {
                    if (date.dateNow.Equals(DateTime.Now.Day.ToString()))
                    {
                        mItems.Add(email);
                    }

                }
            }


    }
c# xamarin.android
1个回答
0
投票

从您的代码中,我们发现您仅遍历列表dateItems并将dateItems的子代添加到mItems,并且mItemsRecyclerView的数据源:

        foreach (var date in dateItems)
        {
            mItems.Add(date);
            foreach (var email in mEmails)
            {
                if (date.dateNow.Equals(DateTime.Now.Day.ToString()))
                {
                    mItems.Add(email);
                }
            }
        }

并且从下面的代码中,我们可以看到List<DateNow> dateItems = new List<DateNow>();,并且dateItemsDateNow类型的数据的集合。

 List<string> dateList = new List<string>();
 List<DateNow> dateItems = new List<DateNow>();
 List<DateYesterday> dateYesterday = new List<DateYesterday>();
 mItems = new List<ListItem>();
 foreach (var email in mEmails)
  {
    if (!dateList.Contains(email.date))
     {
       dateList.Add(email.date);
       dateItems.Add(new DateNow() { dateNow = email.date });
       dateYesterday.Add(new DateYesterday() { dateYesterday = email.date });

      }
   }

因此,回收站仅显示RecyclerViewNowHolder文本视图名称(DateNow),其方法getType()TYPE_HEADER_NOW

注:

如果添加以下代码,您会发现回收站还将显示RecyclerViewYesterdayHolder ViewHolder。

foreach (var date in dateYesterday) {
            mItems.Add(date);
        }
© www.soinside.com 2019 - 2024. All rights reserved.