Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java RecyclerView将未选中的项目标记为选中_Java_Android_Android Recyclerview - Fatal编程技术网

Java RecyclerView将未选中的项目标记为选中

Java RecyclerView将未选中的项目标记为选中,java,android,android-recyclerview,Java,Android,Android Recyclerview,My RecyclerView在滚动时重复标记的项目 我的RecyclerView实际显示了10行!如果我点击第一个,它的背景会突出显示。如果我现在向下滚动,另一项将突出显示 如果我向上滚动,第一个项目将不再突出显示,但另一个项目 以下是问题的图像: 任何我的RecycleServiceAdapter: public class SelectSongRecyclerViewAdapter extends RecyclerView.Adapter<SelectSongRecyclerView

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

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

如果我向上滚动,第一个项目将不再突出显示,但另一个项目

以下是问题的图像:

任何我的RecycleServiceAdapter:

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);
        }
    }
}
公共类SelectSongRecycleServiceAdapter扩展了RecyclerView.Adapter{ 私人宋体; 私人列表选择的歌曲; 公共选择歌曲回收服务适配器(歌曲[]首歌曲){ 宋=歌曲; selectedSongs=new ArrayList(); } @凌驾 public Holder onCreateViewHolder(视图组父级,int-viewType){ View-View=LayoutFlater.from(parent.getContext())。充气(R.layout.adapter\u选择SongsView,parent,false); 支架=新支架(视图); 报税表持有人; } @凌驾 公共无效onBindViewHolder(最终持有人,最终整数位置){ //holder.imvSong.setImageResource(R.drawable.standardartwork); holder.txvSongTitle.setText(sSongs[position].getTitle()); holder.txvSongInfo.setText(sSongs[position].getArtists()); holder.linearLayout.setOnClickListener(新视图.OnClickListener(){ @凌驾 公共void onClick(视图v){ 如果(selectedSongs.contains(sSongs[position])){ 选择歌曲。删除(歌曲[位置]); holder.linearLayout.setBackgroundResource(android.R.color.transparent); } 否则{ 选择歌曲。添加(歌曲[位置]); holder.linearLayout.setBackgroundResource(R.color.colorItemSelected); } } }); } @凌驾 public int getItemCount(){ 返回SSONG!=null?SSONG.length:0; } 公共歌曲[]getSelectedSongs(){ 歌曲[]歌曲=新歌[selectedSongs.size()]; 将所选歌曲返回至阵列(歌曲); } 公共类持有者扩展了RecyclerView.ViewHolder{ 线性布局线性布局; ImageView imvSong; TextView txvSongTitle; TextView txvSongInfo; 公众持有人(视图布局){ 超级(布局); 线性布局=(线性布局)布局; imvSong=(ImageView)layout.findviewbyd(R.id.imvSong); txvSongTitle=(TextView)layout.findviewbyd(R.id.adap_txvSongTitle); txvSongInfo=(TextView)layout.findviewbyd(R.id.adap_txvSongInfo); } } } 我希望你能帮助我!
谢谢

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

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

您应该做的是onBindViewHolder将视图的背景色设置为colorItemSelected(如果项目已选中),或者将其设置为透明(如果未选中)

这是一些co

 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);
            }
        }
    });
}

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

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

您应该做的是onBindViewHolder将视图的背景色设置为colorItemSelected(如果项目已选中),或者将其设置为透明(如果未选中)

这是一些co

 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);
            }
        }
    });
}

这是我使用的工作代码

调用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);
            }
        }
    });
}

这是我使用的工作代码

调用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);
            }
        }
    });
}
试试这个适配器

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);
        }
    }
}
公共类SelectSongRecycleServiceAdapter扩展了RecyclerView.Adapter{ 私人宋体; 私人列表选择的歌曲; 公共选择歌曲回收服务适配器(歌曲[]首歌曲){ 宋=歌曲; selectedSongs=new ArrayList(); } @凌驾 public Holder onCreateViewHolder(视图组父级,int-viewType){ View-View=LayoutFlater.from(parent.getContext())。充气(R.layout.adapter\u选择SongsView,parent,false); 支架=新支架(视图); 报税表持有人; } @凌驾 公共无效onBindViewHolder(最终持有人,最终整数位置){ 固定器。绑定(位置); } @凌驾 public int getItemCount(){ 返回SSONG!=null?SSONG.length:0; } 公共歌曲[]getSelectedSongs(){ 歌曲[]歌曲=新歌[selectedSongs.size()]; 将所选歌曲返回至阵列(歌曲); } 公共类持有者扩展了RecyclerView.ViewHolder实现了View.OnClickListener{ 线性布局线性布局; ImageView imvSong; TextView txvSongTitle; TextView txvSongInfo; 公众持有人(视图布局){ 超级(布局); 线性布局=(线性布局)布局; imvSong=(ImageView)layout.findviewbyd(R.id.imvSong); txvSongTitle=(TextView)layout.findviewbyd(R.id.adap_txvSongTitle); txvSongInfo=(TextView)layout.findviewbyd(R.id.adap_txvSongInfo); linearLayout.setOnClickListener(此); } 公共无效绑定(int位置){ txvSongTitle.setText(sSongs[position].getTitle()); txvSongInfo.setText(sSongs[position].getArtists()); if(selectedSongs.contains(sSongs[getAdapterPosition()])){ linearLayout.setBackgroundResource(android.R.color.transparent); } 否则{ linearLayout.setBackgroundResource(R.color.colorItemSelected); } } @凌驾 P
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
    }
}
<?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>