您好我在Dialog中构建了gridView库。我用复选框显示一些照片。现在我想在imageView点击后选中/取消选中复选框。现在它只检查我何时完全点击它。
这里有一些代码:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if(convertView == null)
{
convertView = LayoutInflater.from(mContext).inflate(R.layout.my_grid, parent, false);
holder=new ViewHolder();
holder.imageView=(ImageView)convertView.findViewById(R.id.imageView);
holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
convertView.setTag(holder);
}
else
holder=(ViewHolder)convertView.getTag();
holder.checkBox.setOnCheckedChangeListener(null);
holder.checkBox.setChecked(check[position]);
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int counter=0;
for(int i=0;i<check.length;i++)
if(check[i]) counter++;
numberCheckboxes=counter;
if (isChecked && numberCheckboxes<=2)
{
numberCheckboxes++;
check[position] = true;
Toast.makeText(mContext, mContext.getResources().getString(R.string.zaznaczono_zdj), Toast.LENGTH_SHORT).show();
}
else if (!isChecked&&position<check.length)
{
numberCheckboxes--;
check[position]=false;
}
if (numberCheckboxes>=4&&isChecked)
{
buttonView.setChecked(false);
numberCheckboxes--;
check[position]=false;
}
}
});
Glide.with(mContext)
.load(this.listFiles.get(position).getAbsolutePath()) //path to picture
.into(holder.imageView);
return convertView;
}
static class ViewHolder{
ImageView imageView;
CheckBox checkBox;
}
xml:
<?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:orientation="vertical">
<ImageView
android:layout_width="@integer/width"
android:layout_height="@integer/height"
android:adjustViewBounds="true"
android:id="@+id/imageView"
android:layout_margin="5dp"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true" />
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/imageView"
android:layout_alignParentTop="true"
android:layout_marginTop="5dp"
android:layout_marginEnd="2dp"
android:focusable="false"
android:scaleX="1.8"
android:scaleY="1.8" />
</RelativeLayout>
答案 0 :(得分:0)
使用:
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.checkBox.performClick();
}
});