单击网格中的项目不会唤起网格元素中存在的复选框上的侦听器

时间:2015-08-12 06:56:13

标签: android android-gridview

有gridview,我正在添加适配器。

 GridView numgrid = (GridView) findViewById(R.id.selectNumberGrid);
 numberAdapter = new NumberAdapter(this);
 numgrid.setAdapter(numberAdapter);

NumberAdapter代码是这样的

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

    final ViewHolder holder;

    if(convertView==null) {
        /****** Inflate xml file for each row ( Defined below ) *******/
        holder = new ViewHolder();
        convertView = inflater.inflate(
                R.layout.select_number_item, null);
        holder.textview = (TextView) convertView.findViewById(R.id.thumbText);
        holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);
        convertView.setTag(holder);
    }
    else
        holder = (ViewHolder) convertView.getTag();

    holder.checkbox.setId(position);
    holder.textview.setId(position*100);

    //set values
    holder.textview.setText(data[position]);
    holder.id = position;
    if(((MyApplication) activity.getApplication()).numbers.contains(Integer.parseInt(data[position])))
        holder.checkbox.setChecked(true);
    else
        holder.checkbox.setChecked(false);

    convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CheckBox cb  = (CheckBox)((ViewGroup)v).getChildAt(1);
            TextView tv = (TextView) ((ViewGroup)v).getChildAt(0);
            if(cb.isChecked()) {
                if(((MyApplication) activity.getApplication()).numbers.size()==1)
                    Toast.makeText(activity,"Select one or more numbers",Toast.LENGTH_SHORT).show();
                else {
                    cb.setChecked(false);
                    ((MyApplication) activity.getApplication()).changeNumState(false, Integer.parseInt(tv.getText().toString()));
                }
            }
            else {
                cb.setChecked(true);
                ((MyApplication) activity.getApplication()).changeNumState(true, Integer.parseInt(tv.getText().toString()));
            }
            Log.i("TAG","clicked");
        }
    });



    return convertView;
}

xml文件就像这样

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"   android:layout_width="match_parent"
 android:layout_height="match_parent"       android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.thakoor.timestables.SelectNumbers"
android:background="#FFFFFF">

<Spinner
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/spinner"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:entries="@array/options"
    android:prompt="@string/options_prompt"
    android:spinnerMode="dialog"
    />

<GridView android:id="@+id/selectNumberGrid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:numColumns="3"
    android:verticalSpacing="2dp"
    android:horizontalSpacing="2dp"
    android:columnWidth="90dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
    android:layout_below="@id/spinner"
    android:background="#e1e1e1"
    android:fadeScrollbars="false"
    android:scrollbarStyle="outsideInset"
    />


 </RelativeLayout>

和网格中的元素 - xml文件就像这样

<?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">
<TextView
    android:id="@+id/thumbText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="20dp"
    android:paddingBottom="20dp"
    android:textSize="20sp"/>

<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"
    android:focusableInTouchMode="false"
    android:scaleX="0.90"
    android:scaleY="0.90"
    />
</RelativeLayout>

每当我点击gridview中的项目时     1.当我点击textview onclick事件被调用时     2.但是当我点击复选框时,不会调用onclick事件

我想在复选框上调用onclick事件..我应该怎么做

1 个答案:

答案 0 :(得分:1)

使用setOnCheckChangeListener()或onClick on holder.checkbox 而不是在convertView上。如果在convertView上添加onClick,则父级将使用click事件,并将同一事件传递给其所有子视图。

要在单击textView时获得检查/取消选中行为,您可以使用以下内容:

 CheckBox cb  = (CheckBox)((ViewGroup)v).getChildAt(1);
 TextView tv = (TextView) ((ViewGroup)v).getChildAt(0);
 cb.setOnCheckChangeListener(new OnCheckedChangeListener() {

          @Override
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)    {
              //your check/uncheck logic
          }
 });

 tv.setOnClickListener(new OnClickListener(){
  @Override
  public void OnClick(View v){
     if (holder.checkbox.isChecked()) {
         holder.checkbox.setChecked(false);
    } else {
         holder.checkbox.setChecked(true);
    }
  } 
 });