当RecyclerView到达底部时隐藏视图

时间:2015-08-31 09:05:22

标签: android android-animation android-recyclerview

在我的Android应用程序中,我有一个recyclerview。它有10个项目。当用户通过滚动到达第9个项目时,我必须隐藏一个位于回收者视图顶部的视图。我怎么能这样做?

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>

活动

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应该在隐藏顶部面板后调整其视图。我该怎么做?

如果问题不明确,请发表评论。

1 个答案:

答案 0 :(得分:3)

上述方法将做的只是为顶部面板设置动画并将recyclerview保持在其位置,以实现您希望您还必须为回收器视图设置动画。或者尝试在动画结束后使用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();
}

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