CoordinatorLayout + AppBarLayout 的简单使用及解决滑动抖动问题

第一步: 导入相应的design包

11 dependencies { 22 ...... 33 implementation 'com.android.support:design:27.1.0' 44 }

第二步:布局文件

1 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 2 xmlns:tools="http://schemas.android.com/tools" 3 3 android:layout_width="match_parent" 4 4 android:layout_height="match_parent" 5 5 xmlns:app="http://schemas.android.com/apk/res-auto" 6 6 android:background="#EFF3F7" 7 7 tools:context=".MainActivity" 8 8 android:orientation="vertical"> 9 9 1010 <android.support.design.widget.CoordinatorLayout 1111 android:layout_width="match_parent" 1212 android:layout_height="wrap_content"> 1313 1414 <android.support.design.widget.AppBarLayout 1515 android:layout_width="match_parent" 1616 android:layout_height="200dp" 1717 app:layout_behavior="com.sun.testpopu.FixAppBarLayout" 1818 app:elevation="0dp"> 1919 2020 <View 2121 android:layout_width="match_parent" 2222 android:layout_height="200dp" 2323 android:background="#000000" 2424 app:layout_scrollFlags="scroll|enterAlwaysCollapsed"/> 2525 </android.support.design.widget.AppBarLayout> 2626 <android.support.v7.widget.RecyclerView 2727 android:layout_width="match_parent" 2828 android:layout_height="1000dp" 2929 app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 3030 </android.support.design.widget.CoordinatorLayout> 3131 </LinearLayout>

第二步:初始化 Recyclerview

1 1 /** 2 2 * @author Sigal 3 3 * on 2018/10/28 4 4 * note: 5 5 */ 6 6 public class MainActivity extends AppCompatActivity { 7 7 private RecyclerView recyclerView; 8 8 private ArrayList<String> mDataList; 9 9 1010 @Override 1111 protected void onCreate(Bundle savedInstanceState) { 1212 super.onCreate(savedInstanceState); 1313 setContentView(R.layout.activity_main); 1414 initData(); 1515 initRecycView(); 1616 } 1717 1818 private void initRecycView() { 1919 recyclerView = findViewById(R.id.recyclerview); 2020 recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this, 2121 LinearLayoutManager.VERTICAL, false)); 2222 recyclerView.setAdapter(new TextviewAdapter()); 2323 2424 } 2525 2626 private void initData() { 2727 mDataList = new ArrayList<>(); 2828 for (int i = 0; i < 26; i++) { 2929 mDataList.add("测试数据" + i); 3030 } 3131 } 3232 3333 public class TextviewAdapter extends RecyclerView.Adapter<TextviewAdapter.ViewHolder>{ 3434 3535 @NonNull 3636 @Override 3737 public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { 3838 View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycleview_item, parent, false); 3939 return new ViewHolder(view); 4040 } 4141 4242 @Override 4343 public void onBindViewHolder(@NonNull ViewHolder holder, int position) { 4444 holder.mText.setText(mDataList.get(position)); 4545 } 4646 4747 @Override 4848 public int getItemCount() { 4949 return mDataList.size(); 5050 } 5151 5252 class ViewHolder extends RecyclerView.ViewHolder{ 5353 private final TextView mText; 5454 5555 public ViewHolder(View itemView) { 5656 super(itemView); 5757 mText = itemView.findViewById(R.id.textview); 5858 } 5959 } 6060 } 6161 }

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

1 1 /** 2 2 * @author Sigal 3 3 * on 2018/11/26 4 4 * note: 解决AppBarLayout滑动时抖动问题 5 5 */ 6 6 public class FixAppBarLayout extends AppBarLayout.Behavior { 7 7 private static final String TAG = "AppBarLayoutBehavior"; 8 8 9 9 public FixAppBarLayout() { 1010 super(); 1111 } 1212 1313 public FixAppBarLayout(Context context, AttributeSet attrs) { 1414 super(context, attrs); 1515 } 1616 1717 @Override 1818 public boolean onInterceptTouchEvent(CoordinatorLayout parent, AppBarLayout child, MotionEvent ev) { 1919 if (ev.getAction() == ACTION_DOWN) { 2020 Object scroller = getSuperSuperField(this, "mScroller"); 2121 if (scroller != null && scroller instanceof OverScroller) { 2222 OverScroller overScroller = (OverScroller) scroller; 2323 overScroller.abortAnimation(); 2424 } 2525 } 2626 2727 return super.onInterceptTouchEvent(parent, child, ev); 2828 } 2929 3030 private Object getSuperSuperField(Object paramClass, String paramString) { 3131 Field field = null; 3232 Object object = null; 3333 try { 3434 field = paramClass.getClass().getSuperclass().getSuperclass().getDeclaredField(paramString); 3535 field.setAccessible(true); 3636 object = field.get(paramClass); 3737 } catch (Exception e) { 3838 e.printStackTrace(); 3939 } 4040 return object; 4141 } 4242 }

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

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )