【问题标题】:onChildClick Event for ExpandableListAdapter not workingExpandableListAdapter 的 onChildClick 事件不起作用
【发布时间】:2013-04-13 20:00:54
【问题描述】:

我一直在查看其他人对这个问题的回答,但到目前为止似乎没有任何效果。

目前我有一个 ExpandableListAdapter,它是根据 SDK 附带的示例之一构建的。就显示数据而言,我可以正常工作,但我根本无法让 onChildClick 事件正常工作。我尝试将我的 XML 元素上的可聚焦属性设置为 false,但我仍然没有任何运气。

目前这是我的代码:

MainActivity.java

package com.example.expandablelisttest;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ExpandableListView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.expandableListView1);

    ExpandableListAdapter expandableAdapter = new ExpandableListAdapter();

    expandableListView.setAdapter(expandableAdapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
}

ExpandableListAdapter.java

package com.example.expandablelisttest;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;

public class ExpandableListAdapter extends BaseExpandableListAdapter
implements ExpandableListView.OnChildClickListener
{       
private String[] groups = { "People Names", "Dog Names", "Cat Names", "Fish Names" };
private String[][] children = {
        { "Arnold", "Barry", "Chuck", "David" },
        { "Ace", "Bandit", "Cha-Cha", "Deuce" },
        { "Fluffy", "Snuggles" },
        { "Goldy", "Bubbles" }
};



public Object getChild(int groupPosition, int childPosition) {
    return children[groupPosition][childPosition];
}

public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent)
{
    if (convertView ==null)
    {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());           
        convertView = layoutInflater.inflate(R.layout.child_view,  null);
    }

    TextView childTxt = (TextView) convertView.findViewById(R.id.childTxt);
    childTxt.setText(getChild(groupPosition, childPosition).toString());

    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    return children[groupPosition].length;
}

@Override
 public Object getGroup(int groupPosition) {
     return groups[groupPosition];
 }

 @Override
 public int getGroupCount() {
     return groups.length;
 }

 @Override
 public long getGroupId(int groupPosition) {
     return groupPosition;
 }

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent)
{
    if (convertView ==null)
    {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());   
        convertView = layoutInflater.inflate(R.layout.group_view,  null);
    }

    TextView groupTxt = (TextView) convertView.findViewById(R.id.groupTxt);
    groupTxt.setText(getGroup(groupPosition).toString());

    return convertView;
}

@Override
public boolean hasStableIds() {
    // TODO Auto-generated method stub
    return true;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    return true;
}

@Override
public boolean onChildClick(ExpandableListView parent, View v,
    int groupPosition, int childPosition, long id)
{
    Log.d("Result:", "Click event triggered");
    return true;
}   
}

activity_main.xml

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

    <ExpandableListView
        android:id="@+id/expandableListView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >
    </ExpandableListView>

</LinearLayout>

child_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:focusable="false" >

<TextView
    android:id="@+id/childTxt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:focusable="false"/>

</LinearLayout>

group_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:focusable="false" >

<TextView
    android:id="@+id/groupTxt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="36dp"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:focusable="false" />

</LinearLayout>

如果有人知道为什么我的点击事件没有触发并可以帮助我。我将不胜感激。提前致谢。

【问题讨论】:

    标签: android expandablelistadapter


    【解决方案1】:

    发生这种情况主要是因为您的孩子在您尝试单击它时没有被选中,因为在包含执行此操作的方法的适配器中称为 isChildSelectable(),它默认返回 flase,因此请按照您的意愿设置它被选中并解决问题...谢谢:)

    【讨论】:

      【解决方案2】:

      OnChildClickListener 不属于Adapter,它应该添加到ExpandableListView,而不是你的Activity

      像这样:

      expandableListView.setOnChildClickListener(new OnChildClickListener() {
          @Override
          public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
              // Handle clicks on the children here...
              return false;
          }
      });
      

      【讨论】:

        【解决方案3】:

        如果您在适配器中使用 isChildSelectable,则更改布尔返回值 true。您可以使用它

        @Override
            public boolean isChildSelectable(int groupPosition, int childPosition) {
                return true;
            }
        

        而不是

        @Override
                public boolean isChildSelectable(int groupPosition, int childPosition) {
                    return false;
                }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-07-18
          • 1970-01-01
          • 2013-05-19
          • 2018-04-01
          • 2012-09-17
          • 2020-11-26
          相关资源
          最近更新 更多