CheckBoxRecyclerActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="com.example.bhaumik.demo.CheckBoxRecyclerActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view_fruits"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="@dimen/_40dp"/>
<Button
android:id="@+id/btn_select_fruit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Selected Fruits"
android:textAllCaps="false"
android:layout_alignParentBottom="true"
android:background="#00BCD4"
android:textColor="#fff" />
</RelativeLayout>
item_fruits.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_fruit_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@+id/empName"
android:text="TextView"
android:layout_weight="1"
android:textColor="#000"
android:padding="@dimen/_20dp"
android:layout_toLeftOf="@+id/checkBox"
android:layout_toStartOf="@+id/checkBox" />
<TextView
android:id="@+id/tv_fruit_price"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textColor="#000"
android:layout_weight="1"
android:layout_toLeftOf="@+id/checkBox"
android:layout_toStartOf="@+id/checkBox"
android:text="TextView" />
<CheckBox
android:id="@+id/checkBox_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text=""
android:layout_marginRight="@dimen/_10dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
Add String Resource
<string-array name="item_fruits">
<item>Apple</item>
<item>Banana</item>
<item>Mango</item>
<item>Orange</item>
<item>Cherry</item>
<item>Grape</item>
<item>Coconut</item>
</string-array>
<string-array name="item_price">
<item>100</item>
<item>40</item>
<item>55</item>
<item>25</item>
<item>80</item>
<item>120</item>
<item>15</item>
</string-array>
Fruits.java
package com.example.bhaumik.demo.pojo;
import java.io.Serializable;
/**
* Created by Bhaumik on 8/25/2017.
*/
public class Fruits implements Serializable {
String name;
String price;
boolean isSelected;
public Fruits(String name, String price, boolean isSelected) {
this.name = name;
this.price = price;
this.isSelected = isSelected;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean selected) {
isSelected = selected;
}
}
FruitsAdapter.java
package com.example.bhaumik.demo.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
import com.example.bhaumik.demo.R;import com.example.bhaumik.demo.pojo.Fruits;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Bhaumik on 8/25/2017.
*/
public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.FruitHolder>{
Context context;
List<Fruits> list = new ArrayList<>();
public FruitAdapter(Context context, List<Fruits> list) {
this.context = context;
this.list = list;
}
@Override
public FruitHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_fruits,parent,false);
return new FruitHolder(view);
}
@Override
public void onBindViewHolder(final FruitHolder holder, final int position) {
Fruits fruits = list.get(position);
holder.tv_name.setText(fruits.getName());
holder.tv_price.setText(fruits.getPrice());
holder.checkBox.setChecked(fruits.isSelected());
holder.checkBox.setTag(list.get(position));
holder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Fruits fruits1 = (Fruits)holder.checkBox.getTag();
fruits1.setSelected(holder.checkBox.isChecked());
list.get(position).setSelected(holder.checkBox.isChecked());
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public static class FruitHolder extends RecyclerView.ViewHolder{
TextView tv_name,tv_price;
CheckBox checkBox;
public FruitHolder(View itemView) {
super(itemView);
tv_name = itemView.findViewById(R.id.tv_fruit_name);
tv_price = itemView.findViewById(R.id.tv_fruit_price);
checkBox = itemView.findViewById(R.id.checkBox_select);
}
}
public List<Fruits> getFruitsList(){
return list;
}
}
CheckBoxRecyclerActivity.java
package com.example.bhaumik.demo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.example.bhaumik.demo.adapter.FruitAdapter;import com.example.bhaumik.demo.pojo.Fruits;
import java.util.ArrayList;
import java.util.List;
public class CheckBoxRecyclerActivity extends AppCompatActivity {
FruitAdapter adapter;
List<Fruits> list = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_box_recycler);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view_fruits);
Button btn_select = (Button) findViewById(R.id.btn_select_fruit);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
String[] fruits = getResources().getStringArray(R.array.item_fruits);
String[] price = getResources().getStringArray(R.array.item_price);
for (int i=0;i<6;i++){
Fruits fruits1 = new Fruits(fruits[i],price[i],false);
list.add(fruits1);
}
adapter = new FruitAdapter(CheckBoxRecyclerActivity.this,list);
recyclerView.setAdapter(adapter);
btn_select.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String data = "";
List<Fruits> list_fruit = adapter.getFruitsList();
for (int i=0;i<list_fruit.size();i++){
Fruits fruits2 = list_fruit.get(i);
if (fruits2.isSelected() == true){
data = data + "\n" + fruits2.getName().toString() + " " + fruits2.getPrice().toString();
}
}
Toast.makeText(CheckBoxRecyclerActivity.this,"Your Fruits Selected : \n " + data,Toast.LENGTH_SHORT)
.show();
}
});
}
}
0 comments:
Post a Comment