GridView子元素未检测到Click事件

时间:2014-02-12 06:41:47

标签: android android-layout gridview

我有一个有两个孩子的GridView

1)的CheckBox 2)ImageView的

我使用ViewHolder和Inflate My Custom GridView,触摸Checkbox时它会被选中并取消选中,但当我点击ImageView时它根本没有检测到任何点击,请帮帮我,,,代码是

galleryitem.xml(CustomGridView)

<?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" >

    <ImageView
        android:id="@+id/thumbImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"/>

    <CheckBox
        android:id="@+id/itemCheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:focusable="false"/>

</RelativeLayout>

multiselectorgrid.xml(MyGridViewActivity)

<?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"
    android:background="@color/kshheadercolor" >

    <include
        android:id="@+id/include1"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        layout="@layout/ksh_header_logo" >
    </include>

    <GridView
        android:id="@+id/PhoneImageGrid"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/selectBtn"
        android:layout_below="@+id/include1"
        android:columnWidth="90dp"
        android:gravity="center"
        android:layout_marginTop="5dp"
        android:horizontalSpacing="10dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:verticalSpacing="10dp" />

    <Button
        android:id="@+id/selectBtn"        
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_marginLeft="10dp"
        android:layout_marginBottom="10dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:background="@drawable/blue_button"
        android:text="@string/back"
        android:textSize="15sp" />

    <Button
        android:id="@+id/btn_ksh_back"
        android:layout_width="wrap_content"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="10dp"
        android:layout_height="40dp"        
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:background="@drawable/blue_button"
        android:text="@string/ok"
        android:textSize="15sp" />

</RelativeLayout>

MyCustom适配器

public class ImageAdapter extends BaseAdapter {
        private LayoutInflater mInflater;

        public ImageAdapter() {
            mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        public int getCount() {
            return count;
        }

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

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

    /*  public void onItemClick(AdapterView<?> parent, View v, int position,
                                    long id) {
            System.out.println("ItemClicked---->");
            RelativeLayout item_view = (RelativeLayout)v;
            CheckBox itemcheck = (CheckBox)item_view.findViewById(R.id.itemCheckBox);
            ImageView imageview = (ImageView) item_view.findViewById(R.id.thumbImage);
            itemcheck.setFocusable(false);
            imageview.setFocusable(false);
            int id1 = itemcheck.getId();
            if (thumbnailsselection[id1]){
                itemcheck.setChecked(false);
                thumbnailsselection[id1] = false;
            } else {
                itemcheck.setChecked(true);
                thumbnailsselection[id1] = true;
            }
         };*/

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

            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.galleryitem, null);
                holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
                holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);
                Typeface font = mGlobals.getTypeFace(MultiImageChooserActivity.this, mCurrentLocale);
                if(font != null)
                {
                    holder.checkbox.setTypeface(font);
                }
                convertView.setTag(holder);
            }
            else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.checkbox.setId(position);
            holder.imageview.setId(position);
            holder.checkbox.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    CheckBox cb = (CheckBox) v;
                    int id = cb.getId();
                    if (thumbnailsselection[id]){
                        cb.setChecked(false);
                        thumbnailsselection[id] = false;
                    } else {
                        cb.setChecked(true);
                        thumbnailsselection[id] = true;
                    }
                }
            });

            holder.imageview.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    int id = v.getId();
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_VIEW);
                    intent.setDataAndType(Uri.parse(getString(R.string.file_path) + arrPath[id]), getString(R.string.mime_type_all));
                    startActivity(intent);
                    System.out.println("ItemClicked---->");
                }
            });
            holder.imageview.setImageBitmap(thumbnails[position]);
            holder.checkbox.setChecked(thumbnailsselection[position]);
            holder.id = position;
            return convertView;
        }
    }
    class ViewHolder {
        ImageView imageview;
        CheckBox checkbox;
        int id;
    }

1 个答案:

答案 0 :(得分:0)

尝试在GridView

中根据需要使用以下库

https://github.com/luminousman/MultipleImagePick