[英]Changing the background of selected items in recyclerView Android (Java)
我的 recyclerView 中有一個線性布局,我想在每個項目上實現點擊事件,當我選擇第一個項目時應該更改第一個項目的線性布局的背景,當我再次按下它時返回第一個背景,我只想選擇多個項目,不僅有人可以幫我解決這個問題!!!!
這是我的適配器代碼
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);
}
}
這是我的自定義列表 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>
在 MyAdapter 中,為所選項目添加一個字段,因為 adkr 是一個字符串數組:
Set<String> selectedItems = new HashSet();
然后正如@REX 所說,向 MyViewHolder 添加一個 onClick 偵聽器,但我會改變方法。 我會添加一個綁定方法,所以它看起來像這樣。
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);
}
}
像這樣綁定它:
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.bind(adkr[position], position);
}
這將允許您跟蹤選擇的內容並適當地顯示它。
您可以在查看器中添加 clicklistener,它將檢測點擊事件。 代碼更改將是這樣的:
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.