问题
I am using expandable listview to show states of india according to south,north,east,centeral.States list show like that ,but when apply onChildclickListener to expandableListView no response.I want when click state of india a new activity open.
expandable listview
<ExpandableListView android:id="@+id/indiaParts" android:layout_width="fill_parent" android:layout_height="wrap_content" android:groupIndicator="@null" android:listSelector="@null" android:scrollbars="none" />list_group.xml
<?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" /> <TextView android:id="@+id/group_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_marginLeft="10dip" android:text="Sample" android:textColor="#FFFFFF" android:textSize="18dip" android:textStyle="bold" />list_group_item.xml
<?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="wrap_content" android:background="@drawable/group_item_normal" android:id="@+id/groupItem" android:clickable="true" > <TextView android:id="@+id/item_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dip" android:text="sample" android:textColor="#000000" android:textSize="20sp" android:layout_gravity="center" android:gravity="center" /> </LinearLayout>Adapter class
public class ExpandableListAdapter extends BaseExpandableListAdapter { private Context mContext; private ExpandableListView mExpandableListView; private ArrayList <GroupEntity> mGroupCollection; private int[] groupStatus; public ExpandableListAdapter(Context pContext, ExpandableListView pExpandableListView, ArrayList<GroupEntity> pGroupCollection) { mContext = pContext; mGroupCollection = pGroupCollection; mExpandableListView = pExpandableListView; groupStatus = new int[mGroupCollection.size()]; setListEvent(); } private void setListEvent() { mExpandableListView .setOnGroupExpandListener(new OnGroupExpandListener() { @Override public void onGroupExpand(int arg0) { // TODO Auto-generated method stub groupStatus[arg0] = 1; } }); mExpandableListView .setOnGroupCollapseListener(new OnGroupCollapseListener() { @Override public void onGroupCollapse(int arg0) { // TODO Auto-generated method stub groupStatus[arg0] = 0; } }); } @Override public String getChild(int arg0, int arg1) { // TODO Auto-generated method stub return mGroupCollection.get(arg0).getStatesList().get(arg1).getStateName(); } @Override public long getChildId(int arg0, int arg1) { // TODO Auto-generated method stub return 0; } @Override 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).getStatesList().get(arg1).getStateName()); return arg3; } @Override public int getChildrenCount(int arg0) { // TODO Auto-generated method stub return mGroupCollection.get(arg0).getStatesList().size(); } @Override public Object getGroup(int arg0) { // TODO Auto-generated method stub return mGroupCollection.get(arg0); } @Override public int getGroupCount() { // TODO Auto-generated method stub return mGroupCollection.size(); } @Override public long getGroupId(int arg0) { // TODO Auto-generated method stub return arg0; } @Override 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).getGroupName()); return arg2; } class GroupHolder { ImageView img; TextView title; } class ChildHolder { TextView title; } @Override public boolean hasStableIds() { // TODO Auto-generated method stub return true; } @Override public boolean isChildSelectable(int arg0, int arg1) { // TODO Auto-generated method stub System.out.println("vcvv.........."+true); return true; }}
Activity class
expandableListView=(ExpandableListView) findViewById(R.id.indiaParts); expandableListAdapter=new ExpandableListAdapter(StatesScreen.this, expandableListView, groupEntityList); expandableListView.setAdapter(expandableListAdapter); expandableListView.setOnChildClickListener(new OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { // TODO Auto-generated method stub Toast.makeText(StatesScreen.this, "Hi click",Toast.LENGTH_LONG).show(); System.out.println("clicked..............////."+groupPosition); Intent categoryList=new Intent(StatesScreen.this,CategoryList.class); startActivity(categoryList); return false; } });
回答1:
This issue appear then your children's View has own focusable element. You can remove it by removing android:clickable from children layout or set to false:
<?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="wrap_content"
android:background="@drawable/group_item_normal"
android:id="@+id/groupItem"
<!-- remove clickable from here -->
<!-- or android:clickable="false" -->
>
If you children element doesn't has any clickable and focusable elements, then work standart ListView focusable.
sorry for my bad english
来源:https://stackoverflow.com/questions/13227973/expandabelistview-not-support-childclicklistener-in-android