RecyclerView与AppBarLayout联动实现悬停效果
话不多说,我们先来看一下我们实现的效果
这里,当 RecyclerView上拉时会先滑动AppBarLayout从而悬停滑动固定区域,然后再继续RecyclerView视图自己的滑动事件,下拉时会先实现RecyclerView自己的滑动事件,当RecyclerView第一个item全部显示后滑动事件会传递给AppBarLayout,从而展示滑动隐藏区域。
我们来看一下视图布局代码以及类代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@color/gray"
android:gravity="center"
android:text="滑动隐藏区域"
android:textColor="@android:color/white"
android:textSize="30sp"
app:layout_scrollFlags="scroll|snap" />
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/green"
android:gravity="center"
android:text="滑动固定区域"
android:textColor="@android:color/white"
android:textSize="20sp" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</androidx.recyclerview.widget.RecyclerView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Activity代码
package com.sudio.demotest;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Build;
import android.os.Bundle;
import com.sudio.demotest.adapter.CaseAdapter;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class CaseFourActivity extends AppCompatActivity {
RecyclerView rv_list;
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_case_four);
rv_list=findViewById(R.id.rv_list);
List<String> listData= Arrays.stream(getResources().getStringArray(R.array.testList)).collect(Collectors.toList());
CaseAdapter caseAdapter=new CaseAdapter();
caseAdapter.setList(listData);
rv_list.setLayoutManager(new LinearLayoutManager(this));
rv_list.setAdapter(caseAdapter);
rv_list.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
}
}
适配器代码
package com.sudio.demotest.adapter;
import android.text.TextUtils;
import android.widget.TextView;
import androidx.annotation.NonNull;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.sudio.demotest.R;
public class CaseAdapter extends BaseQuickAdapter<String, BaseViewHolder> {
public CaseAdapter()
{
super(R.layout.adapter_case );
}
@Override
protected void convert(@NonNull BaseViewHolder baseViewHolder, String content) {
TextView tv_text=baseViewHolder.getView(R.id.tv_title);
tv_text.setText(content);
tv_text.setEllipsize(TextUtils.TruncateAt.MARQUEE);
tv_text.setSingleLine(true);
tv_text.setSelected(true);
}
}
最后附上项目Demo
[点击获取项目源码