繁体   English   中英

在Android中设置图片库视图

[英]Image setting Gallery view in android

如何在图片上切换一些信息? 当我们切换图片时,信息也应该在android的Gallery视图中切换。

  public class ImageAdapter extends BaseAdapter {
        String[] Merchantname=null,Cardname=null,Points=null,Expirydate=null,status=null;
        private Context ctx;
        int imageBackground;
        Bitmap[] image_data;

        public ImageAdapter(Context c, Bitmap []card_image,String [] Merchantname,String [] Cardname,String []points,String[] Expirydate, String []status) {
            ctx = c;
            image_data = card_image;
            TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
            imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
            ta.recycle();
            this.Merchantname=Merchantname;
            this.Cardname=Cardname;
            this.Points=points;
            this.Expirydate=Expirydate;
            this.status=status;
        }

        public int getCount() {

            return image_data.length;
        }

        public Object getItem(int arg0) {

            return arg0;
        }

        public long getItemId(int arg0) {

            return arg0;
        }

        public View getView(int position, View arg1, ViewGroup arg2) {
            TextView tv1,tv2,tv3,tv4,tv5;
            ImageView i ;//= new ImageView(this.ctx);
            if (arg1 == null) { 
                i = new ImageView(this.ctx); 
            } else { 
                i = (ImageView) arg1; 
            } 

            tv1=(TextView)findViewById(R.id.Merchantname);
            tv2=(TextView)findViewById(R.id.Cardname);
            tv3=(TextView)findViewById(R.id.Expirydate);
            tv4=(TextView)findViewById(R.id.status);

            tv1.setText(Merchantname[position]);
            tv2.setText(Cardname[position]);
            tv3.setText(Expirydate[position]);
            tv4.setText(status[position]);

//          ImageView iv = new ImageView(ctx);
//          Drawable drawable = new BitmapDrawable(getResources(), image_data[position]);
            i.setImageBitmap(image_data[position]);
            i.setImageDrawable(new BitmapDrawable(getResources(), image_data[position]));
            i.setScaleType(ImageView.ScaleType.FIT_XY);
            i.setLayoutParams(new Gallery.LayoutParams(300,200));
            i.setBackgroundResource(imageBackground);
            return i;
        }

    }

您只需使用一个ArrayList其索引将与Gallery views各个视图链接,并且ArrayList的长度将与Gallery View的长度相匹配,在setOnItemClickListener上,使用position变量从setOnItemClickListener匹配的Array List中更改Content即可。相同索引

   // Reference the Gallery view
    Gallery g = (Gallery) findViewById(R.id.gallery);
    // Set the adapter to our custom adapter (below)
    g.setAdapter(new ImageAdapter(this));

    // Set a item click listener, and just Toast the clicked position
    g.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position, long id) {
            Toast.makeText(Gallery1.this, "" + position, Toast.LENGTH_SHORT).show();
        }
    });

使用ImageViewTextView创建布局。 然后为Gallery编写自定义适配器,然后在getView方法中将ImageViewTextView的值放入其中。 他们将此适配器设置为您的Gallery

问题就在这里,

    tv1=(TextView)findViewById(R.id.Merchantname);
    tv2=(TextView)findViewById(R.id.Cardname);
    tv3=(TextView)findViewById(R.id.Expirydate);
    tv4=(TextView)findViewById(R.id.status);

您在要求LayoutFile之前忘了提到父视图,为所有TextView添加(TextView)arg1.findViewById()并告诉我们。 希望能帮助到你

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM