我在recyclerview中显示了一个交易清单。我现在想要显示另一个关于单击第一个视图类型的父布局的视图。
我为两种视图类型创建了不同的布局和不同的持有者。
我想知道如何在第一个视图类型的父布局上显示第二个视图类型。
这是我的适配器:
public class TransactionHistoryListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<Transaction> transactionList;
private Context mContext;
//static var
static final int TYPE_LOAD_TRANS = 0, TYPE_LOAD_TRANS_DETAIL = 1;
public TransactionHistoryListAdapter(List<Transaction> transactionsList, Context context) {
this.mContext = context;
this.transactionList = transactionsList;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder = null;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
switch (viewType) {
case TYPE_LOAD_TRANS:
View v_header = inflater.inflate(R.layout.transaction_item_layout, parent, false);
viewHolder = new TransactionHistoryListHolder(v_header);
break;
case TYPE_LOAD_TRANS_DETAIL:
View v_header1 = inflater.inflate(R.layout.transaction_detail_layout, parent, false);
viewHolder = new TransactionDetailHolder(v_header1);
break;
}
return viewHolder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (getItemViewType(position) == TYPE_LOAD_TRANS) {
TransactionHistoryListHolder transsHolder = (TransactionHistoryListHolder) holder;
retriveAllTrans(transsHolder, position);
} else {
}
}
public void retriveAllTrans(final TransactionHistoryListHolder holder, final int position) {
final Transaction data = transactionList.get(position);
holder.txt_id.setText(data.getId());
holder.txt_date.setText(data.getDate());
holder.txt_trans_type.setText(data.getType());
holder.txt_balance.setText(data.getBalance());
holder.lay_row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getItemViewType(position);
}
});
}
@Override
public int getItemViewType(int position) {
Object obj = transactionList.get(position);
if (obj instanceof Transaction) {
return TYPE_LOAD_TRANS;
} else if (obj instanceof Transaction) {
return TYPE_LOAD_TRANS_DETAIL;
}
return super.getItemViewType(position);
}
@Override
public int getItemCount() {
return transactionList.size();
}
}
请帮忙。谢谢。
答案 0 :(得分:1)
我认为问题在于你的getItemView类型方法:
你应该这样做:
Transaction obj = transactionList.get(position);
if (obj.typeToDisp == 0) {
return TYPE_LOAD_TRANS;
} else if (obj.typeToDisp == 1) {
return TYPE_LOAD_TRANS_DETAIL;
}
return super.getItemViewType(position);
然后在你的onClickListener中,你只需要更改类型,然后调用notifyItemChanged,如下所示:
holder.lay_row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int type = transaction.get(holder.getAdapterPosition()).typeToDisp;
if (type == 0)
{
transaction.get(holder.getAdapterPosition()).typeToDisp = 1;
notifyItemChanged(holder.getAdapterPosition())
}
}
});
请注意,我使用holder.getAdapterPosition
,根据谷歌的推荐,你不应该使用职位作为最终职位,但让持有人最终并在听众中获得这样的职位