billy gates
billy gates

Reputation: 185

RecyclerView change location on screen

I have problem with RecyclerView. I want change widget location at screen when scroll recyclerView by

onScrolled(RecyclerView recyclerView, int dx, int dy) 

method.

In this method I change another widget location by change LayoutParams. But I receive shiver when slowly scrolling.

RecyclerView has match_parent height, when I change location of another view who free space to stretch RecyclerView.

How I can solve shiver of RecyclerView when stretch?

layout

Upvotes: 0

Views: 291

Answers (1)

nshmura
nshmura

Reputation: 6010

Dynamically changing the height of RecyclerView seems anti-pattern.

I recommend you to use paddingTop and clipToPadding="false".

Your layout xml may be like this:

<FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false"/>

    <FrameLayout
            android:id="@+id/header_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        ... here is same layout, samll text and "STARTEN" button.

    </FrameLayout>

</FrameLayout>

Your java program may be like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ...

    final View headerContainer = findViewById(R.id.header_container);
    final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

    ... initialization of recyclerView ...

    recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            //change height of headerContainer
        }
    });

    // set paddingTop of RecyclerView
    headerContainer.getViewTreeObserver().addOnGlobalLayoutListener(
            new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        headerContainer.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    } else {
                        headerContainer.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    }

                    recyclerView.setPadding(
                            recyclerView.getPaddingLeft(),
                            headerContainer.getHeight(),
                            recyclerView.getPaddingRight(),
                            recyclerView.getPaddingBottom()
                    );
                }
            });
}

Upvotes: 1

Related Questions