我正在尝试为图像按钮创建一个OnClick侦听器,它应该在单击时删除位图(将其设置为null)。但是,当我按下按钮时没有任何反应,除了它打印我的“Log.d()”消息,并且点击后位图IS实际上为空,但是没有显示更改。代码写在我的适配器中。我甚至尝试将它插入runOnUIThread中以查看是否会产生影响,但是没有任何变化。 我也尝试了setImageDrawable(null)和setImageResource(0)没有运气。我甚至尝试更改其他UI元素,但这也不起作用。我唯一能做的就是打印日志信息......
这是我的onView方法的getView:
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.item_create_product_gridview_normal, parent, false);
//Set up viewholder
viewHolder = new ViewHolder();
viewHolder.productImageIv = (ImageView) convertView.findViewById(R.id.android_gridview_image);
viewHolder.addImageIb = (ImageButton) convertView.findViewById(R.id.addImageButton);
viewHolder.removeImageIb = (ImageButton) convertView.findViewById(R.id.removeImageButton);
viewHolder.productImageIv.setImageBitmap(images[position]);
checkForNullImages(viewHolder, position);
//Store the holder with the view
convertView.setTag(viewHolder);
viewHolder.removeImageIb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
runOnUiThread (new Thread(new Runnable() {
public void run() {
images[position] = null;
viewHolder.productImageIv.setImageBitmap(images[position]);
notifyDataSetChanged();
//Does print this message, item is null, but image is still shown
Log.e("LogMSG", "item should be null " + getItem(position));
}
}));
}
});
}
return convertView;
}
如果相关,这是我对应的XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:id="@+id/android_custom_gridview_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
app:cardCornerRadius="10dp"
app:cardBackgroundColor="@color/white">
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
app:cardCornerRadius="10dp">
<ImageView
android:id="@+id/android_gridview_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/gradient_background"
android:scaleType="centerCrop" />
</android.support.v7.widget.CardView>
</android.support.v7.widget.CardView>
<ImageButton
android:id="@+id/addImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="-30dp"
android:elevation="@dimen/standard_12"
android:background="@drawable/icon_add" />
<ImageButton
android:id="@+id/removeImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="-30dp"
android:background="@drawable/icon_remove"
android:elevation="@dimen/standard_12" />
答案 0 :(得分:1)
这是因为您的行视图回收不正确。您没有使用以下代码回收视图:
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
// ...
}
return convertView;
}
因为如果它不为空,你就不会使用之前的convertView。
因此,将代码更改为以下内容:
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.item_create_product_gridview_normal, parent, false);
//Set up viewholder
holder = new ViewHolder();
holder.productImageIv = (ImageView) convertView.findViewById(R.id.android_gridview_image);
holder.addImageIb = (ImageButton) convertView.findViewById(R.id.addImageButton);
holder.removeImageIb = (ImageButton) convertView.findViewById(R.id.removeImageButton);
// set click listener here.
// don't use runOnUiThread for click listener.
// bind the view here.
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// set the value for view here
holder.productImageIv.setImageBitmap(images[position]);
return convertView;
}