RecyclerView将未选择的项目标记为已选择

时间:2016-04-04 09:09:41

标签: java android android-recyclerview

我的RecyclerView在滚动时重复标记的项目。

我的RecyclerView有10行实际显示!如果我点击第一个,它的背景会突出显示。如果我现在向下滚动,另一个项目会突出显示!

如果我向上滚动,第一项不再突出显示,而是另一项......

以下是问题的图片:

我的RecyclerViewAdapter:

public class SelectSongRecyclerViewAdapter extends RecyclerView.Adapter<SelectSongRecyclerViewAdapter.Holder> {
    private Song[] sSongs;
    private List<Song> selectedSongs;

    public SelectSongRecyclerViewAdapter(Song[] songs) {
        sSongs = songs;

        selectedSongs = new ArrayList<>();
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_selectsongsview, parent, false);

        Holder holder = new Holder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(final Holder holder, final int position) {
        //holder.imvSong.setImageResource(R.drawable.standardartwork);
        holder.txvSongTitle.setText(sSongs[position].getTitle());
        holder.txvSongInfo.setText(sSongs[position].getArtists());

        holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectedSongs.contains(sSongs[position])) {
                    selectedSongs.remove(sSongs[position]);
                    holder.linearLayout.setBackgroundResource(android.R.color.transparent);
                }
                else {
                    selectedSongs.add(sSongs[position]);
                    holder.linearLayout.setBackgroundResource(R.color.colorItemSelected);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return sSongs != null ? sSongs.length : 0;
    }

    public Song[] getSelectedSongs() {
        Song[] songs = new Song[selectedSongs.size()];

        return selectedSongs.toArray(songs);
    }

    public class Holder extends RecyclerView.ViewHolder {
        LinearLayout linearLayout;
        ImageView imvSong;
        TextView txvSongTitle;
        TextView txvSongInfo;

        public Holder(View layout) {
            super(layout);

            linearLayout = (LinearLayout) layout;

            imvSong = (ImageView) layout.findViewById(R.id.imvSong);
            txvSongTitle = (TextView) layout.findViewById(R.id.adap_txvSongtitle);
            txvSongInfo = (TextView) layout.findViewById(R.id.adap_txvSongInfo);
        }
    }
}

我希望你能帮助我! 谢谢!

5 个答案:

答案 0 :(得分:2)

这是我使用的工作代码!

当调用onBindViewHolder时,仅当项目位于selectedSongs - 列表中时才会被选中!

@Override
public void onBindViewHolder(final Holder holder, int position) {
    holder.txvSongTitle.setText(sSongs[position].getTitle());
    holder.txvSongInfo.setText(sSongs[position].getArtists());

    if (!selectedSongs.contains(sSongs[position])) {
        holder.linearLayout.setBackgroundResource(android.R.color.transparent);
    }
    else {
        holder.linearLayout.setBackgroundResource(R.color.colorItemSelected);
    }

    holder.linearLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int pos = holder.getAdapterPosition();System.out.println(sSongs[pos].getTitle());

            if (selectedSongs.contains(sSongs[pos])) {
                selectedSongs.remove(sSongs[pos]);

                holder.linearLayout.setBackgroundResource(android.R.color.transparent);
            }
            else {
                selectedSongs.add(sSongs[pos]);

                holder.linearLayout.setBackgroundResource(R.color.colorItemSelected);
            }
        }
    });
}

答案 1 :(得分:0)

这就是RecyclerView的工作原理: - 创建尽可能多的视图可以放入屏幕内,每次滚动时都将视图传递给onBindViewHolder,以便根据项目索引更新数据

现在,当单击其中一个视图时,您可以更改视图的背景颜色,滚动时可以使用backgroundColor(colorItemSelected)传递视图,只需更改它的数据。

你应该做的是onBindViewHolder如果选择了item,则将view的背景颜色设置为colorItemSelected,否则设置为透明。

这是一些合作

 public void onBindViewHolder(final Holder holder, final int position) {
    //holder.imvSong.setImageResource(R.drawable.standardartwork);
    holder.txvSongTitle.setText(sSongs[position].getTitle());
    holder.txvSongInfo.setText(sSongs[position].getArtists());
    if (selectedSongs.contains(sSongs[position])) {  
              holder.linearLayout.setBackgroundResource(R.color.colorItemSelected);
    }else {
holder.linearLayout.setBackgroundResource(android.R.color.transparent);
}

    holder.linearLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (selectedSongs.contains(sSongs[position])) {
                selectedSongs.remove(sSongs[position]);
                holder.linearLayout.setBackgroundResource(android.R.color.transparent);
            }
            else {
                selectedSongs.add(sSongs[position]);
                holder.linearLayout.setBackgroundResource(R.color.colorItemSelected);
            }
        }
    });
}

答案 2 :(得分:0)

试试这个适配器,

public class SelectSongRecyclerViewAdapter extends RecyclerView.Adapter<SelectSongRecyclerViewAdapter.Holder> { 
private Song[] sSongs;
private List<Song> selectedSongs;

public SelectSongRecyclerViewAdapter(Song[] songs) {
    sSongs = songs;

    selectedSongs = new ArrayList<>();
} 

@Override 
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_selectsongsview, parent, false);

    Holder holder = new Holder(view);

    return holder;
} 

@Override 
public void onBindViewHolder(final Holder holder, final int position) {
    holder.bind(position); 

} 

@Override 
public int getItemCount() { 
    return sSongs != null ? sSongs.length : 0;
} 

public Song[] getSelectedSongs() { 
    Song[] songs = new Song[selectedSongs.size()];

    return selectedSongs.toArray(songs);
} 

public class Holder extends RecyclerView.ViewHolder implements View.OnClickListener {
    LinearLayout linearLayout;
    ImageView imvSong;
    TextView txvSongTitle;
    TextView txvSongInfo;

    public Holder(View layout) {
        super(layout);

        linearLayout = (LinearLayout) layout;

        imvSong = (ImageView) layout.findViewById(R.id.imvSong);
        txvSongTitle = (TextView) layout.findViewById(R.id.adap_txvSongtitle);
        txvSongInfo = (TextView) layout.findViewById(R.id.adap_txvSongInfo);
        linearLayout.setOnClickListener(this);
    }

    public void bind(int position){
        txvSongTitle.setText(sSongs[position].getTitle());
        txvSongInfo.setText(sSongs[position].getArtists());

        if (selectedSongs.contains(sSongs[getAdapterPosition()])) {
            linearLayout.setBackgroundResource(android.R.color.transparent);
        }
        else {
            linearLayout.setBackgroundResource(R.color.colorItemSelected);
        }
    }

    @Override
    public void onClick(View v) {
        if (selectedSongs.contains(sSongs[getAdapterPosition()])) {
            selectedSongs.remove(sSongs[getAdapterPosition()]);
            holder.linearLayout.setBackgroundResource(android.R.color.transparent);
        }
        else {
            selectedSongs.add(sSongs[getAdapterPosition()]);
            holder.linearLayout.setBackgroundResource(R.color.colorItemSelected);
        }
    }
}

}

答案 3 :(得分:0)

将onBindViewHolder更改为此

    @Override
        public void onBindViewHolder(final Holder holder, final int position) {
            //holder.imvSong.setImageResource(R.drawable.standardartwork);
            holder.txvSongTitle.setText(sSongs[position].getTitle());
            holder.txvSongInfo.setText(sSongs[position].getArtists());

holder.linearLayout.setBackgroundResource(selectedSongs.contains(sSongs[position]) ? R.color.colorItemSelected : android.R.color.transparent);


            holder.linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (selectedSongs.contains(sSongs[position])) {
                        selectedSongs.remove(sSongs[position]);
                        notifyDataSetChanged();
                    }
                    else {
                        selectedSongs.add(sSongs[position]);
                        notifyDataSetChanged();
                    }
                }
            });
        }

答案 4 :(得分:0)

Java

private int position = -1;

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
  holder.itemView.setSelected(holder.getLayoutPosition() == this.position);
  holder.itemView.setOnClickListener(v -> {
     holder.itemView.setSelected(true);
     this.position = holder.getLayoutPosition();
  });
}

科特琳

val position = -1

fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
    holder.itemView.isSelected = holder.layoutPosition == this.position
    holder.itemView.setOnClickListener { v: View? ->
        holder.itemView.isSelected = true
        this.position = holder.layoutPosition
    }
}

item_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true">
        <shape>
            <solid android:color="@android:color/holo_blue_bright" />
        </shape>
    </item>

    <item>
        <shape>
            <solid android:color="@android:color/white" />
        </shape>
    </item>
</selector>