微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

向下滚动时 Materialio Appbar 不会隐藏

如何解决向下滚动时 Materialio Appbar 不会隐藏

我遵循了 material documentation for top app bars 并在我的应用中实现了它的一部分,以便在向下滚动列表时能够隐藏它。

我的布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:id="@+id/ddd"
 android:layout_height="match_parent"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 tools:context=".pkgTestforend.DriverListFragment">

    <com.example.dochjavatestimplementation.pkgTestforend.CustomLinearLayout
        android:layout_width="match_parent"
        android:id="@+id/cusll"
        android:layout_height="match_parent"
        app:elevation="0dp">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                    <ListView
                        android:id="@+id/listAllDrivers"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"/>
            </RelativeLayout>

    </com.example.dochjavatestimplementation.pkgTestforend.CustomLinearLayout>

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:liftOnScroll="true" >

            <com.google.android.material.appbar.MaterialToolbar
                android:id="@+id/topAppBar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:title="PageTitle"
                app:menu="@menu/top_app_bar"
                app:layout_scrollFlags="scroll|enteralways|snap"
                app:navigationIcon="@drawable/baseline_menu_24"
                style="@style/Widget.MaterialComponents.Toolbar.Primary"
                />

    </com.google.android.material.appbar.AppBarLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

以及相应的片段:

public class DriverListFragment extends Fragment implements View.OnClickListener {

    public DriverListFragment() {
    }

    public static DriverListFragment newInstance(String param1,String param2) {
        return  new DriverListFragment();
    }

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

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.fragment_driver_list,container,false);
        setUpToolbar(view);
        return view;
    }

    private void setUpToolbar(View view) {
        Toolbar toolbar = view.findViewById(R.id.topAppBar);
        AppCompatActivity activity = (AppCompatActivity) getActivity();
        if (activity != null) {
            activity.setSupportActionBar(toolbar);
        }
    }

    @Override
    public void onCreateOptionsMenu(Menu menu,MenuInflater menuInflater) {
        menuInflater.inflate(R.menu.top_app_bar,menu);
        super.onCreateOptionsMenu(menu,menuInflater);
    }

    ListView listViewDriver;
    Driverlistadapter adapter;
    RoomWithRxJavaviewmodel viewmodel;
    
    public void onViewCreated(View view,@Nullable Bundle savedInstanceState) {
        listViewDriver = view.findViewById(R.id.listAllDrivers);
        viewmodel = new RoomWithRxJavaviewmodel(getActivity().getApplication());
    
           disposable d = viewmodel.getDrivers()
            .subscribe(allusers ->
                {
                    adapter = new Driverlistadapter(getContext(),(ArrayList<Driver>) allusers);
                    listViewDriver.setAdapter(adapter);

                },e -> {
                    //show err mes
                }

            );
    }

}

设置非常简单。 我只有一个自定义的线性布局,我打算稍后对其进行修改。是的,列表视图的相对布局是故意的,所以我可以轻松地修改我未来的按钮位置。

结果如下:

example

所以问题是,尽管顶部的应用栏是可见的,但当我向下滚动列表时它不会隐藏(见上图),尽管我使用的是 app:liftOnScroll="true"app:layout_scrollFlags="scroll|enteralways|snap"

我到底错过了什么?是因为我使用了我的自定义线性布局吗? 将 appbar 放在自定义线性布局之前并没有意外地改变输出

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。