我能够在我的应用程序中成功实现可扩展的回收视图。我使用this来实现它。我有一个要求,即在加载项目时需要显示图像(向下箭头),展开项目时,图像应隐藏。点击该项目(即折叠)后,图像(向下箭头指示符)应该再次可见。如何实现?
这是我的RecycleViewAdaptor:
public class UEventsRecycleViewAdaptor : RecyclerView.Adapter
{
private List<DataType> listItems;
private Context context;
private LayoutInflater inflater;
public event EventHandler<int> GroupClick;
public event EventHandler<int> BttGroupClick;
public event EventHandler<int> BttEntryClick;
public UEventsRecycleViewAdaptor(Context context, List<DataType> listItems)
{
this.context = context;
this.listItems = listItems;
this.inflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
}
void GroupOnClick(int position)
{
if (GroupClick != null)
GroupClick(this, position);
}
void BttGroupOnClick(int position)
{
if (BttGroupClick != null)
BttGroupClick(this, position);
}
void BttEntryOnClick(int position)
{
if (BttEntryClick != null)
BttEntryClick(this, position);
}
public override int ItemCount
{
get
{
return listItems.Count;
}
}
public override int GetItemViewType(int position)
{
return listItems[position].GetItemType();
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
DataType itemNode = listItems[position];
if (itemNode.GetItemType() == 0)
{
UEventsGroupData group = itemNode as UEventsGroupData;
UEventsGroupHolder groupHolder = holder as UEventsGroupHolder;
groupHolder.txtEvents.Text= group.Events;
}
else if (itemNode.GetItemType() == 1)
{
UEventsEntryData entry = itemNode as UEventsEntryData;
UEventsEntryHolder holderEntry = holder as UEventsEntryHolder;
holderEntry.txtLocation.Text = entry.Location;
holderEntry.txtStartDate.Text = entry.StartDate;
holderEntry.txtEndDate.Text = entry.EndDate;
}
}
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
if (viewType == 0)
{
View entryView = inflater.Inflate(Resource.Layout.UEventsRecycler_Group, parent, false);
return new UEventsGroupHolder(entryView, GroupOnClick, BttGroupOnClick);
}
else if (viewType == 1)
{
View sectionView = inflater.Inflate(Resource.Layout.UEventsRecycler_Entry, parent, false);
return new UEventsEntryHolder(sectionView, BttEntryOnClick);
}
else
{
return null;
}
}
}
public class UEventsGroupHolder : RecyclerView.ViewHolder
{
public TextView txtEvents { get; set; }
public UEventsGroupHolder(View view, Action<int> groupClickListener, Action<int> bttClickListener) : base(view)
{
this.txtEvents = (TextView)view.FindViewById(Resource.Id.group_Event);
view.Click += (sender, e) => groupClickListener(base.AdapterPosition);
}
}
public class UEventsEntryHolder : RecyclerView.ViewHolder
{
public TextView txtLocation { set; get; }
public TextView txtStartDate { set; get; }
public TextView txtEndDate { set; get; }
public UEventsEntryHolder(View view, Action<int> bttEntryClickListener) : base(view)
{
this.txtLocation = (TextView)view.FindViewById(Resource.Id.Location);
this.txtStartDate = (TextView)view.FindViewById(Resource.Id.s_StartDate);
this.txtEndDate = (TextView)view.FindViewById(Resource.Id.s_EndDate);
}
}
}
这是我的组布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/group_Event"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_centerHorizontal="true"
android:textColor="@color/white"/>
<ImageView
android:id="@+id/imgArrowUE"
android:src="@drawable/moredetails"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_alignParentRight="true"/>
这是我的条目布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/LocationLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Location :"
android:textColor="@color/white"/>
<TextView
android:id="@+id/Location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="@color/white"
android:layout_toRightOf="@id/LocationLabel"/>
<TextView
android:id="@+id/StartDateLabel"
android:textColor="@color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Date :"
android:layout_below="@id/LocationLabel"/>
<TextView
android:textColor="@color/white"
android:id="@+id/s_StartDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_below="@id/Location"
android:layout_toRightOf="@id/StartDateLabel"/>
<TextView
android:textColor="@color/white"
android:id="@+id/EndDateLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="End Date :"
android:layout_below="@id/StartDateLabel"/>
<TextView
android:textColor="@color/white"
android:id="@+id/s_EndDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_below="@id/s_StartDate"
android:layout_toRightOf="@id/EndDateLabel"/>
</RelativeLayout>
这是我的网上论坛点击列表:
void OnGroupClick(object sender, int pos)
{
UEventsGroupData groupSelect = (UEventsGroupData)listItems[pos];
if (groupSelect.items.Count == 0)
{
int count = 0;
while (listItems.Count > pos + 1 && listItems[pos + 1].GetItemType() == 1)
{
groupSelect.items.Add(listItems[pos + 1]);
listItems.RemoveAt(pos + 1);
recyclerAdapter.NotifyItemRemoved(pos + 1);
count++;
}
}
else
{
int index = pos + 1;
foreach (UEventsEntryData entryNode in groupSelect.items)
{
listItems.Insert(index, entryNode);
recyclerAdapter.NotifyItemInserted(index);
index++;
}
groupSelect.items.Clear();
}
}