我已经用图像,标题和复选框填充了recyclerView。 我有两个问题。
如何在单击imageview或整个回收项目时选中复选框。
我必须通过从recyclerview获取所有选中的项目来进行下一个活动。
我的布局:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_gravity="center_horizontal"
android:contentDescription="Interests"
android:scaleType="centerCrop"
android:src="@drawable/ic_yash_dp" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_gravity="bottom"
android:layout_margin="5dp"
android:layout_marginTop="24dp"
android:background="@drawable/rounded_corners"
android:gravity="bottom"
android:padding="5dp"
android:text="GYM"
android:textAlignment="center"
android:textColor="@color/white" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkBox"
android:layout_margin="2dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
我的适配器:
@Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
final InterestBean model = arrayList.get(position);
final int pos = position;
RecyclerViewHolder mainHolder = (RecyclerViewHolder) holder;// holder
Bitmap image = BitmapFactory.decodeResource(context.getResources(),
model.getImage());// This will convert drawbale image into bitmap
// setting title
mainHolder.title.setText(model.getTitle());
mainHolder.imageview.setImageBitmap(image);
mainHolder.checkBox.setChecked(arrayList.get(position).isSelected());
mainHolder.checkBox.setTag(arrayList.get(position));
mainHolder.checkBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
InterestBean contact = (InterestBean) cb.getTag();
contact.setIsSelected(cb.isChecked());
arrayList.get(pos).setIsSelected(cb.isChecked());
selectedItems.add(pos);
Toast.makeText(
v.getContext(), pos + cb.isChecked(), Toast.LENGTH_LONG).show();
}
});}
答案 0 :(得分:3)
您可以使用Model类来获取recyclerView检查的项目。更多参考: Android Recyclerview checkbox
模型
public class Model {
private boolean isSelected;
private String animal;
public String getAnimal() {
return animal;
}
public void setAnimal(String animal) {
this.animal = animal;
}
public boolean getSelected() {
return isSelected;
}
public void setSelected(boolean selected) {
isSelected = selected;
}
}
适配器
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 java.util.ArrayList;
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
private LayoutInflater inflater;
public static ArrayList<Model> imageModelArrayList;
private Context ctx;
public CustomAdapter(Context ctx, ArrayList<Model> imageModelArrayList) {
inflater = LayoutInflater.from(ctx);
this.imageModelArrayList = imageModelArrayList;
this.ctx = ctx;
}
@Override
public CustomAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.rv_item, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(final CustomAdapter.MyViewHolder holder, int position) {
holder.checkBox.setText("Checkbox " + position);
holder.checkBox.setChecked(imageModelArrayList.get(position).getSelected());
holder.tvAnimal.setText(imageModelArrayList.get(position).getAnimal());
// holder.checkBox.setTag(R.integer.btnplusview, convertView);
holder.checkBox.setTag(position);
holder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Integer pos = (Integer) holder.checkBox.getTag();
Toast.makeText(ctx, imageModelArrayList.get(pos).getAnimal() + " clicked!", Toast.LENGTH_SHORT).show();
if (imageModelArrayList.get(pos).getSelected()) {
imageModelArrayList.get(pos).setSelected(false);
} else {
imageModelArrayList.get(pos).setSelected(true);
}
}
});
}
@Override
public int getItemCount() {
return imageModelArrayList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
protected CheckBox checkBox;
private TextView tvAnimal;
public MyViewHolder(View itemView) {
super(itemView);
checkBox = (CheckBox) itemView.findViewById(R.id.cb);
tvAnimal = (TextView) itemView.findViewById(R.id.animal);
}
}
答案 1 :(得分:0)
1)您可以添加mainHolder.imageview OnClickListener
2)两步:
代码:
<?php
echo $this->Form->submit("Create Schedule", array("id" => "createSchedule", "class" => "btn btn-success"));
echo $this->Form->end();
?>
答案 2 :(得分:0)
只需添加recyclelerview的两个覆盖方法
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return position;
}
答案 3 :(得分:0)
我在onBindViewHolder方法中添加了checkbox listenner,复选框可以点击。
viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int pos = viewHolder.getAdapterPosition();
ChoiceItem currentItem = mChoice.get(pos);
if(viewHolder.checkBox.isChecked()){
onItemCheckListener.onItemCheck(currentItem);
currentItem.setCheck(true);
}else{
onItemCheckListener.onItemUncheck(currentItem);
currentItem.setCheck(false);
}
}
});
答案 4 :(得分:0)
wso2
答案 5 :(得分:0)
这对我有用,对您也一样 只需创建静态列表,我就在MainActivity.java中创建了它
public static List<String> myItems = new ArrayList<>();
在YouAdapter.class中创建setOnCheckedChangeListener
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b){
myItems.add(Model.getYouItem());
}
else{
myItems.remove(Model.getYouItem());
}
}
});
现在,您可以使用myItems列表执行操作了。
答案 6 :(得分:0)
首先在穆迪尔类中设置一个布尔值
并单击复选框将布尔值设置为true
通过以下方法并选中了所有复选框,是否选中了
fun isListContainMethod(arraylist: List<Puzzle>): Boolean {
for (puzzle in arraylist) {
if (!puzzle.isSelect) {
return false
}
}
return true
}