简体   繁体   English

当RecyclerView到达底部时隐藏视图

[英]Hide View when RecyclerView reaches at bottom

In my android application I have a recyclerview. 在我的Android应用程序中,我有一个recyclerview。 it has 10 items. 它有10个项目。 when the user reaches at the 9th item by scrolling I have to hide one view which is lying top of the recyclerview. 当用户通过滚动到达第9个项目时,我必须隐藏一个位于回收者视图顶部的视图。 how can I do that ? 我怎样才能做到这一点 ?

xml 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));
    }

this is working fine. 这工作正常。 but the space of the hidden layout is visible.what i want is recyclerview should adjust its view after the top panel is hidden. 但是隐藏布局的空间是可见的。我想要的是recyclerview应该在隐藏顶部面板后调整其视图。 how will I do this ? 我该怎么做?

please comment if the question is not clear to you. 如果问题不明确,请发表评论。

what the above approach will do is just animate the top panel and keep the recyclerview at its position, to achieve what you are hoping you would have to animate the recycler view also. 上述方法将做的只是为顶部面板设置动画并将recyclerview保持在其位置,以实现您希望您还必须为回收器视图设置动画。 Or try setting the visibility property of top panel to GONE using mTopPanel.setVisibility(View.GONE) after animation end 或者在动画结束后使用mTopPanel.setVisibility(View.GONE)尝试将顶部面板的visibility属性设置为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();
}

One more thought is it could be done by directly animating the parent linear layout. 还有一个想法是可以通过直接设置父线性布局的动画来完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM