将不同的图像设置为不同的图库视图

时间:2012-02-01 22:29:59

标签: java android android-layout

我有一个包含23行的列表视图。当我单击任何一行时,它会打开一个新活动(包含textview和按钮的新屏幕)。单击按钮时,它会打开另一个屏幕,这是一个galleryview。所以我点击了23个按钮打开23个galleryview。我想在每个galleryview中设置不同的图像。

这是我的代码。

(gallery_day.class)

                 public class Day_gallery extends Activity {
Integer[] pics = {
        R.drawable.one_1,
        R.drawable.one_2,
        R.drawable.one_3,
        R.drawable.one_4,
        R.drawable.one_5,
        R.drawable.one_6,
        R.drawable.one_7,
        R.drawable.one_8,
        R.drawable.one_9,
        R.drawable.one_10
   };
    ImageView imageView;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Boolean customTitleSupported = requestWindowFeature     
                (Window.FEATURE_CUSTOM_TITLE); 
     setContentView(R.layout.day_gallery);  
         if (customTitleSupported) { 
             getWindow().setFeatureInt  
                  (Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title); 

         TextView tv = (TextView) findViewById(R.id.tv); 
             tv.setText("day pictures"); 
                } 


        Gallery ga = (Gallery)findViewById(R.id.Gallery);
        ga.setAdapter(new ImageAdapter(this));

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

    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {

                imageView.setImageResource(pics[arg2]);

            }

        });

    }


    public class ImageAdapter extends BaseAdapter {

        private Context ctx;
        int imageBackground;

        public ImageAdapter(Context c) {
            ctx = c;
        TypedArray ta = obtainStyledAttributes(R.styleable.Gallery);
imageBackground = ta.getResourceId 
       (R.styleable.Gallery_android_galleryItemBackground, 1);

            ta.recycle();
        }

        public int getCount() {

            return pics.length;
        }

        public Object getItem(int arg0) {

            return arg0;
        }

        public long getItemId(int arg0) {

            return arg0;
        }

        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 :(得分:0)

有一项活动,您可以传递要使用的文本。将它放在捆绑包中的哪一天,并在下一个活动中,获取整数,然后设置文本。这样你只需要3个活动。一个用于ListView,一个用于文本和按钮,一个用于显示库。从按钮到图库的类似想法。

这可能会有所帮助:http://remwebdevelopment.com/dev/a33/Passing-Bundles-Around-Activities.html

答案 1 :(得分:0)

我按照以下方式达到此解决方案:

 if(row.equalsIgnoreCase("first row")){
int[] tempimages = { R.drawable.one_1,R.drawable.one_2,R.drawable.one_3 };  
    tv.setText("first gallery");}

else if
     (row.equalsIgnoreCase("second row")){
int[] tempimages = { R.drawable.one_4,R.drawable.one_5,R.drawable.one_6 };  
    tv.setText("second gallery");} 

继续其余的行库。