droidev
droidev

Reputation: 7390

Hide View when RecyclerView reaches at bottom

In my android application I have a recyclerview. it has 10 items. when the user reaches at the 9th item by scrolling I have to hide one view which is lying top of the recyclerview. how can I do that ?

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. how will I do this ?

please comment if the question is not clear to you.

Upvotes: 1

Views: 1553

Answers (1)

harshitpthk
harshitpthk

Reputation: 4136

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. Or try setting the visibility property of top panel to GONE using mTopPanel.setVisibility(View.GONE) after animation end

 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.

Upvotes: 3

Related Questions