我正在使用RecyclerView
来显示商品列表。我有两个TextViews
一个低于另一个。最初,第二个TextView
设置为singleline = true
。现在点击项目时,我正在设置singleline = false
。这只是为了在点击时展开它(如ExpandableList
)。代码工作正常但问题是,当RecyclerView
使用回收项时,其他项TextView值也被设置为singleline = false
。现在我该如何避免这种情况?
代码
public class InboxAdapter extends RecyclerView.Adapter<InboxAdapter.InboxViewHolder> {
private Context context;
private List<InboxModel> listInbox;
private InboxModel currentItem;
public InboxAdapter(Context context, List<InboxModel> listInbox) {
this.context = context;
this.listInbox = listInbox;
}
@Override
public InboxViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.custom_inbox_item, parent, false);
InboxViewHolder viewHolder = new InboxViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(final InboxViewHolder holder, int position) {
currentItem = listInbox.get(position);
holder.tvHeader.setText(currentItem.header);
holder.tvMsg.setText(currentItem.msg);
holder.tvHeader.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.tvMsg.setSingleLine(false);
LinearLayout.LayoutParams lp= (LinearLayout.LayoutParams) holder.tvMsg.getLayoutParams();
lp=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
holder.tvMsg.setLayoutParams(lp);
}
});
}
@Override
public int getItemCount() {
return listInbox.size();
}
class InboxViewHolder extends RecyclerView.ViewHolder {
private TextView tvImageHeader;
private TextView tvHeader;
private TextView tvMsg;
private TextView tvDate;
public InboxViewHolder(View itemView) {
super(itemView);
tvImageHeader = (TextView) itemView.findViewById(R.id.tv_img_header);
tvHeader = (TextView) itemView.findViewById(R.id.tv_header);
tvMsg = (TextView) itemView.findViewById(R.id.tv_msg);
tvDate = (TextView) itemView.findViewById(R.id.tv_date);
}
}
答案 0 :(得分:2)
您必须在所有情况中设置属性,以避免回收项目出现问题。
@Override
public void onBindViewHolder(final InboxViewHolder holder, int position){
//....
if (currentItem.myBoolean){
holder.tvMsg.setSingleLine(true);
} else {
holder.tvMsg.setSingleLine(false);
}
//...
}
在您的点击事件中,只需更改对象内的值(而不是视图!)。 Somenthing喜欢:
holder.tvHeader.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentItem.myBoolean = true;
//call the notify !
}
});
如果此状态是独占的,则不使用项目内的变量,而是使用适配器内的变量(InboxModel checkedItem)。
答案 1 :(得分:1)
嗯,为此你必须在onBindViewHolder()中保存点击位置和写条件,如下所示
@Override
public void onBindViewHolder(final InboxViewHolder holder, int position) {
if(Postion==Mysavedposition){
holder.tvMsg.setSingleLine(false);
}else{
holder.tvMsg.setSingleLine(true);
}
currentItem = listInbox.get(position);
holder.tvHeader.setText(currentItem.header);
holder.tvMsg.setText(currentItem.msg);
holder.tvHeader.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Mysavedposition=position;
holder.tvMsg.setSingleLine(false);
LinearLayout.LayoutParams lp= (LinearLayout.LayoutParams) holder.tvMsg.getLayoutParams();
lp=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
holder.tvMsg.setLayoutParams(lp);
}
});
}