簡體   English   中英

僅針對導航視圖中的一個片段折疊工具欄

[英]Collapsing Toolbar only for one Fragment in Navigation View

問題

我有一個帶有不同片段的導航抽屜。 每個Fragment都應該使用一個默認工具欄,除了一個需要折疊Toolbar Fragment

我的問題

如何在片段的工具欄之間切換?

看來您想要實現這樣的目標。

沒有什么花哨

我做了一個帶有通用工具欄的活動。 當切換到折疊的工具欄片段時,我使工具欄透明,片段的工具欄接管。 切換到其他片段時,工具欄的顏色保持不變。

這允許您在 xml 中管理完整的折疊工具欄的布局結構,而邏輯保留在 Fragment 中。

希望這會有所幫助。 請參閱鏈接的 gif。

gif 的要點

我發現可以輕松折疊、鎖定(保持折疊模式)並解鎖折疊工具欄的最佳解決方案。

private void collapseAppBar() {
    // Collapse the AppBarLayout with animation
    mAppBarLayout.setExpanded(false, true);
}

private void lockAppBar() {
    /* Disable the nestedScrolling to disable expanding the
     appBar with dragging the nestedScrollView below it */
    ViewCompat.setNestedScrollingEnabled(nestedScrollView, false);

    /* But still appBar is expandable with dragging the appBar itself
    and below code disables that too
     */
    CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams();
    AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
    behavior.setDragCallback(new AppBarLayout.Behavior.DragCallback() {
        @Override
        public boolean canDrag(AppBarLayout appBarLayout) {
            return false;
        }
    });
}

private void unLockAppBar() {
    ViewCompat.setNestedScrollingEnabled(nestedScrollView, true);

    CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams();
    AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
    if (behavior != null) {
        behavior.setDragCallback(new AppBarLayout.Behavior.DragCallback() {
            @Override
            public boolean canDrag(AppBarLayout appBarLayout) {
                return true;
            }
        });
    }
}

我以這種方式使用這些功能:

    Fragment fragment = null;
    Class fragmentClass;
    switch (menuItem.getItemId()) {
        case R.id.fragment1:
            unLockAppBar();
            fragmentClass = first_Fragment.class;
            break;
        case R.id.fragment2:
            collapseAppBar();
            lockAppBar();
            fragmentClass = second_Fragment.class;
            break;
        case R.id.fragment3:
            collapseAppBar();
            lockAppBar();
            fragmentClass = third_Fragment.class;
            break;

您可以輕松地獲得Toolbar從你的Fragment ,然后修改或更改的某些屬性Toolbar里面Fragment

要從您的Activity獲取Toolbar ,您可以考慮使用它。

Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);

現在您需要在onResume函數中的Toolbar上進行更改,然后每次從onStop函數中的Fragment返回時撤消更改。 否則,當從導航抽屜切換到其他Fragment時,在Fragment所做的更改也將繼續到其他Fragment

但是在您的情況下,我建議每個Fragment都應該有自己的Toolbar這樣它就不會相互沖突,並且可以根據需要進行修改。 是的,從您的Activity刪除Toolbar

因此,像這樣在Fragment的布局中添加Toolbar

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimaryDark"/>

然后在Fragment找到它

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment, container, false);
    Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);

    // Modify your Toolbar here. 
    // ... 
    // For example. 
    // toolbar.setBackground(R.color.red);

    // Create home button
    AppCompatActivity activity = (AppCompatActivity) getActivity();
    activity.setSupportActionBar(toolbar);
    activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

並覆蓋onOptionsItemSelected函數。

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()){
        case android.R.id.home:
            getActivity().onBackPressed();
    }
    return super.onOptionsItemSelected(item);
}

我在我的應用程序中使用Jetpack 的導航組件和單個活動和不同的片段。

一些 Fragments 可以從底部導航訪問(並且有來自 Activity 的Toolbar )。 其他一些是“特殊”片段,並有自己的可折疊工具欄。

為了實現這一點,我將工具欄從“特殊”片段中的活動中隱藏起來,並在活動中使用以下代碼:

// Handle toolbar changes in different Fragments
val navController = findNavController(R.id.nav_host_fragment)
navController.addOnDestinationChangedListener { _, destination, _ ->
    when (destination.id) {
        R.id.my_special_fragment_with_collapsible_toolbar -> {
            binding.toolbarMain.visibility = View.GONE
        }
        else -> {
            binding.toolbarMain.visibility = View.VISIBLE
        }
    }
}

推薦的做法是在片段中使用工具欄,而不是在活動中使用通用工具欄。 這樣您就可以在片段中控制工具欄的外觀和行為。 參考https://developer.android.com/guide/navigation/navigation-ui#support_app_bar_variations

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM