隐藏特定片段的ActionBar的问题

时间:2018-09-30 14:47:08

标签: android android-fragments android-recyclerview android-actionbar

在过去的两周中,我正在研究TabLayout,viewPager,RecyclerView和fragment的组合,所以这里是问题。

我想为特定的Fragment隐藏ActionBar,并且我在Fragment类中做到了像这样:

公共类MA0FragmentWord扩展了android.support.v4.app.Fragment {

View v;

private RecyclerView recyclerView;
private List<MA0WordItems> mList;
private List<MA0WordItems> mlistFull;


public MA0FragmentWord(){
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    setHasOptionsMenu(true);

    v = inflater.inflate(R.layout.word_fragment0,container,false);

    recyclerView = v.findViewById(R.id.word_recycler0);

    MA0RecyclerAdapter recyclerAdapter = new MA0RecyclerAdapter(getContext(),mList);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    recyclerView.setAdapter(recyclerAdapter);

    recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {


         @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

            if (dy > 10) {
            // View view = recyclerView.getLayoutManager().getChildAt(0);
                ((AppCompatActivity)getActivity()).getSupportActionBar().hide();

            } else if (dy<0){

                ((AppCompatActivity)getActivity()).getSupportActionBar().show();
            }
        }

    });

    return v;
}

它似乎可以正常工作,但问题是我的xml布局似乎有错误,并且在视图底部呈现时存在某种问题。我也更改为线性布局,但未做任何更改(当操作栏将视图隐藏到顶部时,视图的底部也会随之向上并创建一些空白),(对不起,英语不好)

<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark"
android:orientation="vertical"
tools:context=".MainActivity0">

<android.support.design.widget.TabLayout
    android:id="@+id/tabLayout0"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimaryDark"
    app:layout_constraintBottom_toTopOf="@+id/viewPager0"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:tabBackground="@color/colorPrimaryDark"
    app:tabIndicatorColor="#d4d4d4"
    app:tabMode="fixed"
    app:tabSelectedTextColor="#a6ae3d"
    app:tabTextColor="#d4d4d4">

</android.support.design.widget.TabLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/viewPager0"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/tabLayout0">

</android.support.v4.view.ViewPager>

<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="@color/colorPrimaryDark"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
    android:id="@+id/word_recycler0"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</android.support.v7.widget.RecyclerView>

1 个答案:

答案 0 :(得分:0)

对于任何遇到与我相同的问题的人,我放弃了隐藏操作栏(它实际上是隐藏的,但是有一些我解释过的问题...),所以我们可以做其他事情,我们可以隐藏操作栏清单:

android:theme="@style/Theme.Design.NoActionBar">

并且诅咒我们可以隐藏/显示针对特定活动的操作栏,如下所示:

<activity android:name=".MainActivity0"
        android:label="Lets Talk"
        android:theme="@style/CustomTheme"></activity>

,我们可以轻松地从Fragment Activity访问Main Activity元素,并实现滚动更改,如下所示:

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {


         @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

            if (dy > 0) {
               TabLayout tabLayout =((AppCompatActivity)getActivity()).findViewById(R.id.tabLayout0);
               tabLayout.setVisibility(View.GONE);




            } else if (dy<0){

                TabLayout tabLayout =((AppCompatActivity)getActivity()).findViewById(R.id.tabLayout0);
                tabLayout.setVisibility(View.VISIBLE);
            }
        }

    });

因此,实际上我们不必在滚动条上隐藏/显示操作栏,我们可以使用视图并实现独立于其的元素,并按照我的说明隐藏/显示它。 如果您知道任何更简单的方法,我想知道。