Fade / Fade in a scroll-based TextView

I have a horizontal RecyclerView in a LinearLayout with a TextView above it, for example:

<TextView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginTop="7dp"
 android:layout_marginLeft="10dp"
 android:layout_marginBottom="7dp"
 android:textColor="#FFa7a7a7"
 android:textSize="15sp"
 android:text="Hello, Android" />

<android.support.v7.widget.RecyclerView
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/recyclerview"
 android:layout_width="match_parent"
 android:layout_height="185dp" />

I want the TextView to disappear when there is a left scroll, and the first element in the RecyclerView goes out of view. And you want it to disappear during the appearance of the first element (through the right scroll). I know that I will have to use addOnScrollChangedListener()to determine when the first recyclerView goes out of view, something that I could not determine is a way to fade out (or fade out) the TextView using scroll behavior.

Here is my Java RecyclerView snippet:

mRecyclerView = (RecyclerView)rootView.findViewById(R.id.recyclerview);
mRecyclerView.setLayoutManager(getLayoutManager());
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setAdapter(mAdapter);
+4
source share
1

: @pskink , . setAlpha() - .

RecyclerView OnScrollListener

.

, , , . .

OnScrollListener, , , , , , :

float alpha = 1.0f;
float newAlpha = 1.0f;
int overallXScroll = 0;

mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
         super.onScrolled(recyclerView, dx, dy);

         //use this value to determine left or right scroll
         overallXScroll = overallXScroll + dx;

         //if scroll left
         float newAlpha = alpha - 0.1f;
         if (newAlpha >= 0){
           textView.setAlpha(newAlpha);
           alpha = newAlpha;
         }

         //if scroll right
         float newAlpha = alpha + 0.1f;
         if (newAlpha <= 1){
           textView.setAlpha(newAlpha);
           alpha = newAlpha;
         }
     }
});
+5

All Articles