问题描述
我有一个pretty的奇怪的问题。我的逻辑很简单,我的应用程序使用了 NavigationDrawer
主应用程序的导航,并且由于可用选项的复杂性,我想用一个 ExpandableListViev
嵌套一些导航项目。
问题过于简单:在抽屉里的物品装载清单;然而,他们不展开显示孩子。
问题是否有任何理由会不会扩大?我应该使用一个不同的UI小部件呢?
若干注记:我添加了相同的code在不同的活动,但不是主持 ExpandableListViev
抽屉,我有它添加一个简单的的LinearLayout
作为唯一的元素,它的也工作得很好有。
我的布局为活动:
< android.support.v4.widget.DrawerLayout
的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:ID =@ + ID / drawer_layout
机器人:layout_width =match_parent
机器人:layout_height =match_parent
工具:上下文=MainActivity。>
<的FrameLayout
机器人:ID =@ + ID / content_frame
机器人:layout_width =match_parent
机器人:layout_height =match_parent/>
< ExpandableListView
机器人:ID =@ + ID / left_drawer
机器人:layout_width =240dp
机器人:layout_height =match_parent
机器人:layout_gravity =开始
机器人:背景=@色/ secondary_color_holo/>
< /android.support.v4.widget.DrawerLayout>
该活动code:
私人DrawerLayout mDrawerLayout;
私人ExpandableListView mDrawerList;
私人ExpandableDrawerAdapter mDrawerAdapter;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
mDrawerLayout =(DrawerLayout)findViewById(R.id.drawer_layout);
mDrawerList =(ExpandableListView)findViewById(R.id.left_drawer);
//设置抽屉的列表视图项,然后单击监听器
mDrawerAdapter =新ExpandableDrawerAdapter(本);
mDrawerList.setAdapter(mDrawerAdapter);
mDrawerList.setOnGroupClickListener(新DrawerGroupClickListener());
mDrawerList.setOnChildClickListener(新DrawerChildClickListener());
}
最后一个非常简单的适配器:
公共类ExpandableDrawerAdapter扩展BaseExpandableListAdapter {
私有静态最终诠释CAMPAIGNS = 0;
私有静态最终诠释CONVERSIONS = 1;
私有静态最终诠释我的帐户= 2;
私人LayoutInflater mInflater;
私有String [] mainNavItems,campaignNavItems,myAccountNavItems;
公共ExpandableDrawerAdapter(活动范围内){
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)。
}
公共对象getChild(INT groupPosition,诠释childPosition){
开关(groupPosition){
案例CAMPAIGNS:
返回campaignNavItems [childPosition]
案例我的帐户:
返回myAccountNavItems [childPosition]
默认:
返回 ;
}
}
众长getChildId(INT groupPosition,诠释childPosition){
返回childPosition;
}
公共查看getChildView(最终诠释groupPosition,最终诠释childPosition,
布尔isLastChild,查看convertView,ViewGroup中父){
如果(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((字符串)getChild(groupPosition,childPosition));
返回convertView;
}
公众诠释getChildrenCount(INT groupPosition){
开关(groupPosition){
案例CAMPAIGNS:
Constants.logMessage(为集团的立场孩子:+ groupPosition +是:+ campaignNavItems.length);
返回campaignNavItems.length;
大小写转换:
Constants.logMessage(为集团的立场孩子:+ groupPosition +是:+ 0);
返回0;
案例我的帐户:
Constants.logMessage(为集团的立场孩子:+ groupPosition +是:+ myAccountNavItems.length);
返回myAccountNavItems.length;
默认:
返回0;
}
}
公共对象getGroup(INT groupPosition){
返回mainNavItems [groupPosition]
}
公众诠释getGroupCount(){
返回mainNavItems.length;
}
众长getGroupId(INT groupPosition){
返回groupPosition;
}
公共查看getGroupView(INT groupPosition,布尔isExpanded,
查看convertView,ViewGroup中父){
如果(convertView == NULL){
convertView = mInflater.inflate(R.layout.drawer_list_item,
空值);
}
TextView的groupText =(TextView中)convertView.findViewById(R.id.drawer_list_item_text);
groupText.setText((字符串)getGroup(groupPosition));
返回convertView;
}
公共布尔hasStableIds(){
返回true;
}
公共布尔isChildSelectable(INT groupPosition,诠释childPosition){
返回true;
}
}
正如我在我的评论说,请确保您返回假
从 DrawerGroupClickListener
您在 ExpandableListView
设置。如果返回真
从听者的 onGroupClick()
回调,这将阻止折叠/展开行动,该事件是<一个href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.3_r2.1/android/widget/ExpandableListView.java#ExpandableListView.handleItemClick%28android.view.View,int,long%29">considered处理。
I am having a pretty strange issue. My logic is simple, my app uses a NavigationDrawer
for main application navigation, and due to the complexity of the options available, I want to use an ExpandableListViev
to nest some navigation items.
The issue? Simple too: The list of items loads on the drawer; however, they don't expand to show the children.
The Question Is there any reason it wouldn't expand? Should I use a different UI widget for this?
Some Notes: I added the same code on a different Activity, but instead of hosting the ExpandableListViev
on the drawer, I had it added on a simple LinearLayout
as the only element, and it does work well there.
My Layout for the Activity:
<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>
The Activity code:
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());
}
And finally a very simple adapter:
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;
}
}
As I said in my comment, make sure that you return false
from the DrawerGroupClickListener
that you set on the ExpandableListView
. If you return true
from the listener's onGroupClick()
callback this will block the collapse/expand action as the event is considered handled.
这篇关于ExpandableListView和NavigationDrawer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!