更改在Android中单击的按钮的图库视图

时间:2011-11-10 06:36:52

标签: android button android-gallery

我有三个按钮图像,视频,音频。我想在点击相应的按钮时用音频,视频,图像资源更改图库中的视图,以便只有按钮的相关图像出现在图库中。

我的代码在这里:

XML布局:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Audios" >
        </Button>

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Videos" >
        </Button>

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Images" >
        </Button>
    </LinearLayout>

    <Gallery
        android:id="@+id/Gallery01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </Gallery>
</LinearLayout>

<ImageView
    android:id="@+id/ImageView01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
</ImageView>

Java文件:

public class GalleryView extends Activity {
Integer[] pics = {
        R.drawable.cloudy,
        R.drawable.hazy,
        R.drawable.mostlycloudyday,
        R.drawable.partlycloud,
        R.drawable.sunny,
        R.drawable.sunrain,
        R.drawable.thunderstorm,
        R.drawable.weathercloudy,
    };
ImageView imageView;
Button mAudio;
Button mVideo;
Button mImages;
/** Called when the activity is first created. */
@Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mAudio=(Button) findViewById(R.id.button1);
    Gallery ga = (Gallery)findViewById(R.id.Gallery01);
    ga.setAdapter(new ImageAdapter(this));

    imageView = (ImageView)findViewById(R.id.ImageView01);
    ga.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
                long arg3) {
            Toast.makeText(getBaseContext(), 
                    "You have selected picture " + (pos+1) +  "of Gallery", 
                    Toast.LENGTH_SHORT).show();
            imageView.setImageResource(pics[pos]);

        }

    });

}


public class ImageAdapter extends BaseAdapter {

    private Context ctx;
    int imageBackground;

    public ImageAdapter(Context c) {
        ctx = c;
        TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
        imageBackground =  ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
        ta.recycle();
    }

    @Override
    public int getCount() {

        return pics.length;
    }

    @Override
    public Object getItem(int arg0) {

        return arg0;
    }

    @Override
    public long getItemId(int arg0) {

        return arg0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        ImageView iv = new ImageView(ctx);
        iv.setImageResource(pics[arg0]);
        iv.setScaleType(ImageView.ScaleType.FIT_XY);
        iv.setLayoutParams(new Gallery.LayoutParams(150,120));
        iv.setBackgroundResource(imageBackground);
        return iv;
    }

 }
}

任何人请帮助我,

感谢。

2 个答案:

答案 0 :(得分:1)

要更改库中的图像,必须更改数组中的Resources图像ID。例如,在 int [] video_pics 中更改视频的图像,依此类推。

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;

public class GalleryView extends Activity implements OnClickListener {
    int[] image_pics = { R.drawable.cloudy, R.drawable.hazy, R.drawable.mostlycloudyday, R.drawable.partlycloud,
            R.drawable.sunny, R.drawable.sunrain, R.drawable.thunderstorm, R.drawable.weathercloudy, };

    int[] video_pics = { R.drawable.cloudy, R.drawable.hazy, R.drawable.mostlycloudyday, R.drawable.partlycloud,
            R.drawable.sunny, R.drawable.sunrain, R.drawable.thunderstorm, R.drawable.weathercloudy, };

    int[] audio_pics = { R.drawable.cloudy, R.drawable.hazy, R.drawable.mostlycloudyday, R.drawable.partlycloud,
            R.drawable.sunny, R.drawable.sunrain, R.drawable.thunderstorm, R.drawable.weathercloudy, };

    ImageView imageView;

    Button mAudio;
    Button mVideo;
    Button mImages;

    ImageAdapter galleryAdapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mAudio = (Button) findViewById(R.id.button1);
        mVideo = (Button) findViewById(R.id.button2);
        mImages = (Button) findViewById(R.id.button3);

        mAudio.setOnClickListener(this);
        mVideo.setOnClickListener(this);
        mImages.setOnClickListener(this);

        Gallery ga = (Gallery) findViewById(R.id.Gallery01);
        galleryAdapter = new ImageAdapter(this); 
        galleryAdapter.setResourseArray(image_pics);
        ga.setAdapter(galleryAdapter);

        imageView = (ImageView) findViewById(R.id.ImageView01);
        ga.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
                Toast.makeText(getBaseContext(), "You have selected picture " + (pos + 1) + "of Gallery", Toast.LENGTH_SHORT)
                        .show();
                imageView.setImageResource(pics[pos]);

            }

        });
    }

    @Override
    public void onClick(View view) {
        if (view == mAudio) {
            if (galleryAdapter != null) {
                galleryAdapter.setResourseArray(audio_pics);
                galleryAdapter.notifyDataSetChanged();
            }
        } else if (view == mVideo) {
            if (galleryAdapter != null) {
                galleryAdapter.setResourseArray(video_pics);
                galleryAdapter.notifyDataSetChanged();
            }
        } else if (view == mImages) {
            if (galleryAdapter != null) {
                galleryAdapter.setResourseArray(image_pics);
                galleryAdapter.notifyDataSetChanged();
            }
        }
        }

    public class ImageAdapter extends BaseAdapter {

        private Context ctx;
        int imageBackground;
        private int[] resourseArray = null;

        public ImageAdapter(Context c) {
            ctx = c;
            TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
            imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
            ta.recycle();
        }

        @Override
        public int getCount() {
            return resourseArray.length;
        }

        public int[] getResourseArray() {
            return resourseArray;
        }

        public void setResourseArray(int[] resourseArray) {
            this.resourseArray = resourseArray;
        }

        @Override
        public Object getItem(int arg0) {

            return arg0;
        }

        @Override
        public long getItemId(int arg0) {

            return arg0;
        }

        @Override
        public View getView(int position, View arg1, ViewGroup arg2) {
            ImageView iv = new ImageView(ctx);
            iv.setImageResource(resourseArray[position]);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            iv.setLayoutParams(new Gallery.LayoutParams(150, 120));
            iv.setBackgroundResource(imageBackground);
            return iv;
        }

    }
}

答案 1 :(得分:0)

Button.onClick事件 - &gt; ga.setSelection(int position);