0

我有 3 个图库图片。当首先单击按钮时,它会显示第一组画廊,然后我单击它会显示第二组画廊。单击第二个按钮时如何删除第一组图库,单击第一个按钮时如何删除第二组图库。

代码:

图片:

Integer[] imageIDs = {

             R.drawable.nat,
                R.drawable.nat1,
                R.drawable.nat2,
                R.drawable.nat3,
                R.drawable.nat4,
                R.drawable.nat5,
                R.drawable.nat6,
                R.drawable.nat7


            };

     Integer[] imageIDs1 = {
             R.drawable.tree,
                R.drawable.tree1,
                R.drawable.tree2,
                R.drawable.tree3,
                R.drawable.tree4,
                R.drawable.tree5,
                R.drawable.tree6,
                R.drawable.tree7
            };

第一组图像的 ImageAdapter:

public class ImageAdapter extends BaseAdapter
        {
        Context context;
        int itemBackground;


        public ImageAdapter(Context c)
          {
            context = c;
            //---setting the style---
           // TypedArray a = obtainStyledAttributes(R.styleable.Gallery1); 
           // itemBackground = a.getResourceId(
                               //   R.styleable.Gallery1_android_galleryItemBackground, 0); 

          //  a.recycle();
          }


        //---returns the number of images---
        public int getCount() {
          return imageIDs.length;
         }

        //---returns the 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---

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            ImageView imageView;
              if (convertView == null) {
                imageView = new ImageView(context);
                 //set the ImageView to display image in array
                 // at user selected position
                imageView.setImageResource(imageIDs[position]);  
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                imageView.setLayoutParams(new Gallery.LayoutParams(150, 120)); 

               // imageView.setLayoutParams(new Gallery.LayoutParams(300, 300));

              } else {
                imageView = (ImageView) convertView;
              } 

              imageView.setBackgroundResource(itemBackground);
              return imageView;
        }
    }

第二组图像的 ImageAdapter:

 public class ImageAdapter1 extends BaseAdapter
        {
        Context context;
        int itemBackground;


        public ImageAdapter1(Context c)
          {
            context = c;
            //---setting the style---
           // TypedArray a = obtainStyledAttributes(R.styleable.Gallery1); 
           // itemBackground = a.getResourceId(
                               //   R.styleable.Gallery1_android_galleryItemBackground, 0); 

          //  a.recycle();
          }


        //---returns the number of images---
        public int getCount() {
          return imageIDs1.length;
         }

        //---returns the 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---

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            ImageView imageView;
              if (convertView == null) {
                imageView = new ImageView(context);
                 //set the ImageView to display image in array
                 // at user selected position
                imageView.setImageResource(imageIDs1[position]);  
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                imageView.setLayoutParams(new Gallery.LayoutParams(150, 120)); 

               // imageView.setLayoutParams(new Gallery.LayoutParams(300, 300));

              } else {
                imageView = (ImageView) convertView;
              } 

              imageView.setBackgroundResource(itemBackground);
              return imageView;
        }
    }

ContextMenu 按钮单击:

 popup_but = (Button) findViewById(R.id.popup_but_id);

            registerForContextMenu(popup_but);

            popup_but.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    openContextMenu(v);

                }



            });

@Override
     public void onCreateContextMenu(ContextMenu menu, View v,
       ContextMenuInfo menuInfo) {
      // TODO Auto-generated method stub
      super.onCreateContextMenu(menu, v, menuInfo);
      getMenuInflater().inflate(R.menu.main, menu);
         menu.setHeaderTitle("Nature");
     }

ContextMenu 点击事件:

     @Override
     public boolean onContextItemSelected(MenuItem item) {
      // TODO Auto-generated method stub
      switch (item.getItemId()) {
      case R.id.nat:

        //  gallery1.setEnabled(false);

           gallery=(Gallery)findViewById(R.id.gall1);

        //  gallery.setEnabled(false);

            gallery.setAdapter(new ImageAdapter(MainActivity.this));

            gallery.setOnItemClickListener(new OnItemClickListener()
            {

                @Override
                public void onItemClick(
                        AdapterView<?> parent, View v,
                        int position, long id) {
                    // TODO Auto-generated method stub

                    final ImageView imageView = (ImageView) findViewById(R.id.image1);
                    imageView.setImageResource(imageIDs[position]);
 return true;
                        }
                    });
break;

case R.id.tree:

        //  gallery1.setEnabled(false);

           gallery1=(Gallery)findViewById(R.id.gall2);

        //  gallery1.setEnabled(false);

            gallery1.setAdapter(new ImageAdapter1(MainActivity.this));

            gallery1.setOnItemClickListener(new OnItemClickListener()
            {

                @Override
                public void onItemClick(
                        AdapterView<?> parent, View v,
                        int position, long id) {
                    // TODO Auto-generated method stub

                    final ImageView imageView = (ImageView) findViewById(R.id.image2);
                    imageView.setImageResource(imageIDs[position]);
 return true;
                        }
                    });
break;
4

1 回答 1

0

编写一个方法来根据情况(即按钮单击)将输入列表更改为您的适配器并调用adapter.notifyDataSetChanged()

于 2013-06-24T06:23:05.793 回答