Как реализовать макет навигационного ящика? - PullRequest
0 голосов
/ 30 марта 2020

Я хочу реализовать макет навигационного ящика, в котором я хочу использовать панель переключения, которая открывает или закрывает навигационный ящик.

Можете ли вы выслать мне код файла Основной активности java и xml?

Ответы [ 2 ]

0 голосов
/ 02 апреля 2020

Я сделал новый код, который отлично работает:

Navigation_Header. 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="240dp"
    android:background="#72B569"
    android:gravity="bottom"
    android:orientation="vertical"
    android:padding="10dp">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:text="Nazmul Islam"
        android:textColor="#fff"
        android:textSize="18dp"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="nazmul@voltagelab.com"
        android:textColor="#fff"
        android:textSize="15dp" />
</LinearLayout>

Navigation_menu. xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showInd="navigation_view">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/homes"
            android:icon="@drawable/ic_home_black_24dp"
            android:title="Home" />

        <item
            android:id="@+id/work"
            android:icon="@drawable/ic_work_black_24dp"
            android:title="Work" />
    </group>

    <item android:title="Communication">

        <menu>
            <item
                android:id="@+id/contact"
                android:icon="@drawable/ic_contact_phone_black_24dp"
                android:title="Contact" />
            <item
                android:id="@+id/share"
                android:icon="@drawable/ic_share_black_24dp"
                android:title="Share" />

        </menu>
    </item>
</menu>

Значения стилей. xml

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
</resources>

Manifext. xml

<activity android:name=".MainActivity"
            android:theme="@style/AppTheme.NoActionBar">

Activitymain. xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity"
    tools:openDrawer="start">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"
            android:background="@color/colorPrimary"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" />

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

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:headerLayout="@layout/nav_header"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:menu="@menu/nav_menu"/>
</androidx.drawerlayout.widget.DrawerLayout>

MainActivity. java

package com.example.navigationscroll;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.android.material.navigation.NavigationView;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    Toolbar toolbar;
    DrawerLayout drawerLayout;
    ActionBarDrawerToggle toggle;
    NavigationView navigationView;


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

        toolbar = findViewById(R.id.toolbar);
        drawerLayout = findViewById(R.id.drawer);
        navigationView = findViewById(R.id.navigation_view);

        setSupportActionBar(toolbar);

        toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.nav_drawer_open, R.string.nav_drawer_close);

        drawerLayout.addDrawerListener(toggle);

        toggle.syncState();

        navigationView.setNavigationItemSelectedListener(this);

//------- Rotate not refresh new state --------------------

        if (savedInstanceState==null){
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_frame,
                    new first()).commit();
        }
    }

    @Override
    public void onBackPressed() {
        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
            drawerLayout.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case R.id.homes:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_frame,
                        new first()).commit();
                break;
            case R.id.work:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_frame,
                        new second()).commit();
                break;
            case R.id.share:
                Toast.makeText(this, "Share Press", Toast.LENGTH_SHORT).show();
                break;
        }

        drawerLayout.closeDrawer(GravityCompat.START);
        return true;
    }
}
0 голосов
/ 31 марта 2020

Я сделал с простым кодом:

res> new> android файл ресурсов> create nav_menu

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view">

    <group android:checkableBehavior="single">
        <item android:id="@+id/home_id"
            android:title="Home"
            android:icon="@drawable/home"/>

        <item android:id="@+id/profile_id"
            android:title="Profile"
            android:icon="@drawable/contact"/>

        <item android:id="@+id/chat_id"
            android:title="Chat"
            android:icon="@drawable/chat"/>
    </group>

    <item android:title="Communicate">
        <menu>
            <item android:id="@+id/home_share"
                android:title="Share"
                android:icon="@drawable/share"/>

            <item android:id="@+id/contact"
                android:title="Contact"
                android:icon="@drawable/person"/>
        </menu>
    </item>
</menu>

Заголовок навигации

<?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="160dp"
    android:background="#A15858"
    android:orientation="vertical"
    android:padding="10dp"
    >
    <ImageView
        android:src="@mipmap/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Nazmul Islam"
        android:textColor="#ffffff"
        android:textStyle="bold"
        android:paddingTop="10dp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="nazmulisrony@gmail.com"
        android:textColor="#fff"/>


</LinearLayout>

Строка xml

<resources>
    <string name="app_name">Navigation Drawer</string>
    <string name="open_string">Open</string>
    <string name="close_string">Close</string>
</resources>

Activity Main

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:openDrawer="start"
    android:id="@+id/drawer"
    tools:context=".MainActivity">

    <com.google.android.material.navigation.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/navigationid"
        app:menu="@menu/nav_menu_layout"
        android:background="#ffffff"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"/>

</androidx.drawerlayout.widget.DrawerLayout>

Сейчас добавить MainActivity

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    DrawerLayout drawerLayout;
    ActionBarDrawerToggle toggle;
    NavigationView navigationView;
    Intent intent;

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

        navigationView=findViewById(R.id.navigationid);

        navigationView.setNavigationItemSelectedListener(this);

        drawerLayout=findViewById(R.id.drawer);
        toggle=new ActionBarDrawerToggle(this,drawerLayout,R.string.open_string,R.string.close_string);

        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

затем добавить эти две вещи

@Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (toggle.onOptionsItemSelected(item)){
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        if (menuItem.getItemId()==R.id.home_id){
            intent=new Intent(this,Home.class);
            startActivity(intent);
        }
        return false;
    }
}
...