我试图通过使用适配器列表视图将图像从可绘制图像存储到整数数组中来使图像单击时不可见,但是我无法获取它。这是我正在使用的代码:

当我单击图像时,它应该不可见。我将图像存储在int数组中并应用setVisibilty不可见,但它不起作用。我希望图像显示在屏幕中央,被单击的图像应该不可见。试图将图像存储在整数数组中并将其设置在适配器列表中。我正在调用此函数


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中,因此我不确定您要在此处执行的操作。

07-26 09:43