Android Jetpack导航-带抽屉项的自定义操作

时间:2018-10-08 10:07:40

标签: android navigation-drawer android-jetpack android-architecture-navigation

我将新的Jetpack Android Navigation与“抽屉布局”结合使用。当在Drawer XML中使用相同的ID并与Navigation Graph中的Fragments结合使用时,一切都会按预期进行。我设置了所有内容:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val navController = findNavController(R.id.navigation_host_fragment)
        setupActionBarWithNavController(navController, find(R.id.drawer_layout))
        val navigationView = findViewById<NavigationView>(R.id.nav_view)
        navigationView.setupWithNavController(findNavController(R.id.navigation_host_fragment))
}

我现在也想触发自定义操作/代码,并且在单击“抽屉”菜单中的某个项目时不执行片段交易。 我具有以下菜单,并且希望在单击“注销”时注销用户

Current menu with logout item

3 个答案:

答案 0 :(得分:0)

我找到了解决方法:

val navigationView = findViewById<NavigationView>(R.id.nav_view)
val logoutItem = navigationView.menu.findItem(R.id.nav_logout)
logoutItem.setOnMenuItemClickListener {
     toast("Log me out")
     true
}

答案 1 :(得分:0)

我也找到了解决方法。

首先,您需要创建自定义NavigationUI。

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

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

import java.lang.ref.WeakReference;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.navigation.NavController;
import androidx.navigation.NavDestination;
import androidx.navigation.ui.NavigationUI;

public class CustomNavigationUI {

    public static void setupWithNavController(@NonNull final NavigationView navigationView,
                                              @NonNull final NavController navController,
                                              @Nullable final NavigationView.OnNavigationItemSelectedListener customListener) {
        navigationView.setNavigationItemSelectedListener(
                item -> {
                    boolean handled = NavigationUI.onNavDestinationSelected(item, navController);
                    if (handled) {
                        ViewParent parent = navigationView.getParent();
                        if (parent instanceof DrawerLayout) {
                            ((DrawerLayout) parent).closeDrawer(navigationView);
                        } else {
                            BottomSheetBehavior bottomSheetBehavior =
                                    findBottomSheetBehavior(navigationView);
                            if (bottomSheetBehavior != null) {
                                bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
                            }
                        }
                    } else {
                        if (customListener != null) {
                            customListener.onNavigationItemSelected(item);
                        }
                    }
                    return handled;
                });
        final WeakReference<NavigationView> weakReference = new WeakReference<>(navigationView);
        navController.addOnDestinationChangedListener(
                new NavController.OnDestinationChangedListener() {
                    @Override
                    public void onDestinationChanged(@NonNull NavController controller,
                                                     @NonNull NavDestination destination, @Nullable Bundle arguments) {
                        NavigationView view = weakReference.get();
                        if (view == null) {
                            navController.removeOnDestinationChangedListener(this);
                            return;
                        }
                        Menu menu = view.getMenu();
                        for (int h = 0, size = menu.size(); h < size; h++) {
                            MenuItem item = menu.getItem(h);
                            item.setChecked(matchDestination(destination, item.getItemId()));
                        }
                    }
                });
    }

    static BottomSheetBehavior findBottomSheetBehavior(@NonNull View view) {
        ViewGroup.LayoutParams params = view.getLayoutParams();
        if (!(params instanceof CoordinatorLayout.LayoutParams)) {
            ViewParent parent = view.getParent();
            if (parent instanceof View) {
                return findBottomSheetBehavior((View) parent);
            }
            return null;
        }
        CoordinatorLayout.Behavior behavior = ((CoordinatorLayout.LayoutParams) params)
                .getBehavior();
        if (!(behavior instanceof BottomSheetBehavior)) {
            // We hit a CoordinatorLayout, but the View doesn't have the BottomSheetBehavior
            return null;
        }
        return (BottomSheetBehavior) behavior;
    }

    @SuppressWarnings("WeakerAccess") /* synthetic access */
    static boolean matchDestination(@NonNull NavDestination destination,
                                    @IdRes int destId) {
        NavDestination currentDestination = destination;
        while (currentDestination.getId() != destId && currentDestination.getParent() != null) {
            currentDestination = currentDestination.getParent();
        }
        return currentDestination.getId() == destId;
    }

}

第二,在“活动”中将其称为 CustomNavigationUI 而不是 NavigationUI

/* NavigationUI.setupWithNavController(binding.navigationView, navController); */
CustomNavigationUI.setupWithNavController(binding.navigationView, navController, item -> {
            switch (item.getItemId()){
                case R.id.sign_out:
                    //todo sign out
                    break;
            }
            return true;
        });

仅此而已!

答案 2 :(得分:0)

我在 Kotlin 中编写了扩展函数来解决这个问题:

fun NavigationView.setSafeNavigationItemSelectedListener(vararg listener: Pair<Int, () -> Unit>) =
listener.forEach { (itemId, act) ->
    menu.findItem(itemId)?.setOnMenuItemClickListener { act(); true }
}

您可以使用如下:

    val navigationView = findViewById<NavigationView>(R.id.nav_view)
    navigationView.setSafeNavigationItemSelectedListener(
        R.id.shareIntent to {
            shareIntent()
            drawerLayout.closeDrawer(GravityCompat.START)
        },
        R.id.mySchedulesIntent to {
            val i = Intent(this, MySchedulesActivity::class.java)
            startActivity(i)
            drawerLayout.closeDrawer(GravityCompat.START)
        },
        R.id.importantDetailsIntent to {
            displayImportantDetailsDialog()
            drawerLayout.closeDrawer(GravityCompat.START)
        },
        R.id.infoIntent to {
            val i = Intent(this, InfoActivity::class.java)
            startActivity(i)
            drawerLayout.closeDrawer(GravityCompat.START)
        },
        R.id.settingsIntent to {
            val i = Intent(this, SettingsActivity::class.java)
            startActivity(i)
            drawerLayout.closeDrawer(GravityCompat.START)
        },
    )