如何使用RecyclerView中当前项目的视图?

时间:2019-04-02 13:42:31

标签: java android android-recyclerview

我有一个歌曲列表,当用户单击某个项目时,我希望该项目将更改其颜色(TextView)。我有听众,但是当我单击时,一项会更改其颜色,但有些项也会更改。救救我。

适配器:

public class SongsRecyclerViewAdapter
    extends RecyclerView.Adapter<SongsRecyclerViewAdapter.SongsViewHolder> {

private Context mContext;
private OnItemClickListener mOnItemClickListener;
private List<Song> mList = new ArrayList<>();

public SongsRecyclerViewAdapter(Context context) {
    mContext = context;
}

@NonNull
@Override
public SongsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(mContext).inflate(
            R.layout.item_song,
            viewGroup,
            false);
    return new SongsViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull SongsViewHolder songsViewHolder, int i) {
    songsViewHolder.mImage.setImageBitmap(mList.get(i).getImage());
    songsViewHolder.mTextTitle.setText(mList.get(i).getTitle());
    songsViewHolder.mTextArtist.setText(mList.get(i).getArtist());
}

@Override
public int getItemCount() {
    return mList.size();
}

class SongsViewHolder extends RecyclerView.ViewHolder {

    private TextView mTextTitle, mTextArtist;
    private ImageView mImage;
    private RelativeLayout mLayout;

    SongsViewHolder(@NonNull View itemView) {
        super(itemView);

        mLayout = itemView.findViewById(R.id.item_song_layout);
        mTextTitle = itemView.findViewById(R.id.text_item_song_title);
        mTextArtist = itemView.findViewById(R.id.text_item_song_artist);
        mImage = itemView.findViewById(R.id.image_button_item_song);

        mLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClickListener(mList.get(
                            getAdapterPosition()),
                            getAdapterPosition());
                }
            }
        });
    }

}

public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
    mOnItemClickListener = onItemClickListener;
}

public interface OnItemClickListener {
    void onItemClickListener(Song song, int position);
}

}

MainActivity:

 mAdapter.setOnItemClickListener(new SongsRecyclerViewAdapter.OnItemClickListener() {
        @Override
        public void onItemClickListener(Song song, int position) {
            markItem(position, -1);
        }
    });
private void markItem(int item, int old) {
    TextView title = mRecyclerView.findViewHolderForAdapterPosition(item).itemView
            .findViewById(R.id.text_item_song_title);
    TextView artist = mRecyclerView.findViewHolderForAdapterPosition(item).itemView
            .findViewById(R.id.text_item_song_artist);

    // mark a new item
    title.setTextColor(getResources().getColor(R.color.colorAccent));
    artist.setTextColor(getResources().getColor(R.color.colorAccent));

    // remark an old item
    if (old != -1) {
        TextView titleOld = mRecyclerView.findViewHolderForAdapterPosition(old).itemView
                .findViewById(R.id.text_item_song_title);
        TextView artistOld = mRecyclerView.findViewHolderForAdapterPosition(old).itemView
                .findViewById(R.id.text_item_song_artist);

        titleOld.setTextColor(getResources().getColor(R.color.colorWhite));
        artistOld.setTextColor(getResources().getColor(R.color.colorWhiteDark));
    }
}

我想您会帮助我弄清楚那里发生的事情) THNX。

1 个答案:

答案 0 :(得分:0)

您应在onBindViewHolder中执行点击侦听器

songsViewHolder.mLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClickListener(
                   //you have the position which is "i" in your case, you can get what you need from your list 
                );
            }
        }
    });