Android 当RecyclerView到达底部时隐藏视图
在我的android应用程序中,我有一个recyclerview。它有10个项目。当用户通过滚动到达第9项时,我必须隐藏位于recyclerview顶部的一个视图。我该怎么做 xmlAndroid 当RecyclerView到达底部时隐藏视图,android,android-animation,android-recyclerview,Android,Android Animation,Android Recyclerview,在我的android应用程序中,我有一个recyclerview。它有10个项目。当用户通过滚动到达第9项时,我必须隐藏位于recyclerview顶部的一个视图。我该怎么做 xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match
<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>
活动
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));
}
private void initView(){
mTopPanel=(线性布局)findViewById(R.id.top\U视图);
RecyclerView RecyclerView=(RecyclerView)findViewById(R.id.recycler\U列表);
recyclerView.setLayoutManager(新的LinearLayoutManager(本));
最终ArrayList listData=getListData();
ListViewAdapter=新的ListViewAdapter(listData);
recyclerView.setAdapter(适配器);
mLinearLayoutManager=(LinearLayoutManager)recyclerView.getLayoutManager();
recyclerView.addOnScrollListener(新的recyclerView.OnScrollListener(){
@凌驾
已填空的公共空间(RecyclerView RecyclerView、int dx、int dy){
super.onScrolled(recyclerView、dx、dy);
if(mLinearLayoutManager.findLastCompletelyVisibleItemPosition()==listData.size()-1){
hideTopPanel();
}
否则{
showTopPanel();
}
}
});
}
私有void showTopPanel(){
mTopPanel.animate().translationY(0).setInterpolator(新减速插补器(2));
mTopPanel.animate().translationY(0).setInterpolator(新的减速插补器(2)).start();
}
私有无效隐藏文件夹(){
mTopPanel.animate().translationY(-mTopPanel.getHeight()).setInterpolator(新的加速器插值器(2));
}
这很好用。但是隐藏布局的空间是可见的。我想要的是recyclerview应该在顶部面板隐藏后调整其视图。我该怎么做
如果您不清楚这个问题,请发表评论。上述方法将只需设置顶部面板的动画,并将recyclerview保持在其位置,即可实现您希望的效果,同时也可以设置recycler视图的动画。或者尝试在动画结束后使用
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();
}
还有一个想法是,可以通过直接为父线性布局设置动画来实现。实际上,我需要在隐藏和显示视图时设置动画。尝试上述更新是否有助于解决问题。我使用过你的代码,但没有测试过。尝试过这个,回收器视图将移到顶部,但其底部也将移到顶部是的,它将意识到回收器视图中视图的高度将是恒定的。理想的场景是什么?你能再描述一下吗。