简体   繁体   English

更改 recyclerView Android (Java) 中选定项目的背景

[英]Changing the background of selected items in recyclerView Android (Java)

I have a linearlayout in my recyclerView and i waant to implement on click event on every item, when i select the first item should change the background of the linearlayout of the first item and when i pressed it again returns the first background , i just want to select multiple items not only one can anyone help me with this!!!!我的 recyclerView 中有一个线性布局,我想在每个项目上实现点击事件,当我选择第一个项目时应该更改第一个项目的线性布局的背景,当我再次按下它时返回第一个背景,我只想选择多个项目,不仅有人可以帮我解决这个问题!!!!

here is my adapter code这是我的适配器代码

public class MyAdapter extends 
RecyclerView.Adapter<MyAdapter.MyViewHolder> {


String[] adkr;
    String[] repeat;
        Animation anim;
            Context co;

public MyAdapter(Context context, String[] all_adkars, String[] all_repeat) {

    co = context;
        adkr = all_adkars;
            repeat = all_repeat;

}


@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    LayoutInflater layoutInflater = LayoutInflater.from(co);
        View view = layoutInflater.inflate(R.layout.custom_list, parent, false);

    return new MyViewHolder(view);
}


@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

    holder.textView.setText(adkr[position]);
        holder.repeat_Tv.setText(repeat[position]);
            setAnimation(holder.itemView,position);

}

@Override
public int getItemCount() {
    return adkr.length;
}


public class MyViewHolder extends RecyclerView.ViewHolder {

    TextView textView, repeat_Tv;
        LinearLayout layout;


    public MyViewHolder(@NonNull View itemView) {
        super(itemView);

        textView = itemView.findViewById(R.id.textView);
            repeat_Tv = itemView.findViewById(R.id.repeat);
                layout = itemView.findViewById(R.id.ll_bg);

    }


}

private void setAnimation(View viewToAnimate, int position) {


    anim = AnimationUtils.loadAnimation(co, R.anim.list_anim);
    viewToAnimate.startAnimation(anim);


}

} }

Here is my custom list xml code这是我的自定义列表 xml 代码

 <?xml version="1.0" encoding="utf-8"?> 
 <androidx.constraintlayout.widget.ConstraintLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:paddingBottom="20dp"
 android:id="@+id/rl_item"
>



<androidx.cardview.widget.CardView
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardCornerRadius="20dp"
    android:layout_marginTop="20dp"
    android:layout_marginBottom="20dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <LinearLayout
        android:id="@+id/ll_bg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/gradient"
        android:orientation="vertical"
        android:padding="10dp">


        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/shorooq"
            android:textAlignment="center"
            android:layout_margin="5dp"
            android:textColor="#CCFFFFFF"
            android:textSize="20sp"
     />

 <Button
 android:layout_width="match_parent"
 android:layout_height="2dp"
 android:background="@drawable/repeat"
 android:layout_marginTop="15dp"
 />

        <TextView
            android:id="@+id/repeat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/shorooq"
            android:textAlignment="center"
            android:layout_gravity="center"
            android:layout_margin="5dp"
            android:textColor="#BFBB8763"
            android:textSize="25sp"
       />



    </LinearLayout>
  </androidx.cardview.widget.CardView>
  </androidx.constraintlayout.widget.ConstraintLayout>

In MyAdapter, add a field for selected items, since adkr is an array of strings:在 MyAdapter 中,为所选项目添加一个字段,因为 adkr 是一个字符串数组:

Set<String> selectedItems = new HashSet();

Then as @REX says, add an onClick listener to the MyViewHolder but I would alter the approach.然后正如@REX 所说,向 MyViewHolder 添加一个 onClick 侦听器,但我会改变方法。 I would add a bind method, so it looks like this.我会添加一个绑定方法,所以它看起来像这样。

public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView textView, repeat_Tv;
        LinearLayout layout;
        int position;
        String value;


        public MyViewHolder(@NonNull View itemView) {
            super(itemView);

            textView = itemView.findViewById(R.id.textView);
            repeat_Tv = itemView.findViewById(R.id.repeat);
            layout = itemView.findViewById(R.id.ll_bg);
        }

        public void bind(String value, int position){
            textView.setText(value);
            repeat_Tv.setText(value);
            if(selected.contains(value)){
                // background for selected
            } else {
                // background for unselected
            }
            this.value = value;
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            if(selected.contains(value)){
                selected.remove(value);
            } else {
                selected.add(value);
            }
            notifyItemChanged(position);
        }
    }

Bind it like this:像这样绑定它:

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    holder.bind(adkr[position], position);
}

This will allow you to track what is selected and show it appropriately.这将允许您跟踪选择的内容并适当地显示它。

You can add clicklistener in your viewholder which will detect the on click events.您可以在查看器中添加 clicklistener,它将检测点击事件。 code changes will be something like this:代码更改将是这样的:

public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    TextView textView, repeat_Tv;
    LinearLayout layout;


    public MyViewHolder(@NonNull View itemView) {
        super(itemView);

        textView = itemView.findViewById(R.id.textView);
        repeat_Tv = itemView.findViewById(R.id.repeat);
        layout = itemView.findViewById(R.id.ll_bg);
        layout.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        // change background of layout here
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM