我正在尝试采用策略来隐藏/显示很好解释的出色文章中的工具栏(或任何视觉元素):
http://mzgreen.github.io/2015/02/15/How-to-hideshow-Toolbar-when-list-is-scroling%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"/>


片段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>


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


并从clipToPadding中删除​​toolbar_actionbar.xml

10-07 19:40
查看更多