我正在使用回收站视图来显示包含12个项目的列表。一次可以看到3到4个项目。我在每个卡片行中显示了更多按钮。如果我点击显示第一张卡片行的更多按钮,当我向下滚动显示另一张卡片行的更多按钮将自动被选中。为什么会发生这种情况,我该如何解决这个问题。
以下是我如何设置适配器的代码:
ToadlineAdapter toadlineAdapter = new ToadlineAdapter(mContext, getData1());
recyclerView.setAdapter(toadlineAdapter);
toadlineAdapter.setClickListener(this);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
我的适配器代码:
public class ToadlineAdapter extends RecyclerView.Adapter<ToadlineAdapter.MyViewHolder> implements ToadlineCommentAdapter.ClickListener {
private ClickListener clickListener;
private SwipeRefreshLayout.OnRefreshListener clickListener1;
private LayoutInflater inflater;
Context mContext;
List<TimelineDataStore> data = Collections.EMPTY_LIST;
public ToadlineAdapter(Context context, List<TimelineDataStore> data) {
inflater = LayoutInflater.from(context);
this.data = data;
mContext = context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.single_card_row, parent, false);
MyViewHolder viewHolder = new MyViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
TimelineDataStore current = data.get(position);
if (clickedPosition.equals("post")) {
holder.postHeader.setText(current.postHeader);
holder.postDescription.setText(current.postDescription);
}
}
@Override
public int getItemCount() {
return data.size();
}
public void setClickListener(ClickListener clickListener) {
this.clickListener = clickListener;
}
public void setClickListener1(SwipeRefreshLayout.OnRefreshListener clickListener1) {
this.clickListener1 = clickListener1;
}
@Override
public void itemClicked(View view, int position) {
}
// View Holder object for Recycler View
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener, ToadlineCommentAdapter.ClickListener {
TextView postHeader, postDescription;
public MyViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener((View.OnClickListener) this);
postHeader = (TextView) itemView.findViewById(R.id.textViewTitle);
postDescription = (TextView) itemView.findViewById(R.id.textViewPostDescription);
mCardView = (CardView) itemView.findViewById(R.id.card_view);
mShowMore = (ImageButton) itemView.findViewById(R.id.buttonSeeMore);
mShowLess = (ImageButton) itemView.findViewById(R.id.buttonSeeLess);
mShowMore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int clickedPosition = getAdapterPosition();
TimelineDataStore current = data.get(clickedPosition);
current.postDescription = data.get(clickedPosition).postDescription;
postDescription.setVisibility(View.VISIBLE);
pollPostImages.setVisibility(View.VISIBLE);
mShowMore.setVisibility(View.GONE);
mShowLess.setVisibility(View.VISIBLE);
}
});
mShowLess.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
postDescription.setVisibility(View.GONE);
pollPostImages.setVisibility(View.GONE);
mShowMore.setVisibility(View.VISIBLE);
mShowLess.setVisibility(View.GONE);
}
});
}
答案 0 :(得分:0)
每行都有相同的OnClickListener
,因为您在ViewHolder
类中设置了它。
实现您想要的行为的一种方法是在OnClickListener
方法中设置OnBindViewHolder
:
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
TimelineDataStore current = data.get(position);
holder.mShowMore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int clickedPosition = getAdapterPosition();
TimelineDataStore current = data.get(clickedPosition);
current.postDescription = data.get(clickedPosition).postDescription;
postDescription.setVisibility(View.VISIBLE);
pollPostImages.setVisibility(View.VISIBLE);
mShowMore.setVisibility(View.GONE);
mShowLess.setVisibility(View.VISIBLE);
}
holder.mShowLess.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
postDescription.setVisibility(View.GONE);
pollPostImages.setVisibility(View.GONE);
mShowMore.setVisibility(View.VISIBLE);
mShowLess.setVisibility(View.GONE);
}
}