【问题标题】:Clicking on an item in grid does not evoke listener on checkbox present in grid element单击网格中的项目不会引起网格元素中存在的复选框上的侦听器
【发布时间】:2015-08-12 16:44:19
【问题描述】:

有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>

而grid中的元素——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 事件。我该怎么办

【问题讨论】:

  • 我在网格视图项的根元素中添加了 android:descendantFocusability="blocksDescendants"。但它仍然无法正常工作
  • 尝试删除android:focusable="false" android:focusableInTouchMode="false"

标签: android android-gridview


【解决方案1】:

holder.checkbox 上使用 setOnCheckChangeListener() 或 onClick,而不是在 convertView 上。如果您在 convertView 上添加 onClick,则单击事件将被父级消耗,并且相同的事件将传递给其所有子视图。

要实现单击 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);
    }
  } 
 });

【讨论】:

  • 但我希望在单击网格视图中的项目后更改复选框状态(即单击文本视图或复选框)
猜你喜欢
  • 1970-01-01
  • 2014-11-28
  • 2022-07-08
  • 1970-01-01
  • 2018-01-10
  • 2014-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多