java - 抽屉导航 - 带 ListView 的标题 View

标签 java android navigation-drawer

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

最佳答案

您可以使用 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”。单击抽屉布局中的项目时,您可以替换 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;
                ...................

创建导航 View 的分步过程去 here

它看起来如何:

enter image description here

关于java - 抽屉导航 - 带 ListView 的标题 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31220504/

相关文章:

java - 有没有办法使用 Java 和 JDBC 监听 Microsoft SQL 数据库表中的更改?

java - 接口(interface)实例如何访问Object类的方法?

java - 当无法使用 Android 将数据写入 Firebase 数据库时如何获取错误消息

java - 如何在模型 View 呈现器模式中使用存储库模式和交互器模式?

java - 具有多个 Activity 的抽屉导航。 - 空指针异常

java - 无法在屏幕右侧实现抽屉导航 - Android/Java

Java 8 流分组通过使用比较器

android - ScrollView 不在 fragment 中滚动

android 支持 v7 appcompat 在 android 2.3v 设备中显示异常,但在 4.2 设备中完美运行

java - 对象列表中的 Android java .contains() int