【问题标题】:ExpandableListView and the NavigationDrawerExpandableListView 和 NavigationDrawer
【发布时间】:2013-08-13 04:49:48
【问题描述】:

我遇到了一个非常奇怪的问题。我的逻辑很简单,我的应用程序使用NavigationDrawer 进行主应用程序导航,并且由于可用选项的复杂性,我想使用ExpandableListViev 来嵌套一些导航项。

问题?也很简单:物品列表加载到抽屉中;但是,它们不会扩展到显示孩子。

问题有什么理由不展开吗?我应该为此使用不同的 UI 小部件吗?

一些注意事项:我在不同的 Activity 上添加了相同的代码,但不是在抽屉上托管 ExpandableListViev,而是将它添加到一个简单的 LinearLayout 作为唯一元素,并且它确实运作良好

我的活动布局:

<android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

<ExpandableListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/secondary_color_holo"/>

</android.support.v4.widget.DrawerLayout>

活动代码:

private DrawerLayout mDrawerLayout;
private ExpandableListView mDrawerList;
private ExpandableDrawerAdapter mDrawerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ExpandableListView) findViewById(R.id.left_drawer);
    // set up the drawer's list view with items and click listener
    mDrawerAdapter = new ExpandableDrawerAdapter(this);
    mDrawerList.setAdapter(mDrawerAdapter);
    mDrawerList.setOnGroupClickListener(new DrawerGroupClickListener());
    mDrawerList.setOnChildClickListener(new DrawerChildClickListener());
}

最后是一个非常简单的适配器:

public class ExpandableDrawerAdapter extends BaseExpandableListAdapter {

private static final int CAMPAIGNS = 0;
private static final int CONVERSIONS = 1;
private static final int MYACCOUNT = 2;

private LayoutInflater mInflater;
private String[] mainNavItems, campaignNavItems, myAccountNavItems;

public ExpandableDrawerAdapter (Activity context) {
    this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.mainNavItems = context.getResources().getStringArray(R.array.main_nav_items);
    this.campaignNavItems = context.getResources().getStringArray(R.array.campaigns_nav_items);
    this.myAccountNavItems = context.getResources().getStringArray(R.array.my_account_nav_items);
}

public Object getChild(int groupPosition, int childPosition) {

    switch (groupPosition) {
        case CAMPAIGNS:
            return campaignNavItems[childPosition];
        case MYACCOUNT:
            return myAccountNavItems[childPosition];
        default:
            return "";
    }
}

public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

public View getChildView(final int groupPosition, final int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.drawer_child_list_item, null);
    }

    TextView childText = (TextView) convertView.findViewById(R.id.drawer_child_list_item_text);
    childText.setText((String) getChild(groupPosition, childPosition));

    return convertView;
}

public int getChildrenCount(int groupPosition) {
    switch (groupPosition) {
        case CAMPAIGNS:
            Constants.logMessage("Children for group position: " + groupPosition + " are: " + campaignNavItems.length);
            return campaignNavItems.length;
        case CONVERSIONS:
            Constants.logMessage("Children for group position: " + groupPosition + " are: " + 0);
            return 0;
        case MYACCOUNT:
            Constants.logMessage("Children for group position: " + groupPosition + " are: " + myAccountNavItems.length);
            return myAccountNavItems.length;
        default:
            return 0;
    }
}

public Object getGroup(int groupPosition) {
    return mainNavItems[groupPosition];
}

public int getGroupCount() {
    return mainNavItems.length;
}

public long getGroupId(int groupPosition) {
    return groupPosition;
}

public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.drawer_list_item,
                null);
    }

    TextView groupText = (TextView) convertView.findViewById(R.id.drawer_list_item_text);
    groupText.setText((String) getGroup(groupPosition));
    return convertView;
}

public boolean hasStableIds() {
    return true;
}

public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}
}

【问题讨论】:

  • 显而易见的问题是,如果您在这些数组中有孩子的值(我猜您有:))。我看到您在您的ExpandableListView 上设置了OnGroupClickListener,我希望您从该侦听器的onGroupClick() 回调中返回false,否则您将阻止折叠/展开事件,返回值为@987654332 @(意味着您已经处理了该事件,因此不需要其他操作)。
  • 嗯,文档并没有说明返回 true 会阻止默认的折叠和展开。奇怪的是,我在抽屉外应用了相同的逻辑,而且效果很好。我当时不在家,但这听起来确实是解决我的问题的正确方法,因为我对两个听众都是正确的。如果您愿意,请继续将其作为答案发布,一旦我回到家并测试返回 false,我就可以接受您的答案。
  • 每个项目是否扩展/收缩?或者您是否有一些可以扩展而另一些仍然是简单的可点击项目?

标签: android expandablelistview navigation-drawer


【解决方案1】:

正如我在评论中所说,请确保从您在ExpandableListView 上设置的DrawerGroupClickListener 返回false。如果您从侦听器的 onGroupClick() 回调中返回 true,这将阻止折叠/展开操作,因为事件是 considered handled

【讨论】:

    猜你喜欢
    • 2015-09-26
    • 2015-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多