Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android自定义Expandablelistview不会触发onChildClick_Android - Fatal编程技术网

Android自定义Expandablelistview不会触发onChildClick

Android自定义Expandablelistview不会触发onChildClick,android,Android,我是android编程的初学者。我已经从下载了代码,工作正常,但唯一的问题是没有触发 一次点击 这里是主要活动 public class MenuCategory extends Activity { private ExpandableListView mExpandableListView; private List<GroupEntity> mGroupCollection; ExpandableListAdapter adapt

我是android编程的初学者。我已经从下载了代码,工作正常,但唯一的问题是没有触发 一次点击

这里是主要活动

public class MenuCategory extends Activity {
        private ExpandableListView mExpandableListView;
        private List<GroupEntity> mGroupCollection;
        ExpandableListAdapter adapter;

        @Override
        public void onCreate(Bundle savedInstanceStater) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.menucategory);

            prepareResource();
            initPage();

        }


        private void prepareResource() {
            String data = null;
            FileInputStream fIn = null;
            InputStreamReader isr = null;
            String response = null;
            String getParam = "?get=menuCategory";
            try{
                fIn = openFileInput("fromfile.dat");
                isr = new InputStreamReader(fIn);
                int size = fIn.available(); 
                char[] inputBuffer = new char[size];
                isr.read(inputBuffer);
                data = new String(inputBuffer);
                isr.close();
                fIn.close();
                String[] dataSep = data.split("#");
                String Hs = dataSep[0];

                try {
                    response = CustomHttpClient.executeHttpGet(Hs+getParam);
                    String res = response.toString(); 
                    if(response.length()!=0){
                        mGroupCollection = new ArrayList<GroupEntity>();
                        JSONObject jsonObject = null;
                        JSONArray eventArray = new JSONArray(res);
                        for (int i = 0; i < eventArray.length(); i++) {
                            jsonObject = eventArray.getJSONObject(i);
                            GroupEntity ge = new GroupEntity();
                            ge.Name = jsonObject.getString("Cat");

                            JSONArray abc  = jsonObject.getJSONArray("SubCat");
                            for (int j=0; j<abc.length(); j++){
                                GroupItemEntity gi = ge.new GroupItemEntity();
                                gi.Name = abc.getString(j);
                                ge.GroupItemCollection.add(gi);
                            }
                            mGroupCollection.add(ge);   
                        }
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


            }catch(IOException e){
                e.printStackTrace(System.err);
            }
        }

        private void initPage() {
            mExpandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
            adapter = new ExpandableListAdapter(this,mExpandableListView, mGroupCollection);
            mExpandableListView.setAdapter(adapter);
            registerForContextMenu(mExpandableListView);
        }
        public boolean onChildClick( ExpandableListView parent, View v, int groupPosition,int childPosition,long id) {
            String string = "Child Click";
            int duration = Toast.LENGTH_SHORT;
            Toast toast = Toast.makeText(getApplicationContext(), string, duration);
            toast.show();
            return false;
        }
    }
公共类菜单类别扩展活动{
私有可扩展列表视图mExpandableListView;
私人名单收集;
可扩展列表适配器;
@凌驾
创建时的公共void(Bundle savedInstanceStater){
super.onCreate(savedInstanceState);
setContentView(R.layout.menucategory);
prepareResource();
initPage();
}
私有void prepareResource(){
字符串数据=null;
FileInputStream fIn=null;
InputStreamReader isr=null;
字符串响应=null;
字符串getParam=“?get=menuCategory”;
试一试{
fIn=openFileInput(“fromfile.dat”);
isr=新的输入流读取器(fIn);
int size=fIn.available();
char[]inputBuffer=新字符[大小];
isr.read(输入缓冲区);
数据=新字符串(inputBuffer);
isr.close();
fIn.close();
字符串[]dataSep=data.split(“#”);
字符串Hs=dataSep[0];
试一试{
response=CustomHttpClient.executeHttpGet(Hs+getParam);
String res=response.toString();
if(response.length()!=0){
mGroupCollection=新的ArrayList();
JSONObject JSONObject=null;
JSONArray事件数组=新的JSONArray(res);
对于(int i=0;i对于(int j=0;j要设置子项单击的侦听器,可以在
initPage
方法(而不是
onCreate()
)中这样做:


在适配器中,您有一个名为isChildSelectable()的方法,它返回false,这就是问题所在。将返回值更改为true以解决问题。

我知道这是旧线程,但如果有人来这里查找答案,这是为他们准备的。
我发现了同样的问题,当我过去让可点击的子项只转到“主组项布局”(在我的代码中是list\u group\u item)并使linearlayout和textview可点击和可传真为false时。

我遇到了这个问题,因为ChildClick没有被触发

我发现这样的评论:

解决了,问题是如果在childview上有多个textview,则focusable属性必须为false(我不知道确切原因),将android:genderantFocusability=“blocksDescendants”添加到视图解决了问题


因此,在childView上添加android:DegenantFocusability=“BlocksDescents”
对我来说很有效。

我看不到在您的代码中有任何地方可以设置子单击侦听器来实际侦听这些事件。当我添加mExpandableListView.setOnChildClickListener(新建onChildClickListener(){public boolean onChildClick(ExpandableListView父级、视图v、int-groupPosition、int-childPosition、长id){String String=“Child Click”;int-duration=Toast.LENGTH\u SHORT;Toast-Toast=Toast.makeText(getApplicationContext(),String,duration);Toast.show();return false;}});onCreate()它向我显示了eclipse上的错误,因为onChildClickListener无法解析为type@Manu我也使用了相同的教程。请告诉我如何在子单击时执行操作。我可以使用这些代码expandableListView.setOnChildClickListener(新OnChildClickListener(){@Override public boolean onChildClick(ExpandableListView父级、视图v、int-groupPosition、int-childPosition、长id){//TODO自动生成的方法存根System.out.println(“单击………”+groupPosition);Intent categoryList=new Intent(StatesScreen.this,categoryList.class);startActivity(categoryList);返回false;});@Manu错误告诉你什么?你确定你有正确的导入:
导入android.widget.ExpandableListView;
导入android.widget.ExpandableListView.OnChildClickListener;
?是的,我已经导入了。但是给我的错误是:方法setOnChildClickListener(ExpandableListView.OnChildClickListener)在类型ExpandableListView不适用于参数(new OnChildClickListener(){})中,OnChildClickListener无法解析为type@Manu如果使用eclipse,请尝试删除所有导入,然后执行
CTRL
+
SHIFT
+
O
(不是数字
0
,而是字母O)并选择正确的导入。我有您的建议,没有错误,但单击事件不起作用。我调试它,但单击时没有响应listener@Manu我猜放置toast不会触发?如果是这种情况,那么添加适配器和行布局的代码。这在很久以前就应该是正确的答案。它让继承的方法是否会被忽略,这让我毛骨悚然:如果您选择了正确的答案,PIt会很有帮助,因此它也可能会帮助其他人或分享最终对您有效的解决方案。这是唯一对我有效的解决方案。其他常见建议,如设置isChildSelectable()若要返回true,则有必要但不足以解决问题。
public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private ExpandableListView mExpandableListView;
    private List<GroupEntity> mGroupCollection;
    private int[] groupStatus;
    private int lastExpandedGroupPosition;

    public ExpandableListAdapter(Context pContext,
            ExpandableListView pExpandableListView,
            List<GroupEntity> pGroupCollection) {
        mContext = pContext;
        mGroupCollection = pGroupCollection;
        mExpandableListView = pExpandableListView;
        groupStatus = new int[mGroupCollection.size()];

        setListEvent();
    }

    private void setListEvent() {

        mExpandableListView
                .setOnGroupExpandListener(new OnGroupExpandListener() {

                    public void onGroupExpand(int arg0) {
                        // TODO Auto-generated method stub
                        groupStatus[arg0] = 1;
                        lastExpandedGroupPosition = arg0;
                    }
                });

        mExpandableListView
                .setOnGroupCollapseListener(new OnGroupCollapseListener() {

                    public void onGroupCollapse(int arg0) {
                        // TODO Auto-generated method stub
                        groupStatus[arg0] = 0;
                    }
                });
    }

    public String getChild(int arg0, int arg1) {
        // TODO Auto-generated method stub
        return mGroupCollection.get(arg0).GroupItemCollection.get(arg1).Name;
    }

    public long getChildId(int arg0, int arg1) {
        // TODO Auto-generated method stub
        return 0;
    }

    public View getChildView(int arg0, int arg1, boolean arg2, View arg3,
            ViewGroup arg4) {
        // TODO Auto-generated method stub

        ChildHolder childHolder;
        if (arg3 == null) {
            arg3 = LayoutInflater.from(mContext).inflate(
                    R.layout.list_group_item, null);

            childHolder = new ChildHolder();

            childHolder.title = (TextView) arg3.findViewById(R.id.item_title);
            arg3.setTag(childHolder);
        }else {
            childHolder = (ChildHolder) arg3.getTag();
        }

        childHolder.title.setText(mGroupCollection.get(arg0).GroupItemCollection.get(arg1).Name);
        return arg3;
    }

    public int getChildrenCount(int arg0) {
        // TODO Auto-generated method stub
        return mGroupCollection.get(arg0).GroupItemCollection.size();
    }

    public Object getGroup(int arg0) {
        // TODO Auto-generated method stub
        return mGroupCollection.get(arg0);
    }

    public int getGroupCount() {
        // TODO Auto-generated method stub
        return mGroupCollection.size();
    }

    public long getGroupId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    public View getGroupView(int arg0, boolean arg1, View arg2, ViewGroup arg3) {
        // TODO Auto-generated method stub
        GroupHolder groupHolder;
        if (arg2 == null) {
            arg2 = LayoutInflater.from(mContext).inflate(R.layout.list_group,
                    null);
            groupHolder = new GroupHolder();
            groupHolder.img = (ImageView) arg2.findViewById(R.id.tag_img);
            groupHolder.title = (TextView) arg2.findViewById(R.id.group_title);
            arg2.setTag(groupHolder);
        } else {
            groupHolder = (GroupHolder) arg2.getTag();
        }
        if (groupStatus[arg0] == 0) {
            groupHolder.img.setImageResource(R.drawable.group_down);
        } else {
            groupHolder.img.setImageResource(R.drawable.group_up);
        }
        groupHolder.title.setText(mGroupCollection.get(arg0).Name);

        return arg2;
    }

    class GroupHolder {
        ImageView img;
        TextView title;
    }

    class ChildHolder {
        TextView title;
    }

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

    public boolean isChildSelectable(int arg0, int arg1) {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    public void onGroupExpanded(int groupPosition){
        //collapse the old expanded group, if not the same
        //as new group to expand
        if(groupPosition != lastExpandedGroupPosition){
            mExpandableListView.collapseGroup(lastExpandedGroupPosition);
        }

        super.onGroupExpanded(groupPosition);           
        lastExpandedGroupPosition = groupPosition;
    }
}
<?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="wrap_content"
    android:background="@drawable/group_header_bg"
    android:gravity="center_vertical" >

    <ImageView
        android:id="@+id/tag_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="10dip"
        android:src="@drawable/group_down"
        android:focusable="false"
         />

    <TextView
        android:id="@+id/group_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dip"
        android:text=""
        android:textColor="#57810f"
        android:textSize="18dip"
        android:textStyle="bold"
        android:focusable="false"
         />

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:baselineAligned="false"
    android:paddingTop="5dip" >

    <LinearLayout
        android:id="@+id/groupItem"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="@drawable/play_group_item"
        android:clickable="true" >


        <TextView
            android:id="@+id/item_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="15dip"
            android:layout_marginLeft="15dip"
            android:text="sample"
            android:textColor="#FFF38585"
            android:textSize="20sp"
            android:focusable="false"
             />

    </LinearLayout>

</LinearLayout>
mExpandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
            adapter = new ExpandableListAdapter(this,mExpandableListView, mGroupCollection);
            mExpandableListView.setAdapter(adapter);
registerForContextMenu(mExpandableListView);
mExpandableListView.setOnChildClickListener(new OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {
                String string = "Child Click";
                int duration = Toast.LENGTH_SHORT;
                Toast toast = Toast.makeText(getApplicationContext(), string,
                        duration);
                toast.show();
                return false;
            }
        });