How to Clear All Values in RecyclerView
EveryThing is Working With Recycler View But the problem is that, the Clear Button at the bottom i want to use that button to clear all selected, but as of now, if i select 59, and then press clear, the 05 will get auto selected and when i press clear again then it get’s cleared. One More thing the recylcerView is not rendering the layout exactly in center. i adjusted it using margins, is there any way to do so properly.
Activity Screenshot
PickNumberPojo
public class PickNumberPOJO { private int text; private boolean isChecked; public PickNumberPOJO() { } public String getText() { return (text < 10 ? "0" + text : String.valueOf(text)); } public void setText(int text) { this.text = text; } public boolean isChecked() { return isChecked; } public void setChecked(boolean checked) { isChecked = checked; } }
PickNumbersDataSet
public class PickNumbersDataSet { public static ArrayList<PickNumberPOJO> getPicNumberDataSet(int count) { ArrayList<PickNumberPOJO> arrayList = new ArrayList<>(); for (int i = 0; i < count; i++) { PickNumberPOJO pickNumberPOJO = new PickNumberPOJO(); pickNumberPOJO.setText(i+1); pickNumberPOJO.setChecked(false); arrayList.add(pickNumberPOJO); } return arrayList; } }
PickNumberAdapter
public class PickNumberAdapter extends RecyclerView.Adapter<PickNumberAdapter.ViewHolder> { Context context; ArrayList<PickNumberPOJO> pickNumberPOJOS; LayoutInflater inflater; public PickNumberAdapter(Context context, ArrayList<PickNumberPOJO> pickNumberPOJOS) { this.context = context; this.pickNumberPOJOS = pickNumberPOJOS; inflater = LayoutInflater.from(context); } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View v = inflater.inflate(R.layout.gridview_layout, parent, false); // set the view's size, margins, padding's and layout parameters return new ViewHolder(v); } @Override public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) { holder.numTextView.setText(pickNumberPOJOS.get(position).getText()); holder.numTextView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (pickNumberPOJOS.get(position).isChecked()) { pickNumberPOJOS.get(position).setChecked(false); v.setBackgroundResource(R.drawable.circle_light_black); } else if (getSelectedCounts() > 6) { Toast.makeText(context, "You Can't Select More Than 6", Toast.LENGTH_SHORT).show(); } else { pickNumberPOJOS.get(position).setChecked(true); v.setBackgroundResource(R.drawable.circle_light_color_filled); } } }); } // Returns Count Of All Selected Items In Boolean Array (Selections) private int getSelectedCounts() { int selectedCounts = 1; for (PickNumberPOJO pickNumberPOJO : pickNumberPOJOS) { if (pickNumberPOJO.isChecked()) selectedCounts += 1; } return selectedCounts; } public void setPickNumberPOJOS(ArrayList<PickNumberPOJO> pickNumberPOJOS) { this.pickNumberPOJOS = pickNumberPOJOS; notifyDataSetChanged(); } @Override public int getItemCount() { return pickNumberPOJOS.size(); } public static class ViewHolder extends RecyclerView.ViewHolder { TextView numTextView; public ViewHolder(@NonNull View itemView) { super(itemView); numTextView = (TextView) itemView.findViewById(R.id.textView); } } }
PickNumbers Activity
public class PickNumbers extends AppCompatActivity { ActivityPickNumbersBinding binding; PickNumberAdapter pickNumberAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = DataBindingUtil.setContentView(this, R.layout.activity_pick_numbers); pickNumberAdapter = new PickNumberAdapter(this, PickNumbersDataSet.getPicNumberDataSet(59)); binding.pickNumbersRecyclerView.setAdapter(pickNumberAdapter); binding.pickNumbersRecyclerView.setLayoutManager(new GridLayoutManager(this,7)); binding.clearSelectionsBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { pickNumberAdapter.setPickNumberPOJOS(PickNumbersDataSet.getPicNumberDataSet(59)); } }); } }
activity_pick_numbers.xml
<?xml version="1.0" encoding="utf-8"?> <layout 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"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".activities.pick_numbers.activity.PickNumbers"> <androidx.cardview.widget.CardView android:id="@+id/pick_new_numbers_action_bar_card" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" app:cardCornerRadius="0dp" app:cardElevation="8dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorPrimary"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:padding="@dimen/sdp_10" android:text="@string/choose_6_numbers" android:textColor="@color/colorWhite" android:textSize="@dimen/ssp_20" /> <Button android:layout_width="@dimen/sdp_30" android:layout_height="@dimen/sdp_30" android:layout_gravity="center" android:layout_marginEnd="@dimen/sdp_10" android:background="@drawable/ic_cancel" android:textColor="@color/colorPrimary" android:textStyle="bold" /> </LinearLayout> </androidx.cardview.widget.CardView> <androidx.recyclerview.widget.RecyclerView android:id="@+id/pick_numbers_recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="8dp" android:layout_marginStart="20dp" android:layout_above="@id/pick_new_numbers_footer_layout" android:layout_below="@id/pick_new_numbers_action_bar_card"> </androidx.recyclerview.widget.RecyclerView> <LinearLayout android:id="@+id/pick_new_numbers_footer_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentEnd="true" android:layout_alignParentBottom="true" android:background="@color/colorOrange"> <Button android:id="@+id/clearSelectionsBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="@dimen/sdp_10" android:layout_marginTop="@dimen/sdp_10" android:layout_marginEnd="@dimen/sdp_5" android:layout_marginBottom="@dimen/sdp_10" android:layout_weight="1" android:background="@drawable/btn_back_only_white_stroke" android:text="@string/clear" android:textColor="@color/colorWhite" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="@dimen/sdp_5" android:layout_marginTop="@dimen/sdp_10" android:layout_marginEnd="@dimen/sdp_10" android:layout_marginBottom="@dimen/sdp_10" android:layout_weight="1" android:background="@drawable/btn_back_primary" android:text="@string/add_line" android:textColor="@color/colorWhite" /> </LinearLayout> </RelativeLayout> </layout>
gridview_layout
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="8dp"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:gravity="center" android:textStyle="bold" android:background="@drawable/circle_light_black" android:textColor="@color/colorPrimary" android:adjustViewBounds="true" /> </RelativeLayout>