【问题标题】:problem with hiding ActionBar for specific Fragment为特定片段隐藏 ActionBar 的问题
【发布时间】:2019-03-05 20:18:39
【问题描述】:

在过去的 2 周里,我正在研究 TabLayout 、 viewPager 、 RecyclerView 和片段的组合,所以这是问题所在。

我想隐藏特定 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>

【问题讨论】:

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


    【解决方案1】:

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

    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);
                }
            }
    
        });
    

    所以实际上我们不必在滚动时隐藏/显示操作栏,我们可以使用视图并实现独立的元素并隐藏/显示它,就像我展示的那样。 如果你知道任何更简单的方法,我想知道。

    【讨论】:

      猜你喜欢
      • 2016-04-06
      • 2019-10-10
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 2016-06-25
      • 2012-05-18
      • 2015-02-20
      • 2017-03-02
      相关资源
      最近更新 更多