繁体   English   中英

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

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

我正在尝试根据解释得很好的出色文章来隐藏/显示工具栏(或任何视觉元素)的策略: http : //mzgreen.github.io/2015/02/15/How-to-hideshow-清单出现问题时的工具栏%28part1%29 /

但就我而言,我使用的是Fragment而不是活动来保存recycleview。 我的问题是未应用填充,因此第一个元素在工具栏下,并且我还有另一个奇怪的行为,因为工具栏也在状态栏下。 我不知道这里发生了什么。 以下是我的“动人作品”:

BasicActivity.java:基于上一篇文章中给出的内容,但是将Fragment部分中的recycleview部分移开了。 它还公开了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));   
    }

}

我的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>

布局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"/>

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

片段的布局只是一个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"/>

回收站的适配器和观察器与文章相同,并且不影响行为。

代码有什么问题?

更新:下面指出了一个MichałZ.。 缺少的是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"/>

为了解决状态栏重叠问题,需要在活动布局上添加“ fitsystemwindows” =“ true”元素。 因此,必须如下所示:

<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仅当主题将状态栏设置为半透明时,才需要fitSystemWindows

您的fragment_recycler.xml文件缺少paddingTopclipToPadding属性。 它看起来应该像这样:

<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"/>

并且还要从您的toolbar_actionbar.xml删除clipToPadding

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM