gpt4 book ai didi

android - Android 中的 ExpandabeListView 不支持 ChildClickListener

转载 作者:太空狗 更新时间:2023-10-29 13:33:20 25 4
gpt4 key购买 nike

我正在使用可扩展 ListView 根据南、北、东、中部显示印度各州。状态列表显示是这样的,但是当将 onChildclickListener 应用于 expandableListView 时没有响应。我希望在单击印度状态时打开一个新 Activity 。

  1. 可扩展 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"
    />
  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. Activity 课

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

最佳答案

此问题出现时您的 child 的 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 可聚焦。

抱歉我的英语不好

关于android - Android 中的 ExpandabeListView 不支持 ChildClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13227973/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com