Moulesh Moulesh - 10 days ago 4
Android Question

Android Display and hide check boxes on all the rows of recyclerview

i have a recyclerview and i need to display check boxes on clicking action bar menu like shown here show check boxes

and like this to check all and hide all
hide/selectall

How can i achieve something like this?? I tried to call a adapter method from my activity and set visiblity of checkbox to visible and then calling

notifyDataSetChanged()
but it displays checkbox in only one row.. here is my adapter code

public class CartItemListAdapter extends RecyclerView.Adapter<CartItemListAdapter.MyViewHolder> {

private List<CartItemDetail> cartItemDetailList;
private Context context;
private CheckBox checkbox;



public CartItemListAdapter (List<CartItemDetail> cartItemDetailList){
this.cartItemDetailList=cartItemDetailList;
}


public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView cartProductName, totalValue,leftValue;
private ImageView cartProductImage, incrementButton,decrementButton;
private EditText coinValue;




public MyViewHolder(View view) {
super(view);
cartProductImage=(ImageView)view.findViewById(R.id.cart_product_image);
incrementButton=(ImageView)view.findViewById(R.id.cartIncrementImg);
decrementButton=(ImageView)view.findViewById(R.id.cartDecrementImg);
coinValue=(EditText)view.findViewById(R.id.cartBetCoinValue);
cartProductName=(TextView)view.findViewById(R.id.cart_product_name);
totalValue=(TextView)view.findViewById(R.id.cart_ProductTotal_qty);
leftValue=(TextView)view.findViewById(R.id.cart_ProductRemaining_qty);
checkbox=(CheckBox)view.findViewById(R.id.cart_item_checkbox);
}

@Override
public void onClick(View v) {

}

}



@Override
public CartItemListAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

context=parent.getContext();

View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.cart_item_row, parent, false);

itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
callProductEventDetails();
}
});


return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(CartItemListAdapter.MyViewHolder holder, int position) {
CartItemDetail cartItemDetail=cartItemDetailList.get(position);
holder.cartProductName.setText(cartItemDetail.getCartProductName());
holder.leftValue.setText(Html.fromHtml("<pre>" + "Left:" + "</pre>" + "<font color=\"#EF4836\">" + cartItemDetail.getProductLeftQty() + "</font>"));
holder.totalValue.setText("Total:"+cartItemDetail.getProductTotalQty());
holder.coinValue.setText(cartItemDetail.getAddedCoins());


Glide.with(context)
.load(cartItemDetail.getCartProductImageUrl())
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.fitCenter()
.into(holder.cartProductImage);
}

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

private void callProductEventDetails(){
Intent intent= new Intent(context, ProductEventDetailActivity.class);
intent.putExtra("eventStatus","inProgress");
context.startActivity(intent);
}

public void displayCheckBox(){
checkbox.setVisibility(View.VISIBLE);
notifyDataSetChanged();
}
}


xml for item row..

<?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"
android:layout_marginBottom="1dp"
android:elevation="0dp"
app:cardElevation="0dp">
<LinearLayout
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true">
<CheckBox
android:visibility="gone"
android:padding="-25dp"
android:gravity="center"
android:id="@+id/cart_item_checkbox"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleX="0.40"
android:scaleY="0.40"
android:button="@drawable/cart_checkbox_selector"/>
<ImageView
android:layout_marginLeft="10dp"
android:id="@+id/cart_product_image"
android:layout_gravity="center_vertical"
android:layout_width="80dp"
android:layout_height="80dp" />
<RelativeLayout
android:padding="10dp"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content">
<TextView
android:id="@+id/cart_product_name"
android:layout_width="wrap_content"
android:maxLines="2"
android:ellipsize="end"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#1d1d1d"
android:text="iPhone7 128G xxxxxxxxxxx xxx"/>

<TextView
android:id="@+id/cart_ProductTotal_qty"
android:layout_marginTop="15dp"
android:layout_below="@+id/cart_product_name"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total:750"/>

<TextView
android:layout_alignParentRight="true"
android:layout_alignBaseline="@+id/cart_ProductTotal_qty"
android:id="@+id/cart_ProductRemaining_qty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:text="Left:95"/>
<LinearLayout
android:layout_below="@+id/cart_ProductTotal_qty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal"
android:background="@drawable/counter_stroke_border">
<ImageView
android:id="@+id/cartDecrementImg"
android:layout_margin="10dp"
android:layout_gravity="center_vertical"
android:layout_width="15dp"
android:layout_height="15dp"
android:background="@drawable/minus_decrement"/>
<View
android:layout_width="0.6dp"
android:layout_height="match_parent"
android:background="#8d8d8d"></View>
<EditText
android:id="@+id/cartBetCoinValue"
android:layout_width="70dp"
android:layout_gravity="center_vertical"
android:focusableInTouchMode="true"
android:layout_height="wrap_content"
android:textColor="#EF4836"
android:ellipsize="start"
android:gravity="center_horizontal"
android:text="1"
android:inputType="number"
android:singleLine="true"
android:background="@android:color/transparent"/>
<View
android:layout_width="0.6dp"
android:layout_height="match_parent"
android:background="#8d8d8d"></View>
<ImageView
android:id="@+id/cartIncrementImg"
android:layout_margin="10dp"
android:layout_gravity="center_vertical"
android:layout_width="15dp"
android:layout_height="15dp"
android:background="@drawable/plus_increment"/>
</LinearLayout>
</RelativeLayout>

</LinearLayout>
</android.support.v7.widget.CardView>

Answer

Modify your code as follows :

@Override
    public void onBindViewHolder(...) {
    ...
    ...
    if(//codition for visibility) {
        checkbox.setVisibility(View.VISIBLE);
    } else {
        checkbox.setVisibility(View.GONE);
    }
}

Inside the Activity modify the dataset ie., cartItemDetailList and call yourAdapter.notifyDataSetChanged();