我使用expandable listview根据南,北,东,中心显示印度的状态。状态列表显示那样,但是当应用onChildclickListener到expandableListView时没有响应。我想在点击印度的状态时打开一个新的活动。
可扩展列表视图
<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>
适配器类
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;
}
}
活动类
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;
}
});
答案 0 :(得分:2)
出现此问题,然后您的孩子的视图具有自己的焦点元素。您可以通过删除android:clickable from children layout或将其设置为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" -->
>
如果你的子元素没有任何可点击和可聚焦的元素,那么可以使用标准的ListView可聚焦。
抱歉我的英文不好