如何在点击视图时加载不同的视图类型?
How to load different view type on click of view?
我在 recyclerview 中显示了一份交易清单。我现在想在单击第一个视图类型的父布局时显示另一个视图。
我为两种视图类型创建了不同的布局和不同的支架。
我想知道如何显示第一个视图类型的父布局的第二个视图类型 onClick。
这是我的适配器:
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();
}
}
请帮忙。谢谢。
我认为问题出在您的 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
,根据 google 的建议,你不应该使用 position 作为 final,而是让 holder 为 final 并在监听器中获得这样的位置
我在 recyclerview 中显示了一份交易清单。我现在想在单击第一个视图类型的父布局时显示另一个视图。
我为两种视图类型创建了不同的布局和不同的支架。
我想知道如何显示第一个视图类型的父布局的第二个视图类型 onClick。
这是我的适配器:
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();
}
}
请帮忙。谢谢。
我认为问题出在您的 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
,根据 google 的建议,你不应该使用 position 作为 final,而是让 holder 为 final 并在监听器中获得这样的位置