ExpandabeListView不支持ChildClickListener在安卓不支持、ExpandabeListView、ChildClickListener

2023-09-06 16:38:54 作者:冷風吹

我使用的可扩展列表视图按南,北,东,centeral.States列表显示一样,显示印度的状态,但是当申请onChildclickListener到expandableListView没有response.I希望在印度的点击状态,一个新的活动开放。

扩展列表视图

 < ExpandableListView
 机器人:ID =@ + ID / indiaParts
 机器人:layout_width =FILL_PARENT
 机器人:layout_height =WRAP_CONTENT
 机器人:groupIndicator =@空
 机器人:listSelector =@空
 机器人:滚动条=无
/>
 

list_group.xml

 < XML版本=1.0编码=UTF-8&GT?;
 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:背景=@可绘制/ group_header_bg
机器人:重力=center_vertical
 >

< ImageView的
    机器人:ID =@ + ID / tag_img
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentRight =真
    机器人:layout_alignParentTop =真
    机器人:layout_marginRight =10dip
    机器人:SRC =@可绘制/ group_down
  />

<的TextView
    机器人:ID =@ + ID / group_title
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentLeft =真
    机器人:layout_marginLeft =10dip
    机器人:文本=样本
    机器人:文字颜色=#FFFFFF
    机器人:TEXTSIZE =18dip
    机器人:TEXTSTYLE =黑体
    />
 

list_group_item.xml

 < XML版本=1.0编码=UTF-8&GT?;
   < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
  机器人:layout_width =FILL_PARENT
  机器人:layout_height =WRAP_CONTENT
  机器人:背景=@可绘制/ group_item_normal
  机器人:ID =@ + ID / groupItem
  机器人:可点击=真
  >

    <的TextView
        机器人:ID =@ + ID / ITEM_TITLE
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_marginLeft =15dip
        机器人:文本=样本
        机器人:文字颜色=#000000
        机器人:TEXTSIZE =20SP
        机器人:layout_gravity =中心
        机器人:重力=中心
        />

  < / LinearLayout中>
 
女心理师 讽刺现实,映射职场现状,配角张海宇,人设引起共鸣

适配器类

 公共类ExpandableListAdapter扩展BaseExpandableListAdapter {

私人语境mContext;
私人ExpandableListView mExpandableListView;
私人的ArrayList< GroupEntity> mGroupCollection;
私人INT [] groupStatus;

公共ExpandableListAdapter(上下文pContext,
        ExpandableListView pExpandableListView,
        ArrayList的< GroupEntity> pGroupCollection){
    mContext = pContext;
    mGroupCollection = pGroupCollection;
    mExpandableListView = pExpandableListView;
    groupStatus =新INT [mGroupCollection.size()];

    setListEvent();
}

私人无效setListEvent(){

    mExpandableListView
            .setOnGroupExpandListener(新OnGroupExpandListener(){

                @覆盖
                公共无效onGroupExpand(INT为arg0){
                    // TODO自动生成方法存根
                    groupStatus [arg0都] = 1;
                }
            });

    mExpandableListView
            .setOnGroupCollapseListener(新OnGroupCollapseListener(){

                @覆盖
                公共无效onGroupCollapse(INT为arg0){
                    // TODO自动生成方法存根
                    groupStatus [arg0都] = 0;
                }
            });



}

@覆盖
公共字符串getChild(INT为arg0,诠释ARG1){
    // TODO自动生成方法存根
    返回mGroupCollection.get(arg0中).getStatesList()得到(ARG1).getStateName()。
}

@覆盖
众长getChildId(INT为arg0,诠释ARG1){
    // TODO自动生成方法存根
    返回0;
}

@覆盖
公共查看getChildView(INT为arg0,诠释ARG1,布尔ARG2,查看ARG3,
        ViewGroup中ARG4){
    // TODO自动生成方法存根

    ChildHolder childHolder;
    如果(参数3 == NULL){
        参数3 = LayoutInflater.from(mContext).inflate(
                R.layout.list_group_item,NULL);

        childHolder =新ChildHolder();

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

    childHolder.title.setText(mGroupCollection.get(arg0中).getStatesList()得到(ARG1).getStateName());
    返回ARG3;
}

@覆盖
公众诠释getChildrenCount(INT为arg0){
    // TODO自动生成方法存根
    返回mGroupCollection.get(arg0中).getStatesList()的大小()。
}

@覆盖
公共对象getGroup(INT为arg0){
    // TODO自动生成方法存根
    返回mGroupCollection.get(为arg0);
}

@覆盖
公众诠释getGroupCount(){
    // TODO自动生成方法存根
    返回mGroupCollection.size();
}

@覆盖
众长getGroupId(INT为arg0){
    // TODO自动生成方法存根
    返回将arg0;
}

@覆盖
公共查看getGroupView(INT为arg0,布尔ARG1,ARG2查看,ViewGroup中ARG3){
    // TODO自动生成方法存根
    GroupHolder groupHolder;
    如果(ARG2 == NULL){
        ARG2 = LayoutInflater.from(mContext).inflate(R.layout.list_group,
                空值);
        groupHolder =新GroupHolder();
        groupHolder.img =(ImageView的)arg2.findViewById(R.id.tag_img);
        groupHolder.title =(TextView中)arg2.findViewById(R.id.group_title);
        arg2.setTag(groupHolder);
    } 其他 {
        groupHolder =(GroupHolder)arg2.getTag();
    }
    如果(groupStatus [为arg0] == 0){
        groupHolder.img.setImageResource(R.drawable.group_down);
    } 其他 {
        groupHolder.img.setImageResource(R.drawable.group_up);
    }
    groupHolder.title.setText(mGroupCollection.get(arg0中).getGroupName());

    返回ARG2;
}

类GroupHolder {
    ImageView的IMG;
    TextView的称号;
}

类ChildHolder {
    TextView的称号;
}

@覆盖
公共布尔hasStableIds(){
    // TODO自动生成方法存根
    返回true;
}

@覆盖
公共布尔isChildSelectable(INT为arg0,诠释ARG1){
    // TODO自动生成方法存根
    的System.out.println(vcvv ..........+真);
    返回true;
}
 

}

活动类

  expandableListView =(ExpandableListView)findViewById(R.id.indiaParts);

    expandableListAdapter =新ExpandableListAdapter(StatesScreen.this,expandableListView,groupEntityList);
    expandableListView.setAdapter(expandableListAdapter);


    expandableListView.setOnChildClickListener(新OnChildClickListener(){

        @覆盖
        公共布尔onChildClick(ExpandableListView父,视图V,
                INT groupPosition,诠释childPosition,长ID){
            // TODO自动生成方法存根
            Toast.makeText(StatesScreen.this,嗨单击Toast.LENGTH_LONG).show();
            的System.out.println(点击.............. ////。+ groupPosition);
            意图所属分类=新的意图(StatesScreen.this,CategoryList.class);
            startActivity(所属分类);
            返回false;
        }
    });
 

解决方案

这个问题出现那么你的孩子的看法有自己的聚焦元素。您可以通过删除机器人将其删除:点击儿童布局,或设置为false:

 < XML版本=1.0编码=UTF-8&GT?;
   < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
  机器人:layout_width =FILL_PARENT
  机器人:layout_height =WRAP_CONTENT
  机器人:背景=@可绘制/ group_item_normal
  机器人:ID =@ + ID / groupItem
  <! - 从这里删除点击 - >
  <  - 或者Android!点击=假 - >
  >
 

如果您的孩子元素不具有任何可以点击的,可聚焦的元素,然后工作非标准的ListView可获得焦点。

对不起我的英文不好

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

解决方案

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