导航到其他片段后,如何在滚动后显示底部应用栏?

时间:2018-10-15 18:33:40

标签: android material-design

我已经在我的应用程序中实现了Bottom App Bar。滚动时我可以隐藏底部应用栏,但是当我导航到其他片段时,它仍然被隐藏吗?如何以编程方式使Bottom App Bar可见?

根据材料设计文档,我在Bottom App Bar中定义了以下属性。

app:hideOnScroll="true"

在Fragment中,我将滚动内容放在NestedScrollView中,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".Admin.AddUpdateCustomerFragment"
    android:orientation="vertical"
    android:transitionName="transtion_name_example"
    >

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:id="@+id/linearLayout">


        </LinearLayout>
    </androidx.core.widget.NestedScrollView>
</LinearLayout>

AdminActivity.java

public class AdminActivity extends AppCompatActivity {
    public BottomAppBar bottomAppBar;
    public FloatingActionButton floatingActionButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin);
        getWindow().setEnterTransition(null);
        floatingActionButton = (FloatingActionButton) findViewById(R.id.fab);
        bottomAppBar = (BottomAppBar) findViewById(R.id.bar);
        setSupportActionBar(bottomAppBar);
        new CustomerFragment().setArguments(Bundle.EMPTY);

        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fragment_container, new CustomerFragment())
                .commit();
    }

    public boolean loadFragment(Fragment fragment, Bundle bundle) {
        if (fragment != null) {
            fragment.setArguments(bundle);
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.fragment_container, fragment)
                    .commit();

            return true;
        }
        return false;
    }
}

CustomerFragment.java

public class CustomerFragment extends Fragment {
    View rootView;

    public CustomerFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_customer_fragment_admin, container, false);

        ((AdminActivity) getActivity()).bottomAppBar.setFabAlignmentMode(BottomAppBar.FAB_ALIGNMENT_MODE_CENTER);
        ((AdminActivity) getActivity()).floatingActionButton.setImageResource(R.drawable.ic_add_black_24dp);
        ((AdminActivity) getActivity()).floatingActionButton.setColorFilter(Color.WHITE);

        ((AdminActivity) getActivity()).floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((AdminActivity) getActivity()).loadFragment(new AddUpdateCustomerFragment(), Bundle.EMPTY);
            }
        });
        return rootView;
    }

AddUpdateCustomerFragment.java

public class AddUpdateCustomerFragment extends Fragment {
    View rootView;

    public AddUpdateCustomerFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView =  inflater.inflate(R.layout.fragment_add_update_customer, container, false);

        ((AdminActivity) getActivity()).bottomAppBar.setNavigationIcon(null);
        ((AdminActivity) getActivity()).bottomAppBar.setFabAlignmentMode(FAB_ALIGNMENT_MODE_END);

        btnBack = (ImageView) rootView.findViewById(R.id.back);

        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((AdminActivity) getActivity()).loadFragment(new CustomerFragment(), Bundle.EMPTY);
            }
        });
        return rootView;
    }
}

1 个答案:

答案 0 :(得分:1)

这是解决您问题的方法。

在您的OnScrollChangeListener上附加一个NestedScrollView,以切换BottomNavBar的可见性

scroller.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
    @Override
    public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {

        if (scrollY > oldScrollY) {
            Log.i(TAG, "Scroll DOWN");
            mBottomNavBar.setVisibility(View.GONE);
        }
        if (scrollY < oldScrollY) {
            Log.i(TAG, "Scroll UP");
            mBottomNavBar.setVisibility(View.VISIBLE);
        }

        if (scrollY == 0) {
            Log.i(TAG, "TOP SCROLL");
        }

       if (scrollY == ( v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight() )) {
           Log.i(TAG, "BOTTOM SCROLL");
       }
   }
});

在片段的onCreateView()内,切换Visibility的{​​{1}},然后将其隐藏。

BottomNavigationBar

或者在启动新的@Override protected void onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //... bottomNavBar.setVisibility(View.VISIBLE) } 时执行相同操作:

Fragment

别忘了删除 btnBack.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { ((AdminActivity) getActivity()).loadFragment(new CustomerFragment(), Bundle.EMPTY); mBottomNavBar.setVisibility(View.VISIBLE); } });