Skip to content

Commit 51e81ee

Browse files
committed
Checkbox RecyclerView
1 parent f56b605 commit 51e81ee

40 files changed

+1127
-2
lines changed

CheckBoxRecyclerView/.gitignore

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/caches
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
/.idea/navEditor.xml
9+
/.idea/assetWizardSettings.xml
10+
.DS_Store
11+
/build
12+
/captures
13+
.externalNativeBuild
14+
.cxx

CheckBoxRecyclerView/app/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

CheckBoxRecyclerView/app/build.gradle

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 29
5+
buildToolsVersion "29.0.0"
6+
defaultConfig {
7+
applicationId "com.codificador.checkboxrecyclerview"
8+
minSdkVersion 22
9+
targetSdkVersion 29
10+
versionCode 1
11+
versionName "1.0"
12+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
13+
}
14+
buildTypes {
15+
release {
16+
minifyEnabled false
17+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
18+
}
19+
}
20+
}
21+
22+
dependencies {
23+
implementation fileTree(dir: 'libs', include: ['*.jar'])
24+
implementation 'androidx.appcompat:appcompat:1.0.2'
25+
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
26+
testImplementation 'junit:junit:4.12'
27+
androidTestImplementation 'androidx.test:runner:1.2.0'
28+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
29+
implementation 'androidx.recyclerview:recyclerview:1.1.0'
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.codificador.checkboxrecyclerview;
2+
3+
import android.content.Context;
4+
5+
import androidx.test.platform.app.InstrumentationRegistry;
6+
import androidx.test.ext.junit.runners.AndroidJUnit4;
7+
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
11+
import static org.junit.Assert.*;
12+
13+
/**
14+
* Instrumented test, which will execute on an Android device.
15+
*
16+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
17+
*/
18+
@RunWith(AndroidJUnit4.class)
19+
public class ExampleInstrumentedTest {
20+
@Test
21+
public void useAppContext() {
22+
// Context of the app under test.
23+
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
24+
25+
assertEquals("com.codificador.checkboxrecyclerview", appContext.getPackageName());
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.codificador.checkboxrecyclerview">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:roundIcon="@mipmap/ic_launcher_round"
10+
android:supportsRtl="true"
11+
android:theme="@style/AppTheme">
12+
<activity android:name=".ItemActivity"></activity>
13+
<activity android:name=".MainActivity">
14+
<intent-filter>
15+
<action android:name="android.intent.action.MAIN" />
16+
17+
<category android:name="android.intent.category.LAUNCHER" />
18+
</intent-filter>
19+
</activity>
20+
</application>
21+
22+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package com.codificador.checkboxrecyclerview;
2+
3+
import android.content.Context;
4+
import android.text.Editable;
5+
import android.text.TextWatcher;
6+
import android.view.LayoutInflater;
7+
import android.view.View;
8+
import android.view.ViewGroup;
9+
import android.widget.CheckBox;
10+
import android.widget.CompoundButton;
11+
import android.widget.EditText;
12+
import android.widget.TextView;
13+
14+
import java.util.ArrayList;
15+
16+
import androidx.annotation.NonNull;
17+
import androidx.recyclerview.widget.RecyclerView;
18+
19+
public class Adapter extends RecyclerView.Adapter<Adapter.ItemHolder>{
20+
21+
ArrayList<Item> items;
22+
LayoutInflater lInflater;
23+
24+
public Adapter(ArrayList<Item> items, Context context){
25+
this.items = items;
26+
lInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
27+
}
28+
29+
@NonNull
30+
@Override
31+
public ItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
32+
View view = lInflater.inflate(R.layout.item_row, parent, false);
33+
ItemHolder itemHolder = new ItemHolder(view);
34+
return itemHolder;
35+
}
36+
37+
public ArrayList<Item> getSelectedItems(){
38+
ArrayList<Item> result = new ArrayList<>();
39+
for(Item item : items){
40+
if(item.isSelected())
41+
result.add(item);
42+
}
43+
return result;
44+
}
45+
46+
@Override
47+
public void onBindViewHolder(@NonNull ItemHolder holder, int position) {
48+
holder.checkBox.setChecked(items.get(position).isSelected());
49+
if(items.get(position).getQty() > 0)
50+
holder.editText.setText(items.get(position).getQty()+"");
51+
holder.textViewName.setText(items.get(position).getName());
52+
holder.checkBox.setTag(position);
53+
holder.checkBox.setOnCheckedChangeListener(null);
54+
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
55+
@Override
56+
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
57+
int position = (int) compoundButton.getTag();
58+
System.out.println("Changed Pos : "+position+" "+b);
59+
items.get(position).setSelected(b);
60+
}
61+
});
62+
CustomTextWatcher customTextWatcher = (CustomTextWatcher) holder.editText.getTag();
63+
if(customTextWatcher != null){
64+
holder.editText.removeTextChangedListener(customTextWatcher);
65+
}
66+
CustomTextWatcher newWatcher = new CustomTextWatcher(position);
67+
holder.editText.addTextChangedListener(newWatcher);
68+
}
69+
70+
class CustomTextWatcher implements TextWatcher{
71+
72+
int position;
73+
public CustomTextWatcher(int pos){
74+
position = pos;
75+
}
76+
77+
@Override
78+
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
79+
80+
}
81+
82+
@Override
83+
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
84+
85+
}
86+
87+
@Override
88+
public void afterTextChanged(Editable editable) {
89+
System.out.println(position+" "+editable.toString());
90+
if(editable.length() > 0)
91+
items.get(position).setQty(Integer.parseInt(editable.toString()));
92+
}
93+
}
94+
95+
@Override
96+
public int getItemCount() {
97+
return items.size();
98+
}
99+
100+
@Override
101+
public long getItemId(int position) {
102+
return position;
103+
}
104+
105+
@Override
106+
public int getItemViewType(int position) {
107+
return position;
108+
}
109+
110+
class ItemHolder extends RecyclerView.ViewHolder{
111+
TextView textViewName;
112+
CheckBox checkBox;
113+
EditText editText;
114+
115+
public ItemHolder(@NonNull View itemView) {
116+
super(itemView);
117+
setIsRecyclable(false);
118+
checkBox = itemView.findViewById(R.id.cb);
119+
editText = itemView.findViewById(R.id.editText);
120+
textViewName = itemView.findViewById(R.id.textItem);
121+
}
122+
}
123+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.codificador.checkboxrecyclerview;
2+
3+
import java.io.Serializable;
4+
5+
public class Item implements Serializable {
6+
7+
private boolean selected;
8+
private int qty;
9+
private int id;
10+
private String name;
11+
12+
public Item(){
13+
selected = false;
14+
qty = 0;
15+
}
16+
17+
public int getQty() {
18+
return qty;
19+
}
20+
21+
public void setQty(int qty) {
22+
this.qty = qty;
23+
}
24+
25+
public int getId() {
26+
return id;
27+
}
28+
29+
public void setId(int id) {
30+
this.id = id;
31+
}
32+
33+
public String getName() {
34+
return name;
35+
}
36+
37+
public void setName(String name) {
38+
this.name = name;
39+
}
40+
41+
public boolean isSelected() {
42+
return selected;
43+
}
44+
45+
public void setSelected(boolean selected) {
46+
this.selected = selected;
47+
}
48+
}

0 commit comments

Comments
 (0)