【问题标题】:Android :- Custom ExpandableListView : cannot expand group item to display child item in CustomListViewAndroid :- Custom ExpandableListView : 无法展开组项以在 CustomListView 中显示子项
【发布时间】:2012-09-29 18:29:48
【问题描述】:

我正在尝试在 SherlockFragment 中实现自定义 ExpandableListView。我按照这个sample 扩展了 BaseExpandableListAdapter 并创建了我的自定义适配器。

这是我的问题,当我的可扩展列表显示在屏幕上时,我可以看到所有组项,但是当我单击其中一个时,应该显示在它下方的子项却没有' t 出现

  1. 我尝试调试它,我放入适配器的 onGroupExpandListener 从未被调用,实际上我在适配器的不同方法中放置了断点,当我单击其中一个组项时从未调用过.

  2. 我尝试修改定义组项和子项的 xml 文件,以使它们可单击或聚焦,但没有任何改变。我还试图删除我放在那里的editText、Button和CheckBox,认为这可能会造成冲突......

  3. 我认为问题可能是由于将 ExpandableListView 与 SherlockFragment 结合使用时出现的一些不兼容问题,但根据开发人员的forum 并非如此。

所以我现在真的不知道在哪里看,这可能只是我在适配器中犯的一个菜鸟错误...... 任何帮助都会很棒,

提前致谢!


这是我的代码:

public class BuildingExpandalbeListAdapter extends BaseExpandableListAdapter {

private Context mContext;
private ExpandableListView mExpandableListView;
private SideEntity[] mSidesCollection;
private int[] groupStatus;

public BuildingExpandalbeListAdapter(Context pContext,
        ExpandableListView pExpandableListView,
        SideEntity[] pSidesCollection) {
    mContext = pContext;
    mSidesCollection = pSidesCollection;
    mExpandableListView = pExpandableListView;
    groupStatus = new int[mSidesCollection.length];
    mExpandableListView.setClickable(true);     
    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 Object getChild(int groupPosition, int childPosition) {
    return mSidesCollection[groupPosition].getSegmentEntity(childPosition)
            .getName();
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return mSidesCollection[groupPosition].getSegmentEntity(childPosition)
            .getId();
}

@Override
public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ChildHolder childHolder;
    if (convertView == null) {
        convertView = LayoutInflater.from(mContext).inflate(
                R.layout.building_list_item, null);

        childHolder = new ChildHolder();

        childHolder.editText1 = (EditText) convertView
                .findViewById(R.id.editText1);
        childHolder.checkBox1 = (CheckBox) convertView
                .findViewById(R.id.checkBox1);
        convertView.setTag(childHolder);
    } else {
        childHolder = (ChildHolder) convertView.getTag();
    }

    childHolder.editText1.setText(mSidesCollection[groupPosition]
            .getSegmentEntity(childPosition).getName());
    childHolder.checkBox1.setChecked(mSidesCollection[groupPosition]
            .getSegmentEntity(childPosition).hasDoor());
    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    // TODO Auto-generated method stub
    return mSidesCollection[groupPosition].getSegmentsCollection().size();
}

@Override
public Object getGroup(int groupPosition) {
    // TODO Auto-generated method stub
    return mSidesCollection[groupPosition];
}

@Override
public int getGroupCount() {
    // TODO Auto-generated method stub
    return mSidesCollection.length;
}

@Override
public long getGroupId(int groupPosition) {
    // TODO Auto-generated method stub
    return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    GroupHolder groupHolder;
    if (convertView == null) {
        convertView = LayoutInflater.from(mContext).inflate(
                R.layout.building_list_group, null);
        groupHolder = new GroupHolder();
        groupHolder.editText1 = (EditText) convertView
                .findViewById(R.id.editText1);
        groupHolder.editText2 = (EditText) convertView
                .findViewById(R.id.editText2);
        convertView.setTag(groupHolder);
    } else {
        groupHolder = (GroupHolder) convertView.getTag();
    }

    groupHolder.editText1
            .setText(mSidesCollection[groupPosition].getName());
    groupHolder.editText2.setText(Integer
            .toString(mSidesCollection[groupPosition]
                    .getSegmentsCollection().size()));

    return convertView;
}

class GroupHolder {
    EditText editText1;
    EditText editText2;
}

class ChildHolder {
    EditText editText1;
    CheckBox checkBox1;
}

@Override
public boolean hasStableIds() {
    // TODO Auto-generated method stub
    return true;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    return true;
}

}

这是我使用此适配器的 SherlockFragment:

public class BuildingFragment extends SherlockFragment {

private ViewGroup myViewGroup;
private View v;
private SideEntity[] mSideCollection;

private BuildingsDbAdapter buildingDataBase;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    if (container == null) {
        return null;
    }
    myViewGroup = container;
    myViewGroup.removeAllViews();
    v = inflater.inflate(R.layout.building_data_layout, container, false);
    buildingDataBase = new BuildingsDbAdapter(getSherlockActivity());
    return v;
}

@Override
public void onResume() {
    super.onResume();
    buildingDataBase.open();
    mSideCollection = BuildingsDbAdapter
            .fetchSideMatchingBuildingId(CustomTabFragmentActivity.mBuildingId);
    for (int i = 0; i < mSideCollection.length; i++) {
        BuildingsDbAdapter.fetchSegmentMatchingSideId(
                mSideCollection[i].getId(), mSideCollection[i]);
    }
    ExpandableListView mExpandableListView = (ExpandableListView) v
            .findViewById(R.id.expandableListView1);
    BuildingExpandalbeListAdapter mAdapter = new BuildingExpandalbeListAdapter(
            v.getContext().getApplicationContext(), mExpandableListView,
            mSideCollection);
    mExpandableListView.setAdapter(mAdapter);
    buildingDataBase.close();
}
}

这是我的 xml 文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center_vertical"
        android:layout_marginLeft="10dip"
        android:layout_weight="3"
        android:singleLine="true"
        android:text="Building name: "
        android:textColor="@android:color/black"
        android:textSize="13dip" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center_vertical"
        android:layout_marginLeft="10dip"
        android:layout_weight="4"
        android:singleLine="true"
        android:text="Columbia Tower"
        android:textColor="@android:color/black"
        android:textSize="15dip"
        android:textStyle="italic" />

   <!--  <Button
        android:id="@+id/button1"
        android:layout_width="0dip"
        android:layout_height="30dp"
        android:layout_gravity="right|center_vertical"
        android:layout_marginRight="10dip"
        android:layout_weight="1"
        android:background="@color/honeycombish_blue"
        android:drawableTop="@drawable/edit_query"
        android:gravity="center_vertical|center_horizontal" /> -->
</LinearLayout>

<ExpandableListView
    android:id="@+id/expandableListView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:scrollbars="none"
    android:divider="@android:color/black"
    android:clipChildren="false"
    android:focusable="true" >
</ExpandableListView>

</LinearLayout>

group_item:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:gravity="center_vertical"
android:orientation="vertical" >

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/RelativeLayout"
    android:layout_marginTop="6dip"
    android:layout_toLeftOf="@+id/button1"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center_vertical"
        android:layout_marginLeft="5dip"
        android:layout_weight="2"
        android:singleLine="true"
        android:text="Side: "
        android:textColor="@android:color/black"
        android:textSize="13dip" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center_vertical"
        android:layout_marginLeft="5dip"
        android:layout_weight="2"
        android:singleLine="true"
        android:textColor="@android:color/black"
        android:textSize="15dip"
        android:textStyle="italic" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center_vertical"
        android:layout_marginLeft="5dip"
        android:layout_weight="5"
        android:singleLine="true"
        android:text="Number of Segment:"
        android:textColor="@android:color/black"
        android:textSize="13dip" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center_vertical"
        android:layout_marginLeft="5dip"
        android:layout_weight="2"
        android:ems="10"
        android:inputType="number"
        android:text="0"
        android:textSize="13dip" />
</LinearLayout>

<Button
    android:id="@+id/button1"
    android:layout_width="30dip"
    android:layout_height="30dip"
    android:layout_alignBottom="@+id/linearLayout1"
    android:layout_alignParentRight="true"
    android:layout_marginLeft="2dip"
    android:background="@android:color/transparent"
    android:drawableBottom="@drawable/ic_edit_shape"
    android:gravity="bottom|center_horizontal" />

</RelativeLayout>

列表项:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:orientation="vertical" >

<Button
    android:id="@+id/button1"
    android:layout_width="30dip"
    android:layout_height="30dip"
    android:layout_alignBottom="@+id/linearLayout1"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="2dip"
    android:background="@android:color/transparent"
    android:drawableBottom="@drawable/ic_edit_shape"
    android:gravity="bottom|center_horizontal" />

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/RelativeLayout"
    android:layout_marginTop="6dip"
    android:layout_toRightOf="@+id/button1"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center_vertical"
        android:layout_marginLeft="5dip"
        android:layout_weight="2"
        android:singleLine="true"
        android:text="Name: "
        android:textColor="@android:color/black"
        android:textSize="13dip" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center_vertical"
        android:layout_marginLeft="5dip"
        android:layout_weight="2"
        android:singleLine="true"
        android:text="Shop1"
        android:textColor="@android:color/black"
        android:textSize="15dip"
        android:textStyle="italic" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="0dip"
        android:layout_height="30dip"
        android:layout_weight="1"
        android:hint="Door" />
</LinearLayout>

</RelativeLayout>

【问题讨论】:

  • 您确定mSidesCollection[groupPosition].getSegmentsCollection().size(); 返回的内容与0 不同吗?另外我不知道您是否尝试过,但将您的视图从组布局设置为不可聚焦android:focusable="false"
  • @Luksprog 感谢您的快速回答,我根据我尝试点击的每个组项检查了mSidesCollection[groupPosition],里面至少有一个段实体,所以mSidesCollection[groupPosition].getSegmentsCollection().size(); 通常至少会返回1.但问题是,无论我点击组项目多少次或点击它的位置,方法public int getChildrenCount(int groupPosition)都不会被调用,我的适配器的其他方法也不会被调用。我还尝试将我的组布局中的每个视图都设置为不可聚焦,但没有更多成功。
  • 您确定没有调用getChildrenCount 吗?如果是这种情况,则说明您的组点击次数正在消耗,否则应调用该方法。您可以尝试在getGroupView 方法(view.setFocusable(false))的代码中使EditText(和按钮,所有视图)不可聚焦吗?
  • 仅供参考,我用另一个片段替换了膨胀我的自定义 ExpandableListView 的片段。这个膨胀而不是 code sample 中提供的 ExpandableListView,它帮助我创建了我的适配器。显然,这个 ExpandableListView 按预期工作,所以我们可以排除 ActionBarSherlock 和我的自定义 ListView 之间的冲突。问题肯定出在我的适配器或其中一个 xml 文件中。像往常一样,任何建议都会非常感激:)
  • 我真的不明白可能是什么问题。我的猜测是布局中的某些内容,我建议您剥离行和分组项目,直到只剩下一个简单的 TextView 并开始添加其他视图并测试以查看是否有相同的问题。如果一个简单的 TextView 工作但添加其他小部件不起作用,那么您在 xml 中有问题。否则,即使一个简单的 TextView 也不起作用,您的适配器中有一些东西。

标签: android actionbarsherlock expandablelistview


【解决方案1】:

好的,我发现我的错误,问题实际上来自 EditText 和我在组项目中使用的 Button。当我将此小部件设置为不可点击且不可聚焦时,对我的组项目的点击会正常执行。

我应该早点检测到这个冲突,我之前尝试删除我的 Button 和我的 EditText,但我忘记了在某些时候我曾尝试将 android:clickable="true" 添加到我的 RelativeLayout(认为它会允许点击我的组项目),但它也产生了冲突:/ 无论如何,如果有人有类似的问题,请记住检查您的组项目中的任何视图是否可点击或可聚焦并将它们设置为android:clickable="false" android:focusable="false"

【讨论】:

  • 我认为这是您的布局中的某些内容。不要忘记接受您的回答。
  • 我也有 EditText。但我需要在其中输入一些文本。按钮和组项目现在是可点击的。但是我怎样才能让edittext接受输入。它现在不接受输入。
  • @Bhargavi 即使您将 editText 设置为不可点击,您也可以为它们设置 OnClickListener,唯一的问题是显示和隐藏您需要手动执行此操作的软键盘,使用 InputMethodManager mgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); mgr.showSoftInput(editText, InputMethodManager.SHOW_FORCED);mgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);。您还需要实现一个 TextWatcher 来检测 onTextChanged。希望对你有帮助
猜你喜欢
  • 2012-10-31
  • 1970-01-01
  • 2014-09-05
  • 2014-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-22
相关资源
最近更新 更多