我创建了一个以卡为主要视图类型的RecyclerView。我试图实现的是,当用户单击某个项目时,该项目的视图类型将从卡片更改为另一个视图类型(例如,可水平滚动放置的列表文本视图)。
我的RecyclerView适配器代码如下:
在下面的代码中,我创建了枚举类型的arrraylist,以跟踪recyclerview中每一行的单击状态,然后获取数据集的大小,并用show\u primary\u内容初始化每一行的arraylist

public class DisplayItemsAdapter extends RecyclerView.Adapter<DisplayItemsAdapter.ViewHolder> {

private static ArrayList<clickedState> itemClickedState;
private enum clickedState {
    SHOW_PRIMARY_CONTENT,
    SHOW_SECONDARY_CONTENT
}

private ArrayList<ItemData> mDataset = new ArrayList<>();

public DisplayItemsAdapter(ArrayList<ItemData> dataset) {
    mDataset = dataset;
    itemClickedState = new ArrayList<>();
    for (int i = 0; i < mDataset.size(); i++) {
        itemClickedState.add(i, clickedState.SHOW_PRIMARY_CONTENT);
    }
}

下面是我的viewholder类,它保存对所有子视图的引用,它实现view.onclicklistener。它实现view.onclicklistener的原因是,它可以在单击的状态之间切换以相应地更改视图类型。
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    TextView tvItemName;
    TextView tvItemNumber;

    public ViewHolder(View itemView) {
        super(itemView);

        tvItemName = (TextView) itemView.findViewById(R.id.tvItemName);
        tvItemNumber = (TextView) itemView.findViewById(R.id.tvItemNumber);

        tvTicketClass = (TextView) itemView.findViewById(R.id.tvTicketClass);
    }

    @Override
    public void onClick(View v) {
        itemClickedState.add(getAdapterPosition(), clickedState.SHOW_SECONDARY_CONTENT);
    }
}

这是我获取getitemviewtype返回的viewtype值并返回膨胀布局的地方
@Override
public DisplayItemsAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
    View v1 = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.display_Items_row, viewGroup, false);
    View v2 = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.display_Items_ticket_class, viewGroup, false);
    if (viewType == 0) {
        return new ViewHolder(v1);
    } else {
        return new ViewHolder(v2);
    }
}

然后在这里,我根据viewholderusing viewholder.getitemviewtype返回的viewtype将视图与值绑定
@Override
public void onBindViewHolder(DisplayItemsAdapter.ViewHolder viewHolder, final int position) {
    if (viewHolder.getItemViewType() == 0) {
        viewHolder.tvItemName.setText(mDataset.get(position).strItemName);
        viewHolder.tvItemNumber.setText(mDataset.get(position).strItemNumber);
    } else if (viewHolder.getItemViewType() == 1) {
        for (int i = 0; i < mDataset.get(position).strClass.length; i++) {
            viewHolder.tvTicketClass.setText(mDataset.get(position).strClass[i]);
        }
    }
}

@Override
public int getItemCount() {
    return mDataset.size();
}

最后是我的getitemViewType逻辑
@Override
public int getItemViewType(int position) {
    if (itemClickedState.get(position) == clickedState.SHOW_SECONDARY_CONTENT)
        return 1;
    return 0;
}
}

我不明白的是,为什么我运行代码时什么都不显示,,,对我来说一切都是有效的。帮助我!!!!!

最佳答案

看我的例子:

 // RecycleItemObject.java
 public class RecycleItemObject {
       public static int LAYOUT_ITEM_PORTRAIN = 0;
       public static int LAYOUT_ITEM_LANDSCAPE = 1;
       private int type;
         ....................
      /**
      * @return get layout type of item
      */
      public int getType() {
        return type;
      }

      /**
      * set layout type of item
      * @param type
      */
      public void setType(int type) {
         this.type = type;
      }
  }

 // adapter
 public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view;
    if (viewType == RecycleItemObject.LAYOUT_ITEM_PORTRAIN) {
        view = LayoutInflater.from(context).inflate(R.layout.item_recycle_view_main, parent, false);
    } else {
        view = LayoutInflater.from(context).inflate(R.layout.item_recycle_view_main_land, parent, false);
    }
 }

// event click item
@onClick....
curentObject.setType(RecycleItemObject.LAYOUT_ITEM_LANDSCAPE);
// notify item only
notifyItemChanged(itemPosition);

和我一起工作很好

07-24 09:24