我有一个RecyclerViewAdapter
类来获取歌曲列表,当我触摸一首歌曲时,该歌曲的背景变为蓝色。问题是,如果我尝试触摸另一首歌,那首歌也会变成蓝色,但是我找不到解决方案来更改列表中先前触摸过的歌曲,从而将CardView
改回原来的位置,因此,在任何给定的触摸下,时间都会变成蓝色。
内:
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
final ViewHolder viewHolder = (ViewHolder) holder;
viewHolder.setImageResourceType(mList, position);
viewHolder.getTxtViewTitle().setText(mList.get(position).getTitle());
viewHolder.getTxtViewArtist().setText(mList.get(position).getArtist());
viewHolder.getTxtViewDuration().setText(MusicUtil.GetDurationHMS(mList.get(position).getDuration()));
viewHolder.getCardView().setCardBackgroundColor(Color.argb(0, 0, 0, 0));
viewHolder.getCardView().setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
mListener.onRecyclerViewItemClicked(viewHolder, position, view.getId());
viewHolder.getCardView().setCardBackgroundColor(Color.BLUE);
mLastPosition = position; // To be used to store last position clicked
}
});
}
答案 0 :(得分:1)
在适配器类中管理lastHolder
全局变量。
viewHolder.getCardView().setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
mListener.onRecyclerViewItemClicked(viewHolder, position, view.getId());
viewHolder.getCardView().setCardBackgroundColor(Color.BLUE);
mLastPosition = position; // To be used to store last position clicked
if(lastHolder!=null)
{
// do your task here which you want to do for previous selection
lastHolder.getCardView().setCardBackgroundColor(Color.WHITE);
}
lastHolder=viewHolder;
}
});