如何在xamarin.android中创建可扩展的RecyclerView

时间:2018-09-17 14:27:30

标签: c# xamarin.android expandablerecyclerview

我正在尝试将以下android(java adapter)代码转换为xamarin.android(C#)。

1。 ExpandableRecyclerAdapter.java

E/GraphResponse: {HttpStatus: 400, errorCode: 100, subErrorCode: 33, errorType: GraphMethodException, errorMessage: Unsupported get request. Object with ID 'XXXXX' does not exist, cannot be loaded due to missing permissions, or does not support this operation. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api}

2。 PeopleAdapter.java

{
  "data": {
    "app_id": "XXXXXXX",
    "type": "APP",
    "application": "AppName",
    "is_valid": true,
    "scopes": [
    ]
  }
}

}

但是对于xamarin,我无法为参数类型为ExpandableRecyclerAdapter.ViewHolder的返回类型ViewHolder和“ onBindViewHolder” 覆盖方法的“ onCreateViewHolder” 。 / p>

谢谢。

这是我的C#代码。

1。 ExpandableRecyclerAdapter.cs

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.cs

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;
}

0 个答案:

没有答案