【发布时间】: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