계발에서 개발까지
RecylerView and CheckBox , Delete
build.gradle 추가
dependencies {
implementation 'com.google.android.material:material:1.0.0-alpha1'
implementation "androidx.cardview:cardview:1.0.0"
implementation "androidx.recyclerview:recyclerview:1.1.0"
implementation "androidx.recyclerview:recyclerview-selection:1.1.0-rc01"
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cameraContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<CheckBox
android:id="@+id/check_btn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:background="#FFEB3B"
android:text="전체선택체크"
android:textSize="22dp" />
<Button
android:id="@+id/btn_delete"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:layout_weight="1"
android:background="#03A9F4"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:text="전체삭제"
android:textAllCaps="false"
android:textColor="#ffffff"
android:textSize="18dp" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none" />
</LinearLayout>
row_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="75dp"
android:layout_margin="5dp"
android:orientation="horizontal"
card_view:cardCornerRadius="5dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<CheckBox
android:id="@+id/chk_selected"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp" />
<TextView
android:id="@+id/txt_Name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/chk_selected"
android:layout_weight="1"
android:padding="10dp"
android:text="아이템 이름"
android:textColor="@color/black"
android:textSize="16dp" />
<ImageButton
android:id="@+id/btn_delete_unit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@null" />
</LinearLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>
MainActivity.java
package com.example.androdilistviewcheckbox;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.snackbar.Snackbar;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private CheckBox chk_select_all;
private Button btn_delete_all;
private ArrayList<Model> item_list = new ArrayList<>();
private ModelAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initControls();
}
private void initControls() {
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
chk_select_all = (CheckBox) findViewById(R.id.check_btn);
btn_delete_all = (Button) findViewById(R.id.btn_delete);
item_list.add(new Model("테스트1", false));
item_list.add(new Model("테스트2", false));
item_list.add(new Model("테스트3", false));
item_list.add(new Model("테스트4", false));
item_list.add(new Model("테스트5", false));
item_list.add(new Model("테스트6", false));
item_list.add(new Model("테스트7", false));
item_list.add(new Model("테스트8", false));
item_list.add(new Model("테스트9", false));
item_list.add(new Model("테스트10", false));
item_list.add(new Model("테스트11", false));
item_list.add(new Model("테스트12", false));
item_list.add(new Model("테스트13", false));
item_list.add(new Model("테스트14", false));
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new ModelAdapter(item_list);
recyclerView.setAdapter(mAdapter);
chk_select_all.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (chk_select_all.isChecked()) {
for (Model model : item_list) {
model.setSelected(true);
}
} else {
for (Model model : item_list) {
model.setSelected(false);
}
}
mAdapter.notifyDataSetChanged();
}
});
btn_delete_all.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (chk_select_all.isChecked()) {
item_list.clear();
mAdapter.notifyDataSetChanged();
chk_select_all.setChecked(false);
} else {
Snackbar.make(v, "Please click on select all check box, to delete all items.", Snackbar.LENGTH_LONG).show();
}
}
});
}
}
Model.java
package com.example.androdilistviewcheckbox;
import java.io.Serializable;
public class Model implements Serializable {
private String itemName;
private boolean isSelected;
public Model(String itemName, boolean isSelected) {
this.itemName = itemName;
this.isSelected = isSelected;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean selected) {
isSelected = selected;
}
}
ModelAdapter.java
package com.example.androdilistviewcheckbox;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class ModelAdapter extends RecyclerView.Adapter<ModelAdapter.ViewHolder> {
public ArrayList<Model> item_list;
public ModelAdapter(ArrayList<Model> arrayList) {
item_list = arrayList;
}
@Override
public ModelAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item, null);
// create ViewHolder
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(ModelAdapter.ViewHolder holder, int position) {
final int pos = position;
holder.item_name.setText(item_list.get(position).getItemName());
holder.chkSelected.setChecked(item_list.get(position).isSelected());
holder.chkSelected.setTag(item_list.get(position));
holder.chkSelected.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
Model model = (Model) cb.getTag();
model.setSelected(cb.isChecked());
item_list.get(pos).setSelected(cb.isChecked());
}
});
holder.btn_delete.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
deleteItemFromList(v, pos);
}
});
}
@Override
public int getItemCount() {
return item_list.size();
}
// confirmation dialog box to delete an unit
private void deleteItemFromList(View v, final int position) {
AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
//builder.setTitle("Dlete ");
builder.setMessage("Delete Item ?")
.setCancelable(false)
.setPositiveButton("CONFIRM",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
item_list.remove(position);
notifyDataSetChanged();
}
})
.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
builder.show();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView item_name;
public ImageButton btn_delete;
public CheckBox chkSelected;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
item_name = (TextView) itemLayoutView.findViewById(R.id.txt_Name);
btn_delete = (ImageButton) itemLayoutView.findViewById(R.id.btn_delete_unit);
chkSelected = (CheckBox) itemLayoutView.findViewById(R.id.chk_selected);
}
}
}
수고하셨습니다 ★
더 많은 정보 https://deumdroid.tistory.com/
'Android Studio' 카테고리의 다른 글
안드로이드 스튜디오 웹뷰( WebView)설정하기 (0) | 2020.03.13 |
---|---|
안드로이드 스튜디오 구글애드몹 네이티브 광고(Nativetemplates) 넣기 (1) | 2020.03.13 |
안드로이드 스튜디오 BottomSheetDialog 하단 다이얼로그 적용 (0) | 2020.03.06 |
안드로이드 스튜디오 OnClickListener 와 setOnLongClickListener 중복 사용 하기 (0) | 2020.03.06 |
안드로이드 스튜디오 ToolBar 적용하기 (0) | 2020.03.04 |