gpt4 book ai didi

android - 当 RecyclerView 到达底部时隐藏 View

转载 作者:行者123 更新时间:2023-11-29 17:32:25 25 4
gpt4 key购买 nike

在我的 android 应用程序中,我有一个 recyclerview。它有 10 个项目。当用户通过滚动到达第 9 个项目时,我必须隐藏一个位于 recyclerview 顶部的 View 。我该怎么做?

xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/dimen_sm"
android:paddingLeft="@dimen/dimen_sm"
android:paddingRight="@dimen/dimen_sm"
android:paddingTop="@dimen/dimen_sm"
tools:context=".MainActivity">

<LinearLayout
android:background="@color/white"
android:id="@+id/top_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="@dimen/dimen_sm"
android:paddingTop="@dimen/dimen_sm">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

<android.support.v7.widget.RecyclerView
android:layout_marginTop="@dimen/dimen_xs"
android:id="@+id/recycler_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white" />

</LinearLayout>

Activity

private void initView() {
mTopPanel = (LinearLayout) findViewById(R.id.top_view);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_list);

recyclerView.setLayoutManager(new LinearLayoutManager(this));
final ArrayList<Data> listData = getListData();
ListViewAdapter adapter = new ListViewAdapter(listData);
recyclerView.setAdapter(adapter);

mLinearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);

if(mLinearLayoutManager.findLastCompletelyVisibleItemPosition() == listData.size()-1){
hideTopPanel();
}
else{
showTopPanel();
}
}
});
}

private void showTopPanel() {
mTopPanel.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2));
mTopPanel.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2)).start();
}

private void hideTopPanel() {
mTopPanel.animate().translationY(-mTopPanel.getHeight()).setInterpolator(new AccelerateInterpolator(2));
}

这工作正常。但是隐藏布局的空间是可见的。我想要的是 recyclerview 应该在隐藏顶部面板后调整其 View 。我该怎么做?

如果您不清楚问题,请发表评论。

最佳答案

上述方法所做的只是为顶部面板设置动画并将回收站 View 保持在其位置,以实现您希望还必须为回收站 View 设置动画的效果。或者尝试在动画结束后使用 mTopPanel.setVisibility(View.GONE) 将顶部面板的可见性属性设置为 GONE

 private void showTopPanel() {
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_list);
recyclerView.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2)).start();
mTopPanel.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2)).start();
}
private void hideTopPanel() {
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_list);
recyclerView.animate().translationY(-mTopPanel.getHeight()).setInterpolator(new DecelerateInterpolator(2)).start();
mTopPanel.animate().translationY(-mTopPanel.getHeight()).setInterpolator(new AccelerateInterpolator(2)).start();
}

还有一个想法是可以通过直接动画父线性布局来完成。

关于android - 当 RecyclerView 到达底部时隐藏 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32307571/

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