图库没有按钮点击

时间:2012-03-29 09:39:06

标签: android

在ma应用程序中,我使用的是具有ImageView和按钮的图库小部件。我能够让Imageview点击gallery.setOnItemClickListener事件,但我没有得到ma按钮点击。如何才能点击按钮。请帮帮我。

请查看ma代码:

public ImageAdapter(Activity c,Integer[] images) {
        this.mContext = c;
        this.Images=images;
        TypedArray attr = mContext.obtainStyledAttributes(R.styleable.HelloGal999lery);
        mGalleryItemBackground = attr.getResourceId(
                R.styleable.HelloGal999lery_android_galleryItemBackground, 0);
        attr.recycle();
    }

    public int getCount() {
        return Images.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return 1;
    }

    public boolean onInterceptTouchEvent(MotionEvent __e) {
        return false;
    }


     static class ViewHolder{
        public ImageView imageView1;
        public ImageButton btn1;
     }

    public View getView(int position, View convertView, ViewGroup parent) {  

         ViewHolder holder; 
         View rowView = convertView;

        if(rowView==null)
        {
         LayoutInflater inflater = mContext.getLayoutInflater();
         rowView = inflater.inflate(R.layout.test, null, true);
         holder=new ViewHolder();
         holder.imageView1=(ImageView) rowView.findViewById(R.id.ImageViewuser);    
         holder.btn1=(ImageButton)rowView.findViewById(R.id.Download);
         holder.imageView1.setImageResource(Images[position]);               
         holder.imageView1.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
         holder.imageView1.setBackgroundResource(mGalleryItemBackground);                
        }
        // rowView.setMinimumHeight(100);
         return rowView;                    
    }

Ma父级布局

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"    
     >  


            <Gallery  
        android:id="@+id/gallery"
        android:layout_width="fill_parent"
        android:spacing="10dp"            
        android:layout_height="fill_parent" />



</RelativeLayout> 

Ma子布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical" >

   <ImageView 
       android:id="@+id/ImageViewuser"
       android:layout_width="150dip"
       android:layout_height="150dip"         

       />
<ImageButton 
    android:id="@+id/Download"
    android:layout_below="@+id/ImageViewuser"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"    
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="50dp"    
    android:background="@drawable/clearall1"
    />

</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

onclickListener方法的ImageButton上设置getView(),然后点击ImageButton的位置:

btn1.setOnClickListener(new View.OnClickListener() {
   public void onClick(View v) {
     ///  your code 
   }     
}); 

NB:关于该职位,您已经在getView()方法的参数上使用了该职位,您可以在onClick()方法中使用

答案 1 :(得分:0)

你没有为点击事件编写代码,在getView()方法中为按钮添加onClick()函数

holder.btn1=(ImageButton)rowView.findViewById(R.id.Download);     
btn1.setOnClickListener(new View.OnClickListener() {
   public void onClick(View v) {
     ///  write code here  
   }     
}); 

你可以在这里找到位置

还有 你可以得到这样的位置 画廊中项目的位置

gallery.setOnItemClickListener(new OnItemClickListener() {
   public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
      Toast.makeText(GalleryActivity.this, "" + position, Toast.LENGTH_SHORT).show();
   }
});