Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

preface

The above effect is very common in our daily use App. In fact, this effect may have a vague idea at a glance. Generally, it is to calculate a sliding distance and then calculate the alpha value according to the distance.

Code

xml

<? The XML version = "1.0" encoding = "utf-8"? > <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".big.BigActivity"> <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" Android :paddingTop="20dp" Android :paddingBottom="20dp" Android :text=" black" Android :textColor="@color/black" android:textSize="18dp" app:layout_constraintTop_toTopOf="parent" /> <androidx.recyclerview.widget.RecyclerView android:id="@+id/rv" android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginTop="10dp" android:background="@color/teal_200" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toBottomOf="@id/tv" /> </androidx.constraintlayout.widget.ConstraintLayout>Copy the code

Activity

public class BigActivity extends AppCompatActivity { private TextView mTv; private RecyclerView mRv; private int mCurrentfirstVisibleItem; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_big); initView(); } private void initView() { mTv = findViewById(R.id.tv); mRv = findViewById(R.id.rv); List<String> list = new ArrayList<>(); for (int i = 0; i < 40; i++) { list.add(""); } LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); mRv.setLayoutManager(linearLayoutManager); mRv.setAdapter(new TestAdapter(this, list)); }}Copy the code

Adapter I will not paste, otherwise this article will be full of water

First of all, we need to know that we slide a distance, in accordance with common sense, recyclerView should have sliding monitoring, we first in accordance with the name of the Yang to search to try it

The second one is outdated, the third one is deleted, the fourth one is cleared, and the fifth one has a change, which is estimated to judge the sliding state

Has two callback onScrolled onScrollStateChanged, can selectively override, not necessarily all, I’d choose onScrolled here

@Override
public void onScrolled(@NonNull @NotNull RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);
    Log.e("TAG", "onScrolled: "+dx+"  "+dy);
}
Copy the code

We can see that there are two parameters that seem to be important, one is dx and one is dy, so let’s just print the log

So every slide is going to be a callback, so up is a relative integer, down is a relative negative, yeah, ok, let’s define a parameter here, plus equals.

mRv.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(@NonNull @NotNull RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); } @Override public void onScrolled(@NonNull @NotNull RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); test += dy; Log.e("TAG", "onScrolled: "+ test); }});Copy the code

Print logs

So far we have successfully obtained the slip distance data of RecyclerView. Next I will show how to use slip distance to change the title bar and status bar in the next post