配置导航抽屉不会显示除背景之外的任何内容

时间:2014-08-11 07:04:25

标签: android navigation-drawer

我试图创建一个导航抽屉。我按照一些教程调整了一些代码以满足我的应用需求。现在问题是,我想要显示的项目列表没有显示。基本上我只是创建一个应该是包含我的列表内容的适配器。

以下是代码:

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</FrameLayout>

 <!-- The navigation drawer -->
<ListView android:id="@+id/left_slidermenu"
    android:layout_width="350dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="@drawable/bg_menu"/>

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

的strings.xml

<!-- Nav Drawer Menu Items -->
<string-array name="nav_drawer_items">
    <item >Dashboard</item>
    <item >Flashcard</item>
    <item >Qbank</item>
    <item >Video Assist</item>
    <item >Contact Us</item>
</string-array>

<!-- Nav Drawer List Item Icons -->
<!-- Keep them in order as the titles are in -->
<array name="nav_drawer_icons">
    <item>@drawable/ico_dashboard_squared</item>
    <item>@drawable/ico_flashback</item>
    <item>@drawable/ico_qbank</item>
    <item>@drawable/ico_contact_us</item>
    <item>@drawable/ico_contact_us</item>
</array>

NavigationDrawListAdapter

public class NavDrawerListAdapter extends BaseAdapter {
private Context context;
private String[] data;
private TypedArray data2;

public NavDrawerListAdapter(Context context, String[] data1, TypedArray data2){
    this.context = context;
    this.data = data1;
    this.data2 = data2;
}

/* (non-Javadoc)
 * @see android.widget.Adapter#getCount()
 */
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return 0;
}

/* (non-Javadoc)
 * @see android.widget.Adapter#getItem(int)
 */
@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

/* (non-Javadoc)
 * @see android.widget.Adapter#getItemId(int)
 */
@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

/* (non-Javadoc)
 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
 */
@Override
public View getView(int position, View convertView, ViewGroup parent) {
     if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater)
                    context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.drawer_list_item, null);
        }

        ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon);
        TextView txtTitle = (TextView) convertView.findViewById(R.id.title);

        imgIcon.setImageResource(data2.getInt(position, 0));        
        txtTitle.setText(data[position]);

        return convertView;
}

}

我的抽屉配置:

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerListLeft = (ListView) findViewById(R.id.left_slidermenu);

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;

    mDrawerListLeft.getLayoutParams().width = width - 150;
    mDrawerListLeft.setLayoutParams(mDrawerListLeft.getLayoutParams());

    // load slide menu items
    navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);

    // nav drawer icons from resources
    navMenuIcons = getResources()
            .obtainTypedArray(R.array.nav_drawer_icons);

    adapter = new NavDrawerListAdapter(getApplicationContext(),
            navMenuTitles, navMenuIcons);
    mDrawerListLeft.setAdapter(adapter);

这是屏幕截图

enter image description here

我实际上并不知道为什么列表不在那里,但我提供的背景就在那里。你可以注意到,我没有动作栏,因为我真的不需要动作吧。有任何想法吗?谢谢!

1 个答案:

答案 0 :(得分:0)

我认为您的问题是适配器。尝试修改它:

        @Override
        public int getCount()
        {
            return data1.length;
        }


        @Override
        public Object getItem(int position)
        {
            return data1[position];
        }


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