【发布时间】:2015-03-02 16:08:20
【问题描述】:
我正在尝试从解释清楚且很棒的文章中调整隐藏/显示工具栏(或任何视觉元素)的策略: http://mzgreen.github.io/2015/02/15/How-to-hideshow-Toolbar-when-list-is-scroling%28part1%29/
但在我的情况下,我使用片段来保存回收视图而不是活动。我的问题是没有应用填充,所以第一个元素在工具栏下,我还有另一个奇怪的行为,因为工具栏也在状态栏下。我不知道这里发生了什么。 以下是我的“动人”:
BasicActivity.java:基于上一篇文章中给出的内容,但将 recycleview 部分移到 Fragment 部分。它还公开了 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>
布局工具栏_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;
}
} 而fragment的布局只是一个recyclerview fragment_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"/>
recycler的adapter和viewholder与文章相同,不影响行为。
代码有什么问题?
更新: 下面的 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"/>
并且要解决状态栏重叠问题,需要在activity布局上添加一个“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
【问题讨论】:
标签: android scroll toolbar android-recyclerview