RecyclerView更改编辑项目的布局并在eddit后更新数据

时间:2017-11-29 08:45:49

标签: android android-layout android-recyclerview

我有物品的RecyclerView。我的项目看起来像这样(1):

enter image description here

当我点击项目时,我需要一个像2或3的项目。

如果喜欢2 - 我是否需要在DB中为每次更改更新数据 - +或添加"应用"按钮

我该如何制作这个逻辑?我不想使用库和make默认的android工具。但如果它很难,你建议图书馆

1 个答案:

答案 0 :(得分:-1)

您可以在recycleview中管理多种视图类型。检查示例代码以供参考。

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    class ViewHolder0 extends RecyclerView.ViewHolder {


   ...
    public ViewHolder0(View itemView){
    ...
    }
}

class ViewHolder2 extends RecyclerView.ViewHolder {
    ...
    public ViewHolder2(View itemView){
    ...
}

@Override
public int getItemViewType(int position) {
    // Just as an example, return 0 or 2 depending on position
    // Note that unlike in ListView adapters, types don't have to be contiguous
    return position % 2 * 2;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
     switch (viewType) {
         case 0: return new ViewHolder0(...);
         case 2: return new ViewHolder2(...);
         ...
     }
}

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
    switch (holder.getItemViewType()) {
        case 0:
            ViewHolder0 viewHolder0 = (ViewHolder0)holder;
            ...
            break;

        case 2:
            ViewHolder2 viewHolder2 = (ViewHolder2)holder;
            ...
            break;
    }
}

}