EveryThing работает с представлением Recycler. Но проблема в том, что кнопка «Очистить» внизу, я хочу использовать эту кнопку, чтобы очистить все выбранное, но на данный момент, если я выберу 59, а затем нажмите «Очистить», 05 будет получить автоматический выбор, и когда я снова нажимаю очистить, он очищается. Еще одна вещь recylcerView не отображает макет точно по центру. Я отрегулировал его, используя поля, есть ли способ сделать это правильно.
Скриншот активности
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 getPicNumberDataSet(int count) {
ArrayList 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 {
Context context;
ArrayList pickNumberPOJOS;
LayoutInflater inflater;
public PickNumberAdapter(Context context, ArrayList 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 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"?>
android: text = "@ string / add_line" android: textColor = "@ color / colorWhite" />
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>