【问题标题】:Navigation drawer - Header View with ListView导航抽屉 - 带有 ListView 的标题视图
【发布时间】:2015-07-04 12:16:09
【问题描述】:

我目前正在创建和定义一个导航抽屉。我现在想要一个标题视图,就像他们在谷歌应用程序上一样,在项目行上方。我只找到了 RecyclerViews 的示例,我不想使用它。我已经完成了一个 ListView 和所有其他的东西。也许有人可以帮助我:)提前谢谢

【问题讨论】:

标签: java android navigation-drawer


【解决方案1】:

您可以使用android设计支持库创建NavigationView,而无需创建listview或RecyclerView,它们都是由android创建的。

要将其添加到您的项目中,您需要将 android 设计支持库添加到您的项目中,在 build.gradle 中添加以下行

compile 'com.android.support:design:22.2.0

查看android设计支持功能here

首先创建一个header(header.xml)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="190dp"
    android:background="@drawable/background_material"
    android:orientation="vertical"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="nyname"
</RelativeLayout>

接下来创建一个菜单资源文件,菜单中的项目将是抽屉中显示的项目(drawer.xml)

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">

        <item
            android:id="@+id/first1"
            android:checked="false"
            android:icon="@drawable/icon1"
            android:title="@string/string1" />

        <item
            android:id="@+id/second2"
            android:checked="false"
            android:icon="@drawable/icon2"
            android:title="@string/string2" />
</menu>

接下来创建一个DrawerLayout 文件,在drawerlayout 中你可以看到我包含了一个Toolbar 和一个“FrameLayout”。当点击drawerlayout中的item时,可以替换fragment。

其中还有带有这些参数的 NavigationView:

app:headerLayout="@layout/header" 
app:menu="@menu/drawer"
android:layout_gravity="start"

app:headerLayout 是我们在步骤 1 中创建的 header.xml。 app:menu 是菜单资源项,即drawer.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="vertical"
        >
        <include
            android:id="@+id/toolbar" 
            layout="@layout/tool_bar"
        />
        <FrameLayout
            android:id="@+id/frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </FrameLayout>

    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:layout_gravity="start"
        app:headerLayout="@layout/header"
        app:menu="@menu/drawer"
        />
</android.support.v4.widget.DrawerLayout>

接下来在您的 MainActivity 中扩展 AppcompatActivity,

public class MainActivity extends AppCompatActivity {
............................................

初始化 NavigationView 并调用 setNavigationItemSelectedListener 获取点击事件,

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

    // Initializing Toolbar and setting it as the actionbar
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    //Initializing NavigationView
    navigationView = (NavigationView) findViewById(R.id.navigation_view);

    //Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

        // This method will trigger on item Click of navigation menu
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {

            //Checking if the item is in checked state or not, if not make it in checked state
            if(menuItem.isChecked()) menuItem.setChecked(false);
            else menuItem.setChecked(true);

            //Closing drawer on item click
            drawerLayout.closeDrawers();

            //Check to see which item was being clicked and perform appropriate action
            switch (menuItem.getItemId()){
                //Replacing the main content with ContentFragment 
                case R.id.first1:
                    SomeFragment fragment = new SomeFragment();
                    android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.frame,fragment);
                    fragmentTransaction.commit();
                    return true;
                ...................

创建导航视图的分步过程 go here

它的样子:

【讨论】:

  • 非常感谢,但我还有一个问题。现在我有一个操作栏,并没有定义我自己的工具栏,但现在我需要一个带有 appcompat 的样式,但是当我使用父母中的某个人时,我收到错误:“尝试调用虚拟方法 'android.content.Context android. app.ActionBar.getThemedContext()' 在空对象引用上”,在设置适配器的导航抽屉中调用。你有什么解决建议吗? :) 还是我应该创建工具栏?
  • 您能否将其作为一个单独的问题发布,因为需要更多详细信息才能解决它。它还会引起更多的stackoverflows关注,它们也可以提供帮助。创建它后,您可以发布问题在这里...
  • 如果对您有帮助,您可以将我的答案标记为已回答...我将研究其他问题
  • 我也发布了我对您其他问题的回答。请检查一下。如果对您有帮助,请不要忘记投票/标记已回答
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-14
  • 1970-01-01
  • 1970-01-01
  • 2016-01-22
  • 1970-01-01
  • 2017-05-11
相关资源
最近更新 更多