【问题标题】:Android: navigationDrawer not showing itemsAndroid:导航抽屉不显示项目
【发布时间】:2014-10-20 11:04:34
【问题描述】:

我正在开发一个使用navigationDrawer 的应用程序。我想我把所有的东西都设置好了,但侧边栏不包含我的项目。我怎样才能做到这一点?

Main.java:

private DrawerLayout drawerLayout;
    private ListView drawerList;
    private ActionBarDrawerToggle drawerToggle;
    private String appTitle;
    
    // slide menu items
    private String[] menuTitles;
    private TypedArray menuIcons;
 
    private ArrayList<DrawerItem> drawerItems;
    private DrawerAdapter drawerAdapter;
    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getApplicationContext();
        setContentView(R.layout.activity_main);
        appTitle = getResources().getString(R.string.app_name);
     
        createDrawer();
        
        if (savedInstanceState == null) {
            // on first time display view for first nav item
            displayView(0);
        }
    }
    
    /**
     * Displaying fragment view for selected nav drawer list item
     * */
    private void displayView(int position) {
        // update the main content by replacing fragments
        Fragment fragment = null;
        switch (position) {
        case 0:
            fragment = new HomeFragment();
            break;
        case 1:
            fragment = new SecondFragment();
            break;
 
        default:
            break;
        }
 
        if (fragment != null) {
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.fragment_container, fragment).commit();
 
            // update selected item and title, then close the drawer
            drawerList.setItemChecked(position, true);
            drawerList.setSelection(position);
            setTitle(menuTitles[position]);
            drawerLayout.closeDrawer(drawerList);
        } else {
            // error in creating fragment
            Log.e("MainActivity", "Error in creating fragment");
        }
    }
    
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // display view for selected nav drawer item
        displayView(position);
    }

    private void createDrawer(){
         // load slide menu items
        menuTitles = getResources().getStringArray(R.array.drawer_items);
 
        // nav drawer icons from resources
        menuIcons = getResources().obtainTypedArray(R.array.drawer_icons);
        
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawerList = (ListView) findViewById(R.id.list_slidermenu);
        
        drawerItems = new ArrayList<DrawerItem>();
        
        for (int i=0; i<menuTitles.length; i++){
            drawerItems.add(new DrawerItem(menuTitles[i], menuIcons.getIndex(i)));
        }
        
        menuIcons.recycle();
    
        drawerAdapter = new DrawerAdapter(context, drawerItems);
        drawerList.setAdapter(drawerAdapter);
        
     // enabling action bar app icon and behaving it as toggle button
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);
        
       drawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
                R.drawable.ic_drawer, //nav menu toggle icon
                R.string.app_name, // nav drawer open - description for accessibility
                R.string.app_name // nav drawer close - description for accessibility
        ){
           public void onDrawerClosed(View view) {
               getActionBar().setTitle(appTitle);
               // calling onPrepareOptionsMenu() to show action bar icons
               invalidateOptionsMenu();
           }

           public void onDrawerOpened(View drawerView) {
               getActionBar().setTitle(appTitle);
               // calling onPrepareOptionsMenu() to hide action bar icons
               invalidateOptionsMenu();
           }
       };
       drawerLayout.setDrawerListener(drawerToggle);
       
       
    }
    
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        drawerToggle.syncState();
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // toggle nav drawer on selecting action bar app icon/title
        if (drawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        // Handle action bar actions click
        switch (item.getItemId()) {
        case R.id.action_settings:
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggls
        drawerToggle.onConfigurationChanged(newConfig);
    }

main_activity.xml:

<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="idlike.doctor.Main" >

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

   
    <!-- Listview to display slider menu -->
    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        
        android:divider="@color/Blue"
        android:dividerHeight="1dp"       
        android:background="@color/Black"/>

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

drawer_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@drawable/drawer_selector" >
    
    <ImageView
        android:id="@+id/icon"
        android:layout_width="75dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_gravity="center_vertical" />
 
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:textColor="@color/Orange"
        android:textSize="30sp"
        android:gravity="center_vertical" />

</LinearLayout>

【问题讨论】:

  • 您的R.array.drawer_items 是否包含您的strings.xml 中的字符串?
  • 调试检查数组中的元素可能是数组将为空
  • 是的,数组中填充了正确的字符串

标签: java android navigation-drawer items


【解决方案1】:

[已解决] 我写了一行将项目添加到适配器:

for (int i=0; i<menuTitles.length; i++){
            drawerItems.add(new DrawerItem(menuTitles[i], menuIcons.getIndex(i)));
        }

变成:

for (int i=0; i<menuTitles.length; i++){
            drawerItems.add(new DrawerItem(menuTitles[i], menuIcons.getResourceId(i, -1)));
        }

这个错误应该会阻止图标显示,但由于某种原因甚至没有显示文本....

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-21
    • 1970-01-01
    • 2016-01-04
    • 2020-12-08
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多