大家好,我叫谷口
我已经实现了recyclerview并按组将行分开。我正在使用抽象列表来分隔行。
我在工具栏上插入了一个复选框,以选择recyclerview中的所有行及其工作,但是我也想做的是使用一个列表来存储collum值,并且此处显示错误,因为我想存储该值被插入到提供给recyclerview行的列表中。
我的recyclerview适配器:
public class RecyclerAdapter : RecyclerView.Adapter
{
private List<ListItem> mEmails;
private Activity mActivity;
public static int listPosition;
private int position_collectID = 0;
private List<collectSubject> mcollecSubject;
private Boolean isSelected = false;
public Boolean IsSelected()
{
return isSelected;
}
public void setSelected(Boolean selected)
{
isSelected = selected;
}
public RecyclerAdapter(List<ListItem> emails, Activity activity)
{
mEmails = emails;
mActivity = activity;
}
public override int GetItemViewType(int position)
{
return mEmails[position].getType();
}
public override int ItemCount => mEmails.Count;
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
if (viewType == ListItem.TYPE_HEADER_DATENOW)
{
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_DATEYESTERDAY)
{
View headYesterday = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.recyclerviewYesterday, parent, false);
RecyclerViewYesterdayHolder view = new RecyclerViewYesterdayHolder(headYesterday);
return view;
}
else if (viewType == ListItem.TYPE_HEADER_BEFORE)
{
View headBefore = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.recyclerviewBefore, parent, false);
RecyclerViewBeforeHolder view = new RecyclerViewBeforeHolder(headBefore);
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_DATENOW)
{
RecyclerViewNowHolder myHolder = holder as RecyclerViewNowHolder;
myHolder.mDateNow.Text = "Hoje";
}
else if (type == ListItem.TYPE_HEADER_BEFORE)
{
RecyclerViewBeforeHolder myHolder = holder as RecyclerViewBeforeHolder;
myHolder.mDateBefore.Text = "Antes";
}
else if (type == ListItem.TYPE_HEADER_DATEYESTERDAY)
{
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;
myHolder.ItemView.SetBackgroundColor(((ListItem)mEmails[position]).IsSelected() ? Color.LightBlue : Color.Transparent);
myHolder.ItemView.Tag = position;
}
}
public void checkall()
{
int itemcheck = 0;
if (mEmails != null)
{
mcollecSubject = new List<collectSubject>();
foreach (ListItem email in mEmails)
{
email.setSelected(true);
if (mcollecSubject == null) ;
mcollecSubject.Add(new collectSubject() { Subject = ((Email)mEmails[itemcheck]).Subject });
itemcheck++;
}
}
}
}
我在哪里调用recyclerview:
public class MainActivity : AppCompatActivity
{
public static RecyclerView mRecyclerView;
public static RecyclerView.LayoutManager mLayoutManager;
public RecyclerView.ViewHolder holder;
public RecyclerAdapter mAdapter;
private List<ListItem> mItems;
public static List<Email> mEmails;
public static Android.Support.V7.Widget.Toolbar toolbar;
public static Android.Widget.CheckBox checkbox;
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();
List<string> dateList = new List<string>();
List<DateNow> dateNow = new List<DateNow>();
List<DateYesterday> dateYesterday = new List<DateYesterday>();
List<DateBefore> dateBefore = new List<DateBefore>();
mItems = new List<ListItem>();
foreach (var email in mEmails)
{
if (!dateList.Contains(email.date))
{
dateList.Add(email.date);
dateNow.Add(new DateNow() { dateNow = email.date });
dateYesterday.Add(new DateYesterday() { dateYesterday = email.date });
dateBefore.Add(new DateBefore() { dateBefore = email.date });
}
}
DateTime today = DateTime.Today;
string sub = today.ToString().Substring(0, 10);
int yesterday = Convert.ToInt32(today.Day.ToString()) - 1;
string ontem = today.Month.ToString() + "/" + yesterday.ToString() + "/" + today.Year.ToString();
string ontem2 = today.Month.ToString() + "/" + yesterday.ToString() + "/" + "201";
foreach (var date in dateNow)
{
string currentmes = today.Month.ToString();
if (date.dateNow.Equals(sub))
{
mItems.Add(date);
foreach (var email in mEmails)
{
if (email.date.Equals(sub))
{
mItems.Add(email);
}
}
}
else if (date.dateNow.Equals(ontem))
{
foreach (var DateYesterday in dateYesterday)
{
if (DateYesterday.dateYesterday.Equals(ontem))
{
mItems.Add(DateYesterday);
foreach (var email in mEmails)
{
if (email.date.Equals(ontem))
{
mItems.Add(email);
}
}
}
}
}
}
int count = 0;
foreach (var DateBefore in dateBefore)
{
if (count == 0)
{
mItems.Add(DateBefore);
foreach (var email in mEmails)
{
if (!email.date.Equals(ontem) && !email.date.Equals(sub))
{
mItems.Add(email);
}
}
}
count = 1;
}
//Create our layout Manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.SetLayoutManager(mLayoutManager);
mAdapter = new RecyclerAdapter(mItems, this);
mRecyclerView.SetAdapter(mAdapter);
var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);
CheckBox checkbox = FindViewById<CheckBox>(Resource.Id.checkBox);
checkbox.Click += (o, e) => {
if (checkbox.Checked)
{
mAdapter.checkall();
}
};
}
private void SetupList()
{
mEmails = new List<Email>();
mEmails.Add(new Email() { date = "9/25/2019", Subject = "Wanna Hang Out?", Message = "I ' ll be around tomorrow!!" });
mEmails.Add(new Email() { date = "9/25/2019", Subject = "Wanna Hang Out?", Message = "I ' ll be around tomorrow!!" });
mEmails.Add(new Email() { date = "9/25/2019", Subject = "Wanna Hang Out?", Message = "I ' ll be around tomorrow!!" });
mEmails.Add(new Email() { date = "9/24/2019", Subject = "Wanna Hang Out?", Message = "I ' ll be around tomorrow!!" });
mEmails.Add(new Email() { date = "9/24/2019", Subject = "Wanna Hang Out?", Message = "I ' ll be around tomorrow!!" });
}
错误在recyclerview中在线:
mcollecSubject.Add(new collectSubject() { Subject = ((Email)mEmails[itemcheck]).Subject });
错误是:System.InvalidCastException:'指定的转换无效。'
错误状态: