第一步: 导入相应的design包

1 dependencies {
2    ......
3     implementation 'com.android.support:design:27.1.0'
4 }

第二步:布局文件

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     xmlns:app="http://schemas.android.com/apk/res-auto"
 6     android:background="#EFF3F7"
 7     tools:context=".MainActivity"
 8     android:orientation="vertical">
 9
10     <android.support.design.widget.CoordinatorLayout
11         android:layout_width="match_parent"
12         android:layout_height="wrap_content">
13
14         <android.support.design.widget.AppBarLayout
15             android:layout_width="match_parent"
16             android:layout_height="200dp"
17             app:layout_behavior="com.sun.testpopu.FixAppBarLayout"
18             app:elevation="0dp">
19
20             <View
21                 android:layout_width="match_parent"
22                 android:layout_height="200dp"
23                 android:background="#000000"
24                 app:layout_scrollFlags="scroll|enterAlwaysCollapsed"/>
25         </android.support.design.widget.AppBarLayout>
26         <android.support.v7.widget.RecyclerView
27             android:layout_width="match_parent"
28             android:layout_height="1000dp"
29             app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
30     </android.support.design.widget.CoordinatorLayout>
31 </LinearLayout>

第二步:初始化 Recyclerview

 1 /**
 2  * @author Sigal
 3  * on 2018/10/28
 4  * note:
 5  */
 6 public class MainActivity extends AppCompatActivity {
 7     private RecyclerView recyclerView;
 8     private ArrayList<String> mDataList;
 9
10     @Override
11     protected void onCreate(Bundle savedInstanceState) {
12         super.onCreate(savedInstanceState);
13         setContentView(R.layout.activity_main);
14         initData();
15         initRecycView();
16     }
17
18     private void initRecycView() {
19         recyclerView = findViewById(R.id.recyclerview);
20         recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this,
21                 LinearLayoutManager.VERTICAL, false));
22         recyclerView.setAdapter(new TextviewAdapter());
23
24     }
25
26     private void initData() {
27         mDataList = new ArrayList<>();
28         for (int i = 0; i < 26; i++) {
29             mDataList.add("测试数据" + i);
30         }
31     }
32
33     public class TextviewAdapter extends RecyclerView.Adapter<TextviewAdapter.ViewHolder>{
34
35         @NonNull
36         @Override
37         public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
38             View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycleview_item, parent, false);
39             return new ViewHolder(view);
40         }
41
42         @Override
43         public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
44             holder.mText.setText(mDataList.get(position));
45         }
46
47         @Override
48         public int getItemCount() {
49             return mDataList.size();
50         }
51
52         class ViewHolder extends RecyclerView.ViewHolder{
53             private final TextView mText;
54
55             public ViewHolder(View itemView) {
56                 super(itemView);
57                 mText = itemView.findViewById(R.id.textview);
58             }
59         }
60     }
61 }

第三步:解决滑动抖动问题

 1 /**
 2  * @author Sigal
 3  * on 2018/11/26
 4  * note: 解决AppBarLayout滑动时抖动问题
 5  */
 6 public class FixAppBarLayout extends AppBarLayout.Behavior {
 7     private static final String TAG = "AppBarLayoutBehavior";
 8
 9     public FixAppBarLayout() {
10         super();
11     }
12
13     public FixAppBarLayout(Context context, AttributeSet attrs) {
14         super(context, attrs);
15     }
16
17     @Override
18     public boolean onInterceptTouchEvent(CoordinatorLayout parent, AppBarLayout child, MotionEvent ev) {
19         if (ev.getAction() == ACTION_DOWN) {
20             Object scroller = getSuperSuperField(this, "mScroller");
21             if (scroller != null && scroller instanceof OverScroller) {
22                 OverScroller overScroller = (OverScroller) scroller;
23                 overScroller.abortAnimation();
24             }
25         }
26
27         return super.onInterceptTouchEvent(parent, child, ev);
28     }
29
30     private Object getSuperSuperField(Object paramClass, String paramString) {
31         Field field = null;
32         Object object = null;
33         try {
34             field = paramClass.getClass().getSuperclass().getSuperclass().getDeclaredField(paramString);
35             field.setAccessible(true);
36             object = field.get(paramClass);
37         } catch (Exception e) {
38             e.printStackTrace();
39         }
40         return object;
41     }
42 }

注:在布局文件中引用:app:layout_behavior="com.sun.testpopu.FixAppBarLayout"

04-04 10:22