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() 함수가 핵심입니다.
참고하여 해당 기능 만드시는 데 도움이 되길 바랍니다.
'안드로이드 자바' 카테고리의 다른 글
[Android][JAVA] 반투명 로딩다이얼로그 구현하기 (0) | 2023.05.21 |
---|---|
[Android][JAVA]액티비티 Zoom In, Out 예제 (0) | 2023.05.20 |
[ Android][Java] 두 좌표(위도, 경도)간 거리 구하기 (0) | 2023.05.19 |
[Android][Java] GroupBarchart 만들기 (0) | 2023.05.17 |
[Android][Java] 온보딩 페이지 만들기 (0) | 2023.05.15 |