简体   繁体   English

在片段内而不是活动内使用recyclerview时,在滚动条上隐藏工具栏

[英]Hide toolbar on scroll when the using a recyclerview inside a Fragment instead of an Activity

I'm trying to adapt the strategy for hiding / showing a toolbar (or any visual element) from the well explained and great article: http://mzgreen.github.io/2015/02/15/How-to-hideshow-Toolbar-when-list-is-scroling%28part1%29/ 我正在尝试根据解释得很好的出色文章来隐藏/显示工具栏(或任何视觉元素)的策略: http : //mzgreen.github.io/2015/02/15/How-to-hideshow-清单出现问题时的工具栏%28part1%29 /

But in my case I'm using a Fragment to hold the recycleview instead of the activity. 但就我而言,我使用的是Fragment而不是活动来保存recycleview。 My problem is that the padding is not being applied so the first element is under the toolbar, and I have also another strange behavior, as the toolbar is also under the statusbar. 我的问题是未应用填充,因此第一个元素在工具栏下,并且我还有另一个奇怪的行为,因为工具栏也在状态栏下。 I don't know what is happening here. 我不知道这里发生了什么。 The following are my "moving pieces": 以下是我的“动人作品”:

BasicActivity.java: based on the one given on the previous post, but moving away the recycleview part as is going to be on the Fragment piece. BasicActivity.java:基于上一篇文章中给出的内容,但是将Fragment部分中的recycleview部分移开了。 Also it exposes the show and hide methods to allow the fragment to access it: 它还公开了show和hide方法,以允许片段访问它:

public class BasicActivity extends ActionBarActivity {

    private Toolbar mToolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_basic);
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.container,new RecycleFragment())
                .commit();
        overridePendingTransition(0, 0);
        initToolbar();
    }

    private void initToolbar() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        setTitle(getString(R.string.app_name));
        mToolbar.setTitleTextColor(getResources().getColor(android.R.color.white));
    }

    public void hideViews() {
        mToolbar.animate().translationY(-mToolbar.getHeight()).setInterpolator(new AccelerateInterpolator(2));

    }

    public void showViews() {
        mToolbar.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2));   
    }

}

My activiy_basic.xml is the following: 我的activiy_basic.xml是以下内容:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    />
    <include layout="@layout/toolbar_actionbar" />
</FrameLayout>

The layout toolbar_actionbar.xml 布局toolbar_actionbar.xml

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:clipToPadding="false"/>

The Fragment RecycleFragment.java: public class RecycleFragment extends Fragment { Fragment RecycleFragment.java:公共类RecycleFragment扩展了Fragment {

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


@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    initRecyclerView(view);

}

private void initRecyclerView(View view) {
    RecyclerView recyclerView = (RecyclerView)view.findViewById(R.id.recyclerView);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    RecyclerAdapter recyclerAdapter = new RecyclerAdapter(createItemList());
    recyclerView.setAdapter(recyclerAdapter);

    recyclerView.setOnScrollListener(new HidingScrollListener() {
        @Override
        public void onHide() {
            ((BasicActivity)getActivity()).hideViews();
        }

        @Override
        public void onShow() {
            ((BasicActivity)getActivity()).showViews();
        }
    });
}

private List<String> createItemList() {
    List<String> itemList = new ArrayList<>();
    for(int i=0;i<20;i++) {
        itemList.add("Item "+i);
    }
    return itemList;
}

} And the layout for the fragment is just a recyclerview fragment_recycler.xml: 片段的布局只是一个recyclerviewfragment_recycler.xml:

<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

The adapter and the viewholder for the recycler are the same as the article, and they doesn't affect the behavior. 回收站的适配器和观察器与文章相同,并且不影响行为。

What is wrong with the code? 代码有什么问题?

UPDATE: A Michał Z. below pointed out. 更新:下面指出了一个MichałZ.。 What was missing is the paddingTop and clipptoPadding on the Recyclerview view So the final xml should be: 缺少的是Recyclerview视图上的paddingTop和clipptoPadding,因此最终的xml应该是:

<android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="?attr/actionBarSize"
        android:clipToPadding="false"/>

And to solve the statusbar overlapping problem, it is needed to add a "fitsystemwindows" = "true" element on the activity layout. 为了解决状态栏重叠问题,需要在活动布局上添加“ fitsystemwindows” =“ true”元素。 So it must be as the following: 因此,必须如下所示:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">        
    <FrameLayout android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    />
    <include layout="@layout/toolbar_actionbar" />
</FrameLayout>

UPDATE2 The fitSystemWindows is only needed if the theme is setting the statusbar as translucent UPDATE2仅当主题将状态栏设置为半透明时,才需要fitSystemWindows

Your fragment_recycler.xml file is missing paddingTop and clipToPadding attributes. 您的fragment_recycler.xml文件缺少paddingTopclipToPadding属性。 It should look like this: 它看起来应该像这样:

<android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="?attr/actionBarSize"
        android:clipToPadding="false"/>

And also remove clipToPadding from your toolbar_actionbar.xml . 并且还要从您的toolbar_actionbar.xml删除clipToPadding

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过在Fragment中滚动recyclerview来隐藏活动中的工具栏? - Hide toolbar in activity by scrolling a recyclerview inside the Fragment? 在NestedScrollView中使用recyclerView时如何隐藏滚动条上的工具栏 - how to hide toolbar on scroll when using recyclerView inside NestedScrollView 如果我向下滚动片段中的recyclerView,如何自动隐藏工具栏? - How to hide toolbar automatically if I scroll down the recyclerView inside a fragment? 隐藏工具栏滚动与片段内的recyclerview - Hiding Toolbar on scroll with recyclerview inside fragment 无法使用片段滚动带有 recyclerview 的折叠工具栏 - Unable to scroll collapsing toolbar with recyclerview using fragment 滚动“活动工具栏”,在屏幕外滚动片段中的Recyclerview - Scroll Activity Toolbar off screen scrolling Recyclerview in fragment 使用CoordinatorLayout从片段中隐藏活动中的工具栏 - Hide Toolbar in activity from fragment using CoordinatorLayout 在片段中滚动RecyclerView时隐藏应用程序栏 - Hide appbar when scroll RecyclerView in a fragment 在 Fragment 中使用 RecyclerView 而不是 Activity 会导致 E/RecyclerView: No adapter attached; 跳过布局错误 - Using a RecyclerView inside a Fragment instead of an Activity results in E/RecyclerView: No adapter attached; skipping layout error 使用CoordinatorLayout隐藏工具栏,但在片段上使用RecyclerView - Hide Toolbar with CoordinatorLayout, but RecyclerView on a fragment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM