如何在 android 中通过单击 ImageView 在适配器列表视图中对整数数组使用 setVisibility
How to use setVisibility on integer array in adapter list view by onclick on ImageView in android
我试图通过使用适配器列表视图将可绘制对象中的图像存储到整数数组中来在单击图像时隐藏它,但我无法获取它。这是我正在使用的代码:
当我点击一张图片时,它应该 invisible.I 正在将图片存储在 int 数组中并应用 setVisibilty invisible 但它不 working.i 希望图片显示在屏幕中央,而另一张图片单击应该得到 invisible.i 我正在尝试将图像存储在整数数组中并在适配器中设置它 list.i 我正在调用此函数
imageIDs[position].setVisible(false);
Integer[] imageIDs = {
R.drawable.c2,
R.drawable.c3,
R.drawable.c4,
R.drawable.c5,
R.drawable.c6,
R.drawable.c7,
R.drawable.c8
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Note that Gallery view is deprecated in Android 4.1---
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
//Adapter list
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
//onclick event
public void onItemClick(AdapterView<?> parent, View v, int position,long id)
{//displaying image clicked i am trying to invisible this pic when click
Toast.makeText(getBaseContext(),"pic" + (position + 1) + " selected",//dispplpaying msg
Toast.LENGTH_SHORT).show();
//imageIDs[position].setVisible(false);
// display the images selected
ImageView imageView = (ImageView) findViewById(R.id.image1);
imageView.setImageResource(imageIDs[position]);
//setting image on screen from using xml
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context context;
private int itemBackground;
public ImageAdapter(Context c)
{
context = c;
// sets a grey background; wraps around the images
TypedArray a =obtainStyledAttributes(R.styleable.MyGallery);
itemBackground = a.getResourceId(R.styleable.MyGallery_android_galleryItemBackground, 0);
a.recycle();
}
// returns the number of images
public int getCount() {
return imageIDs.length;
}
// returns the ID of an item
public Object getItem(int position) {
return position;
}
// returns the ID of an item
public long getItemId(int position) {
return position;
}
// returns an ImageView view
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
//imageIDs[position].setVisible(false);
//i am trying it here but its not working
imageView.setImageResource(imageIDs[position]);
imageView.setLayoutParams(new Gallery.LayoutParams(100, 100));
imageView.setBackgroundResource(itemBackground);
return imageView;
}
}
}
我假设您正在尝试使用此代码:
//imageIDs[position].setVisible(false);
如果是这样,那么您正在做的是在没有该方法的 Integer 上调用 setVisible。您需要做的是获得对显示图像的 ImageView 的引用,然后对其调用 setVisibility(View.INVISIBLE) 或 setVisibility(View.GONE)。
另外,您似乎正在尝试将图像设置为不可见,但随后您将相同的资源放回 ImageView,所以我不确定您要在那里做什么。
我试图通过使用适配器列表视图将可绘制对象中的图像存储到整数数组中来在单击图像时隐藏它,但我无法获取它。这是我正在使用的代码:
当我点击一张图片时,它应该 invisible.I 正在将图片存储在 int 数组中并应用 setVisibilty invisible 但它不 working.i 希望图片显示在屏幕中央,而另一张图片单击应该得到 invisible.i 我正在尝试将图像存储在整数数组中并在适配器中设置它 list.i 我正在调用此函数
imageIDs[position].setVisible(false);
Integer[] imageIDs = {
R.drawable.c2,
R.drawable.c3,
R.drawable.c4,
R.drawable.c5,
R.drawable.c6,
R.drawable.c7,
R.drawable.c8
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Note that Gallery view is deprecated in Android 4.1---
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
//Adapter list
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
//onclick event
public void onItemClick(AdapterView<?> parent, View v, int position,long id)
{//displaying image clicked i am trying to invisible this pic when click
Toast.makeText(getBaseContext(),"pic" + (position + 1) + " selected",//dispplpaying msg
Toast.LENGTH_SHORT).show();
//imageIDs[position].setVisible(false);
// display the images selected
ImageView imageView = (ImageView) findViewById(R.id.image1);
imageView.setImageResource(imageIDs[position]);
//setting image on screen from using xml
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context context;
private int itemBackground;
public ImageAdapter(Context c)
{
context = c;
// sets a grey background; wraps around the images
TypedArray a =obtainStyledAttributes(R.styleable.MyGallery);
itemBackground = a.getResourceId(R.styleable.MyGallery_android_galleryItemBackground, 0);
a.recycle();
}
// returns the number of images
public int getCount() {
return imageIDs.length;
}
// returns the ID of an item
public Object getItem(int position) {
return position;
}
// returns the ID of an item
public long getItemId(int position) {
return position;
}
// returns an ImageView view
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
//imageIDs[position].setVisible(false);
//i am trying it here but its not working
imageView.setImageResource(imageIDs[position]);
imageView.setLayoutParams(new Gallery.LayoutParams(100, 100));
imageView.setBackgroundResource(itemBackground);
return imageView;
}
}
}
我假设您正在尝试使用此代码:
//imageIDs[position].setVisible(false);
如果是这样,那么您正在做的是在没有该方法的 Integer 上调用 setVisible。您需要做的是获得对显示图像的 ImageView 的引用,然后对其调用 setVisibility(View.INVISIBLE) 或 setVisibility(View.GONE)。
另外,您似乎正在尝试将图像设置为不可见,但随后您将相同的资源放回 ImageView,所以我不确定您要在那里做什么。