我正在尝试将以下 android(java 适配器)代码转换为 xamarin.android(C#)。
1. ExpandableRecyclerAdapter.java
public abstract class ExpandableRecyclerAdapter<T extends ExpandableRecyclerAdapter.ListItem> extends RecyclerView.Adapter<ExpandableRecyclerAdapter.ViewHolder> {
protected Context mContext;
protected List<T> allItems = new ArrayList<>();
protected List<T> visibleItems = new ArrayList<>();
private List<Integer> indexList = new ArrayList<>();
private SparseIntArray expandMap = new SparseIntArray();
private int mode;
protected static final int TYPE_HEADER = 1000;
private static final int ARROW_ROTATION_DURATION = 150;
public static final int MODE_NORMAL = 0;
public static final int MODE_ACCORDION = 1;
public ExpandableRecyclerAdapter(Context context) {
mContext = context;
}
public static class ListItem {
public int ItemType;
public ListItem(int itemType) {
ItemType = itemType;
}
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public int getItemCount() {
return visibleItems == null ? 0 : visibleItems.size();
}
protected View inflate(int resourceID, ViewGroup viewGroup) {
return LayoutInflater.from(mContext).inflate(resourceID, viewGroup, false);
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View view) {
super(view);
}
}
public class HeaderViewHolder extends ViewHolder {
ImageView arrow;
public HeaderViewHolder(View view, final ImageView arrow) {
super(view);
this.arrow = arrow;
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
handleClick();
}
});
}
protected void handleClick() {
if (toggleExpandedItems(getLayoutPosition(), false)) {
openArrow(arrow);
} else {
closeArrow(arrow);
}
}
public void bind(int position) {
arrow.setRotation(isExpanded(position) ? 90 : 0);
}
}
public boolean toggleExpandedItems(int position, boolean notify) {
if (isExpanded(position)) {
collapseItems(position, notify);
return false;
} else {
expandItems(position, notify);
if (mode == MODE_ACCORDION) {
collapseAllExcept(position);
}
return true;
}
}
public void expandItems(int position, boolean notify) {
int count = 0;
int index = indexList.get(position);
int insert = position;
for (int i=index+1; i<allItems.size() && allItems.get(i).ItemType != TYPE_HEADER; i++) {
insert++;
count++;
visibleItems.add(insert, allItems.get(i));
indexList.add(insert, i);
}
notifyItemRangeInserted(position + 1, count);
int allItemsPosition = indexList.get(position);
expandMap.put(allItemsPosition, 1);
if (notify) {
notifyItemChanged(position);
}
}
public void collapseItems(int position, boolean notify) {
int count = 0;
int index = indexList.get(position);
for (int i=index+1; i<allItems.size() && allItems.get(i).ItemType != TYPE_HEADER; i++) {
count++;
visibleItems.remove(position + 1);
indexList.remove(position + 1);
}
notifyItemRangeRemoved(position + 1, count);
int allItemsPosition = indexList.get(position);
expandMap.delete(allItemsPosition);
if (notify) {
notifyItemChanged(position);
}
}
public class StaticViewHolder extends ViewHolder {
public StaticViewHolder(View view) {
super(view);
}
}
public class ItemViewHolder extends ViewHolder {
public ItemViewHolder(View view) {
super(view);
}
}
protected boolean isExpanded(int position) {
int allItemsPosition = indexList.get(position);
return expandMap.get(allItemsPosition, -1) >= 0;
}
@Override
public int getItemViewType(int position) {
return visibleItems.get(position).ItemType;
}
public void setItems(List<T> items) {
allItems = items;
List<T> visibleItems = new ArrayList<>();
expandMap.clear();
indexList.clear();
for (int i=0; i<items.size(); i++) {
if (items.get(i).ItemType == TYPE_HEADER) {
indexList.add(i);
visibleItems.add(items.get(i));
}
}
this.visibleItems = visibleItems;
notifyDataSetChanged();
}
protected void notifyItemInserted(int allItemsPosition, int visiblePosition) {
incrementIndexList(allItemsPosition, visiblePosition, 1);
incrementExpandMapAfter(allItemsPosition, 1);
if (visiblePosition >= 0) {
notifyItemInserted(visiblePosition);
}
}
protected void removeItemAt(int visiblePosition) {
int allItemsPosition = indexList.get(visiblePosition);
allItems.remove(allItemsPosition);
visibleItems.remove(visiblePosition);
incrementIndexList(allItemsPosition, visiblePosition, -1);
incrementExpandMapAfter(allItemsPosition, -1);
notifyItemRemoved(visiblePosition);
}
private void incrementExpandMapAfter(int position, int direction) {
SparseIntArray newExpandMap = new SparseIntArray();
for (int i=0; i<expandMap.size(); i++) {
int index = expandMap.keyAt(i);
newExpandMap.put(index < position ? index : index + direction, 1);
}
expandMap = newExpandMap;
}
private void incrementIndexList(int allItemsPosition, int visiblePosition, int direction) {
List<Integer> newIndexList = new ArrayList<>();
for (int i=0; i<indexList.size(); i++) {
if (i == visiblePosition) {
if (direction > 0) {
newIndexList.add(allItemsPosition);
}
}
int val = indexList.get(i);
newIndexList.add(val < allItemsPosition ? val : val + direction);
}
indexList = newIndexList;
}
public void collapseAll() {
collapseAllExcept(-1);
}
public void collapseAllExcept(int position) {
for (int i=visibleItems.size()-1; i>=0; i--) {
if (i != position && getItemViewType(i) == TYPE_HEADER) {
if (isExpanded(i)) {
collapseItems(i, true);
}
}
}
}
public void expandAll() {
for (int i=visibleItems.size()-1; i>=0; i--) {
if (getItemViewType(i) == TYPE_HEADER) {
if (!isExpanded(i)) {
expandItems(i, true);
}
}
}
}
public void expendSingle()
{
if (!isExpanded(2)) {
expandItems(2, true);
}
}
public static void openArrow(View view) {
view.animate().setDuration(ARROW_ROTATION_DURATION).rotation(90);
}
public static void closeArrow(View view) {
view.animate().setDuration(ARROW_ROTATION_DURATION).rotation(0);
}
public int getMode() {
return mode;
}
public void setMode(int mode) {
this.mode = mode;
}}
2. PeopleAdapter.java
public class PeopleAdapter extends ExpandableRecyclerAdapter<PeopleAdapter.PeopleListItem> {
public static final int TYPE_PERSON = 1001;
public PeopleAdapter(Context context) {
super(context);
setItems(getSampleItems());
}
public static class PeopleListItem extends ExpandableRecyclerAdapter.ListItem {
public String Text;
public PeopleListItem(String group) {
super(TYPE_HEADER);
Text = group;
}
public PeopleListItem(String first, String last) {
super(TYPE_PERSON);
Text = first + " " + last;
}
}
public class HeaderViewHolder extends ExpandableRecyclerAdapter.HeaderViewHolder {
TextView name;
public HeaderViewHolder(View view) {
super(view, (ImageView) view.findViewById(R.id.item_arrow));
name = (TextView) view.findViewById(R.id.item_header_name);
}
public void bind(int position) {
super.bind(position);
name.setText(visibleItems.get(position).Text);
}
}
public class PersonViewHolder extends ExpandableRecyclerAdapter.ViewHolder {
TextView name;
public PersonViewHolder(View view) {
super(view);
name = (TextView) view.findViewById(R.id.item_name);
}
public void bind(int position) {
name.setText(visibleItems.get(position).Text);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case TYPE_HEADER:
return new HeaderViewHolder(inflate(R.layout.item_header, parent));
case TYPE_PERSON:
default:
return new PersonViewHolder(inflate(R.layout.item_person, parent));
}
}
@Override
public void onBindViewHolder(ExpandableRecyclerAdapter.ViewHolder holder, int position) {
switch (getItemViewType(position)) {
case TYPE_HEADER:
((HeaderViewHolder) holder).bind(position);
break;
case TYPE_PERSON:
default:
((PersonViewHolder) holder).bind(position);
break;
}
}
private List<PeopleListItem> getSampleItems() {
List<PeopleListItem> items = new ArrayList<>();
items.add(new PeopleListItem("Friends"));
items.add(new PeopleListItem("Bill", "Smith"));
items.add(new PeopleListItem("John", "Doe"));
items.add(new PeopleListItem("Frank", "Hall"));
items.add(new PeopleListItem("Sue", "West"));
items.add(new PeopleListItem("Family"));
items.add(new PeopleListItem("Drew", "Smith"));
items.add(new PeopleListItem("Chris", "Doe"));
items.add(new PeopleListItem("Alex", "Hall"));
items.add(new PeopleListItem("Associates"));
items.add(new PeopleListItem("John", "Jones"));
items.add(new PeopleListItem("Ed", "Smith"));
items.add(new PeopleListItem("Jane", "Hall"));
items.add(new PeopleListItem("Tim", "Lake"));
items.add(new PeopleListItem("Colleagues"));
items.add(new PeopleListItem("Carol", "Jones"));
items.add(new PeopleListItem("Alex", "Smith"));
items.add(new PeopleListItem("Kristin", "Hall"));
items.add(new PeopleListItem("Pete", "Lake"));
items.add(new PeopleListItem("Friends"));
items.add(new PeopleListItem("Bill", "Smith"));
items.add(new PeopleListItem("John", "Doe"));
items.add(new PeopleListItem("Frank", "Hall"));
items.add(new PeopleListItem("Sue", "West"));
items.add(new PeopleListItem("Family"));
items.add(new PeopleListItem("Drew", "Smith"));
items.add(new PeopleListItem("Chris", "Doe"));
items.add(new PeopleListItem("Alex", "Hall"));
items.add(new PeopleListItem("Associates"));
items.add(new PeopleListItem("John", "Jones"));
items.add(new PeopleListItem("Ed", "Smith"));
items.add(new PeopleListItem("Jane", "Hall"));
items.add(new PeopleListItem("Tim", "Lake"));
items.add(new PeopleListItem("Colleagues"));
items.add(new PeopleListItem("Carol", "Jones"));
items.add(new PeopleListItem("Alex", "Smith"));
items.add(new PeopleListItem("Kristin", "Hall"));
items.add(new PeopleListItem("Pete", "Lake"));
items.add(new PeopleListItem("Friends"));
items.add(new PeopleListItem("Bill", "Smith"));
items.add(new PeopleListItem("John", "Doe"));
items.add(new PeopleListItem("Frank", "Hall"));
items.add(new PeopleListItem("Sue", "West"));
items.add(new PeopleListItem("Family"));
items.add(new PeopleListItem("Drew", "Smith"));
items.add(new PeopleListItem("Chris", "Doe"));
items.add(new PeopleListItem("Alex", "Hall"));
items.add(new PeopleListItem("Associates"));
items.add(new PeopleListItem("John", "Jones"));
items.add(new PeopleListItem("Ed", "Smith"));
items.add(new PeopleListItem("Jane", "Hall"));
items.add(new PeopleListItem("Tim", "Lake"));
items.add(new PeopleListItem("Colleagues"));
items.add(new PeopleListItem("Carol", "Jones"));
items.add(new PeopleListItem("Alex", "Smith"));
items.add(new PeopleListItem("Kristin", "Hall"));
items.add(new PeopleListItem("Pete", "Lake"));
return items;
}
}
但是我无法用返回类型 ViewHolder 和参数类型为 ExpandableRecyclerAdapter.ViewHolder 的 PeopleAdapter 类的“onBindViewHolder”覆盖方法的“onCreateViewHolder” ,以防 xamarin。
提前致谢。
这是我的 C# 代码。
1. ExpandableRecyclerAdapter.cs
public abstract class ExpandableRecyclerAdapter : RecyclerView.Adapter
{
protected internal Context mContext;
protected internal IList<ListItem> allItems = new List<ListItem>();
protected internal IList<ListItem> visibleItems = new List<ListItem>();
private IList<int> indexList = new List<int>();
private SparseIntArray expandMap = new SparseIntArray();
private int mode;
protected internal const int TYPE_HEADER = 1000;
private const int ARROW_ROTATION_DURATION = 150;
public const int MODE_NORMAL = 0;
public const int MODE_ACCORDION = 1;
public ExpandableRecyclerAdapter(Context context)
{
mContext = context;
}
public class ListItem
{
public int ItemType;
public ListItem(int itemType)
{
ItemType = itemType;
}
}
public override long GetItemId(int i)
{
return i;
}
public override int ItemCount
{
get
{
return visibleItems == null ? 0 : visibleItems.Count;
}
}
protected internal virtual View inflate(int resourceID, ViewGroup viewGroup)
{
return LayoutInflater.From(mContext).Inflate(resourceID, viewGroup, false);
}
public class ViewHolder : RecyclerView.ViewHolder
{
public ViewHolder(View view) : base(view)
{
}
}
public class HeaderViewHolder : ViewHolder,View.IOnClickListener
{
internal ImageView arrow;
public HeaderViewHolder(View view, ImageView arrow) : base(view)
{
this.arrow = arrow;
view.SetOnClickListener(this);
}
protected internal virtual void handleClick()
{
if (toggleExpandedItems(LayoutPosition, false))
{
openArrow(arrow);
}
else
{
closeArrow(arrow);
}
}
public virtual void bind(int position)
{
arrow.Rotation = isExpanded(position) ? 90 : 0;
}
public void OnClick(View v)
{
handleClick();
}
}
public virtual bool toggleExpandedItems(int position, bool notify)
{
if (isExpanded(position))
{
collapseItems(position, notify);
return false;
}
else
{
expandItems(position, notify);
if (mode == MODE_ACCORDION)
{
collapseAllExcept(position);
}
return true;
}
}
public virtual void expandItems(int position, bool notify)
{
int count = 0;
int index = indexList[position];
int insert = position;
for (int i = index + 1; i < allItems.Count && allItems[i].ItemType != TYPE_HEADER; i++)
{
insert++;
count++;
visibleItems.Insert(insert, allItems[i]);
indexList.Insert(insert, i);
}
NotifyItemRangeInserted(position + 1, count);
int allItemsPosition = indexList[position];
expandMap.Put(allItemsPosition, 1);
if (notify)
{
NotifyItemChanged(position);
}
}
public virtual void collapseItems(int position, bool notify)
{
int count = 0;
int index = indexList[position];
for (int i = index + 1; i < allItems.Count && allItems[i].ItemType != TYPE_HEADER; i++)
{
count++;
visibleItems.RemoveAt(position + 1);
indexList.RemoveAt(position + 1);
}
NotifyItemRangeRemoved(position + 1, count);
int allItemsPosition = indexList[position];
expandMap.Delete(allItemsPosition);
if (notify)
{
NotifyItemChanged(position);
}
}
public class StaticViewHolder : ViewHolder
{
public StaticViewHolder(View view):base(view)
{
}
}
public class ItemViewHolder : ViewHolder
{
public ItemViewHolder(View view) : base(view)
{
}
}
protected bool isExpanded(int position)
{
int allItemsPosition = indexList[position];
return expandMap.Get(allItemsPosition, -1) >= 0;
}
public override int GetItemViewType(int position)
{
return visibleItems[position].ItemType;
}
public void setItems(List<ListItem> items)
{
allItems = items;
List<ListItem> visibleItems = new List<ListItem>();
expandMap.Clear();
indexList.Clear();
for (int i = 0; i < items.Count; i++)
{
if (items[i].ItemType == TYPE_HEADER)
{
indexList.Add(i);
visibleItems.Add(items[i]);
}
}
this.visibleItems = visibleItems;
NotifyDataSetChanged();
}
protected void notifyItemInserted(int allItemsPosition, int visiblePosition)
{
incrementIndexList(allItemsPosition, visiblePosition, 1);
incrementExpandMapAfter(allItemsPosition, 1);
if (visiblePosition >= 0)
{
NotifyItemInserted(visiblePosition);
}
}
protected void removeItemAt(int visiblePosition)
{
int allItemsPosition = indexList[visiblePosition];
//allItems.Remove(allItemsPosition);
//visibleItems.Remove(visiblePosition);
incrementIndexList(allItemsPosition, visiblePosition, -1);
incrementExpandMapAfter(allItemsPosition, -1);
NotifyItemRemoved(visiblePosition);
}
private void incrementExpandMapAfter(int position, int direction)
{
SparseIntArray newExpandMap = new SparseIntArray();
for (int i = 0; i < expandMap.Size(); i++)
{
int index = expandMap.KeyAt(i);
newExpandMap.Put(index < position ? index : index + direction, 1);
}
expandMap = newExpandMap;
}
private void incrementIndexList(int allItemsPosition, int visiblePosition, int direction)
{
List<int> newIndexList = new List<int>();
for (int i = 0; i < indexList.Count; i++)
{
if (i == visiblePosition)
{
if (direction > 0)
{
newIndexList.Add(allItemsPosition);
}
}
int val = indexList[i];
newIndexList.Add(val < allItemsPosition ? val : val + direction);
}
indexList = newIndexList;
}
public void collapseAll()
{
collapseAllExcept(-1);
}
public void collapseAllExcept(int position)
{
for (int i = visibleItems.Count - 1; i >= 0; i--)
{
if (i != position && GetItemViewType(i) == TYPE_HEADER)
{
if (isExpanded(i))
{
collapseItems(i, true);
}
}
}
}
public void expandAll()
{
for (int i = visibleItems.Count - 1; i >= 0; i--)
{
if (GetItemViewType(i) == TYPE_HEADER)
{
if (!isExpanded(i))
{
expandItems(i, true);
}
}
}
}
public void expendSingle()
{
if (!isExpanded(2))
{
expandItems(2, true);
}
}
public static void openArrow(View view)
{
view.Animate().SetDuration(ARROW_ROTATION_DURATION).Rotation(90);
}
public static void closeArrow(View view)
{
view.Animate().SetDuration(ARROW_ROTATION_DURATION).Rotation(0);
}
public int getMode()
{
return mode;
}
public void setMode(int mode)
{
this.mode = mode;
}
}
2. PeopleAdapter.cs
public class PeopleAdapter : ExpandableRecyclerAdapter
{
public const int TYPE_PERSON = 1001;
public PeopleAdapter(Context context):base(context)
{
setItems(getSampleItems());
}
public class PeopleListItem : ExpandableRecyclerAdapter.ListItem
{
public string Text;
public PeopleListItem(String group):base(TYPE_HEADER)
{
Text = group;
}
public PeopleListItem(String first, String last) : base(TYPE_PERSON)
{
Text = first + " " + last;
}
}
public class HeaderViewHolder : ExpandableRecyclerAdapter.HeaderViewHolder
{
TextView name;
public HeaderViewHolder(View view): base(view, (ImageView)view.FindViewById(Resource.Id.item_arrow))
{
name = (TextView)view.FindViewById(Resource.Id.item_header_name);
}
public void bind(int position)
{
base.bind(position);
name.SetText(visibleItems[position].Text);
}
}
public class PersonViewHolder : ExpandableRecyclerAdapter.ViewHolder
{
TextView name;
public PersonViewHolder(View view):base(view)
{
name = (TextView)view.FindViewById(Resource.Id.item_name);
}
public void bind(int position)
{
name.SetText(visibleItems[position].Text);
}
}
public override void OnBindViewHolder(ExpandableRecyclerAdapter.ViewHolder holder, int position)
{
switch (GetItemViewType(position))
{
case TYPE_HEADER:
((HeaderViewHolder)holder).bind(position);
break;
case TYPE_PERSON:
default:
((PersonViewHolder)holder).bind(position);
break;
}
}
public override ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
switch (viewType)
{
case TYPE_HEADER:
return new HeaderViewHolder(inflate(Resource.Layout.item_header, parent));
case TYPE_PERSON:
default:
return new PersonViewHolder(inflate(Resource.Layout.item_person, parent));
}
}
private List<PeopleListItem> getSampleItems()
{
List<PeopleListItem> items = new List<PeopleListItem>();
items.Add(new PeopleListItem("Friends"));
items.Add(new PeopleListItem("Bill", "Smith"));
items.Add(new PeopleListItem("John", "Doe"));
items.Add(new PeopleListItem("Frank", "Hall"));
items.Add(new PeopleListItem("Sue", "West"));
items.Add(new PeopleListItem("Family"));
items.Add(new PeopleListItem("Drew", "Smith"));
items.Add(new PeopleListItem("Chris", "Doe"));
items.Add(new PeopleListItem("Alex", "Hall"));
items.Add(new PeopleListItem("Associates"));
items.Add(new PeopleListItem("John", "Jones"));
items.Add(new PeopleListItem("Ed", "Smith"));
items.Add(new PeopleListItem("Jane", "Hall"));
items.Add(new PeopleListItem("Tim", "Lake"));
items.Add(new PeopleListItem("Colleagues"));
items.Add(new PeopleListItem("Carol", "Jones"));
items.Add(new PeopleListItem("Alex", "Smith"));
items.Add(new PeopleListItem("Kristin", "Hall"));
items.Add(new PeopleListItem("Pete", "Lake"));
items.Add(new PeopleListItem("Friends"));
items.Add(new PeopleListItem("Bill", "Smith"));
items.Add(new PeopleListItem("John", "Doe"));
items.Add(new PeopleListItem("Frank", "Hall"));
items.Add(new PeopleListItem("Sue", "West"));
items.Add(new PeopleListItem("Family"));
items.Add(new PeopleListItem("Drew", "Smith"));
items.Add(new PeopleListItem("Chris", "Doe"));
items.Add(new PeopleListItem("Alex", "Hall"));
items.Add(new PeopleListItem("Associates"));
items.Add(new PeopleListItem("John", "Jones"));
items.Add(new PeopleListItem("Ed", "Smith"));
items.Add(new PeopleListItem("Jane", "Hall"));
items.Add(new PeopleListItem("Tim", "Lake"));
items.Add(new PeopleListItem("Colleagues"));
items.Add(new PeopleListItem("Carol", "Jones"));
items.Add(new PeopleListItem("Alex", "Smith"));
items.Add(new PeopleListItem("Kristin", "Hall"));
items.Add(new PeopleListItem("Pete", "Lake"));
items.Add(new PeopleListItem("Friends"));
items.Add(new PeopleListItem("Bill", "Smith"));
items.Add(new PeopleListItem("John", "Doe"));
items.Add(new PeopleListItem("Frank", "Hall"));
items.Add(new PeopleListItem("Sue", "West"));
items.Add(new PeopleListItem("Family"));
items.Add(new PeopleListItem("Drew", "Smith"));
items.Add(new PeopleListItem("Chris", "Doe"));
items.Add(new PeopleListItem("Alex", "Hall"));
items.Add(new PeopleListItem("Associates"));
items.Add(new PeopleListItem("John", "Jones"));
items.Add(new PeopleListItem("Ed", "Smith"));
items.Add(new PeopleListItem("Jane", "Hall"));
items.Add(new PeopleListItem("Tim", "Lake"));
items.Add(new PeopleListItem("Colleagues"));
items.Add(new PeopleListItem("Carol", "Jones"));
items.Add(new PeopleListItem("Alex", "Smith"));
items.Add(new PeopleListItem("Kristin", "Hall"));
items.Add(new PeopleListItem("Pete", "Lake"));
return items;
}}