导航抽屉 - 带ListView的标题视图 [英] Navigation drawer - Header View with ListView

查看:151
本文介绍了导航抽屉 - 带ListView的标题视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm currently creating and defining a navigation drawer. I want now a header view, like their on the google apps, above the item rows. I only found examples with RecyclerViews, and i dont want to use it. I have all finished with a ListView and all other stuff. Maybe someone can help me :) Thanks in advance

推荐答案

你可以创建 NavigationView 使用android设计支持库,无需创建listview或RecyclerView,它全部由android创建。

You can create NavigationView using android design support library without having pain to create listview or RecyclerView, its all created by android.

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

To add it to your project you need to add the android design support library to your project, add below line in build.gradle

compile 'com.android.support:design:22.2.0

查看android设计支持功能这里

Check out android design support features here

首先创建一个头文件(header.xml) )

First create a 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)

Next create a menu resource file, the items in the menu will be the items displayed in the drawer(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你可以看到我已经包含了一个工具栏和一个'FrameLayout`。当单击drawerlayout中的项目时,您可以替换片段。

Next create a DrawerLayout file, within the drawerlayout you can see I have included a Toolbar and a 'FrameLayout`. When the item in the drawerlayout is clicked you can replace fragment.

同样在其中的是包含以下参数的NavigationView:

Also within it is the NavigationView with these parameters:

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

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

app:headerLayout is the header.xml that we created in step 1. app:menu is the menu resource item i.e 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,

Next in your MainActivity extend AppcompatActivity,

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

Intialise NavigationView并调用setNavigationItemSelectedListener来获取点击事件,

Intialise NavigationView and call setNavigationItemSelectedListener to get click events,

@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;
                ...................

创建导航视图的一步一步程序转到此处

Step by step procedure to create navigationview go here

外观如何:

这篇关于导航抽屉 - 带ListView的标题视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆