본문 바로가기
안드로이드 자바

[Android][Java] Recyclerview 다중선택

by teamnova 2023. 5. 19.
728x90

안녕하세요. 리사이클러뷰로 조회, 다중 선택을 해보겠습니다. 

 

- 최종 결과물

 

- Student.java

public class Student {
    private String name;
    private boolean isSelected;
    public Student(String name, boolean isSelected) {
        this.name = name;
        this.isSelected = isSelected;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public boolean isSelected() {
        return isSelected;
    }
    public void setSelected(boolean selected) {
        isSelected = selected;
    }


    @Override
    public String toString() {
        return
            "\n name='" + name +"/isSelected=" + isSelected +
            "\n}";
    }
}

 

- list_student.xml 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="28sp"
        android:textStyle="bold" />
</LinearLayout>

 

- StudentAdapter.java

public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.ViewHolder> {
    Context context;
    ArrayList<Student> arrayList;
    public StudentAdapter(Context context, ArrayList<Student> arrayList) {
        this.context = context;
        this.arrayList = arrayList;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.list_student, parent, false);
        return new ViewHolder(view);
    }
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Student student = arrayList.get(position);
        holder.textView.setText(student.getName());
        if(student.isSelected()){
            holder.itemView.setBackgroundColor(Color.parseColor("#FBE1B8"));
        }else{
            holder.itemView.setBackgroundColor(Color.parseColor("#FF9422"));
        }
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder{
        LinearLayout layout;
        TextView textView;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            layout = itemView.findViewById(R.id.layout);
            textView = itemView.findViewById(R.id.textView);
            layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    setMultipleSelection(getAdapterPosition());
                }
            });
        }
    }

    private void setMultipleSelection(int adapterPosition){
        if(arrayList.get(adapterPosition).isSelected()){
            arrayList.get(adapterPosition).setSelected(false);
        }else{
            arrayList.get(adapterPosition).setSelected(true);
        }
        Log.e("arrayList", arrayList.toString());
        notifyDataSetChanged();
    }
}

 

- main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/recyclerView"/>
</LinearLayout>

 

- MainActivity.java

public class MainActivity extends AppCompatActivity {
    RecyclerView recyclerView;
    ArrayList<Student> arrayList = new ArrayList<>();
    StudentAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = findViewById(R.id.recyclerView);
        adapter = new StudentAdapter(MainActivity.this, getData());
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerItemDecoration(this,
                LinearLayoutManager.VERTICAL));
        recyclerView.setAdapter(adapter);
    }
    private ArrayList<Student> getData(){
        for(int i = 0; i < 10; i++){
            arrayList.add(new Student(i +"번쨰 아이템", false));
        }
        return arrayList;
    }
}

 

어댑터의 setMultipleSelection() 함수가 핵심입니다. 

참고하여 해당 기능 만드시는 데 도움이 되길 바랍니다.