导航抽屉不显示在主页中

时间:2015-01-22 10:13:27

标签: android navigation-drawer

  • 我正在使用棒棒糖(api等级21)。我实施了导航 在我的应用程序中的抽屉。一切都很好。但我没有看到 输出结尾处的导航抽屉。

  • 我在运行时没有收到任何错误。我唯一的问题是,我不知道 为什么我最后没有得到导航抽屉。

  • 下面我发布了与之相关的代码。

ChapterActivity.java:

public class ChapterActivity extends ActionBarActivity implements OnItemClickListener {

private ActionBarDrawerToggle mDrawerToggle;
    private CharSequence mDrawerTitle;
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;

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

        mTitle = mDrawerTitle = getTitle();
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer,
                R.string.app_name, 

                R.string.app_name 
        ) {
            public void onDrawerClosed(View view) {
                setTitle(mTitle);
                // calling onPrepareOptionsMenu() to show action bar icons
                invalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {
                setTitle(mDrawerTitle);
                // calling onPrepareOptionsMenu() to hide action bar icons
                invalidateOptionsMenu();
            }
        };  

        mDrawerLayout.setDrawerListener(mDrawerToggle);
        initialize();
        listChapter.setOnItemClickListener(this);
        mDrawerList.setOnItemClickListener(this);
    }

     private void initialize() {
     ......
     actionBar();
    }

     public void actionBar() {

        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        actionBar.setBackgroundDrawable(new ColorDrawable(Color  
                .parseColor("#FFFFFF"))); 
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowTitleEnabled(false);

        actionBar.setCustomView(mCustomView);
        actionBar.setDisplayShowCustomEnabled(true);
    }

     public class getChapter extends AsyncTask<String, Void, String> {


        @Override
        protected String doInBackground(String... params) {

            chapterList = DatabaseQueryHelper.getInstance().getChapters(); 


            return null;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

        @Override
        protected void onPostExecute(String result) {

            ac = new AdapterChapter(ChapterActivity.this, chapterList); // AdapterChapter

            listChapter.setAdapter(ac); 
            mDrawerList.setAdapter(ac);                 

        }

    }
}

activity_chapter.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/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <RelativeLayout
            android:id="@+id/container"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" >

            <TextView
                android:id="@+id/textviewHeading"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="5dp"
                android:fontFamily="fonts/Dosis.otf"
                android:text="@string/heading_chapter"
                android:textSize="25sp"
                android:visibility="gone" />

            <ListView
                android:id="@+id/listviewChapters"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_above="@+id/imageviewBanner"
                android:layout_below="@+id/textviewHeading"
                android:layout_marginTop="5dp" >
            </ListView>

            <ImageView
                android:id="@+id/imageviewBanner"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:src="@drawable/banner"
                android:visibility="invisible" />
        </RelativeLayout>
    </FrameLayout>

    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/list_background"
        android:choiceMode="singleChoice"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector" />

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

styles.xml:

<resources>

    <style name="AppBaseTheme" parent="@style/Theme.AppCompat"></style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>
</resources>

最后我没有在主页上看到导航抽屉。任何人都可以帮我这个。我做错了。谢谢。

1 个答案:

答案 0 :(得分:2)

您似乎正在使用android.support.v4.app.ActionBarDrawerToggle。不推荐使用此类,您应该使用android.support.v7.app.ActionBarDrawerToggle,它也有不同的constructor

public ActionBarDrawerToggle (Activity activity, DrawerLayout drawerLayout, int openDrawerContentDescRes, int closeDrawerContentDescRes)
public ActionBarDrawerToggle (Activity activity, DrawerLayout drawerLayout, Toolbar toolbar, int openDrawerContentDescRes, int closeDrawerContentDescRes)

第一个链接与你的实际操作栏。您只需删除对ic_launcher的引用,然后在导入中将v4.app.ActionBarDrawerToggle切换为v7.app.ActionBarDrawerToggle

第二个与Toolbar视图绑定,我认为你没有实现。


我看到的第二个问题是在覆盖onDrawerOpened()onDrawerClosed()方法时缺少调用。你应该总是打电话给超级班:

        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            // other stuff
        }
{p>同样适用于onDrawerOpened()