3

我正在使用可扩展列表视图根据南、北、东、中心显示印度的状态。状态列表显示类似,但是当将 onChildclickListener 应用于 expandableListView 时没有响应。我希望在单击印度状态时打开一个新活动。

  1. 可展开的列表视图

     <ExpandableListView
     android:id="@+id/indiaParts"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content" 
     android:groupIndicator="@null"
     android:listSelector="@null"
     android:scrollbars="none"
    />
    
  2. 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"
        />
    

  3. 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>
    
  4. 适配器类

     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;
    }
    

    }

  5. 活动课

           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;
            }
        });
    
4

1 回答 1

2

出现此问题后,您的孩子的 View 有自己的可聚焦元素。您可以通过从子布局中删除 android:clickable 或设置为 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 可聚焦元素。

对不起,我的英语不好

于 2012-11-05T08:27:46.763 回答