gpt4 book ai didi

android - 当在 Fragment 而不是 Activity 中使用 recyclerview 时,在滚动时隐藏工具栏

转载 作者:行者123 更新时间:2023-11-29 14:47:57 24 4
gpt4 key购买 nike

我正在尝试调整隐藏/显示工具栏(或任何视觉元素)的策略,该策略来自解释得很好且很棒的文章: http://mzgreen.github.io/2015/02/15/How-to-hideshow-Toolbar-when-list-is-scroling%28part1%29/

但在我的例子中,我使用 fragment 来保存回收 View 而不是 Activity 。我的问题是没有应用填充,所以第一个元素在工具栏下面,我还有另一个奇怪的行为,因为工具栏也在状态栏下面。我不知道这里发生了什么。以下是我的“动人棋子”:

BasicActivity.java:基于上一篇文章中给出的那个,但是移除了 recycleview 部分,因为它将在 Fragment 部分。它还公开显示和隐藏方法以允许 fragment 访问它:

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:公共(public)类 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"/>

回收器的适配器和 View 持有者与文章相同,它们不影响行为。

代码有什么问题?

更新:下面的一位 Michał Z. 指出。缺少的是 Recyclerview View 上的 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

最佳答案

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

关于android - 当在 Fragment 而不是 Activity 中使用 recyclerview 时,在滚动时隐藏工具栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28814303/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com