2023年7月13日发(作者:)
android布局置顶_AndroidRecyclerView实现可点击的顶部悬浮条效果之前的项⽬中有需要开发⼀个悬浮置顶的效果,今天看到⼀篇⽂章,也是讲到这么⼀个效果,但是实现⽅法不同,明显⽐⾃⼰的更有技术成分,所以就恬不知耻的观摩了⼀下,下⾯双⼿奉上链接然后是我⾃⼰实现的⼀个效果,看上去是⼀模⼀样的,但是实现⽅式却不同,甩你⼀脸图因为电脑⾊差问题,我把悬浮条改成了异常鲜艳的红⾊,请⾃觉忽略。下⾯附上关键代码//随意添加的⼀些数据,其中⽤top字段判断是否存在悬浮条,⽤⽇期分类,每个⽇期的第⼀条top为truedatas = new ArrayList<>();(new WalletBean("20180101",100,false));(new WalletBean("20180101",100,false));(new WalletBean("20180101",100,false));(new WalletBean("20180102",100,false));(new WalletBean("20180102",100,false));(new WalletBean("20180102",100,false));(new WalletBean("20180102",100,false));(new WalletBean("20180103",100,false));(new WalletBean("20180103",100,false));(new WalletBean("20180103",100,false));(new WalletBean("20180103",100,false));(new WalletBean("20180104",100,false));(new WalletBean("20180105",100,false));linearLayoutManager = new LinearLayoutManager(getActivity());outManager(linearLayoutManager); //BaseQuickAdapter是我使⽤的⼀个快速适配器框架,把代码放⼊Recycler的adapter中都可以使⽤mAdapter = new BaseQuickAdapter(_wallet, datas) { @Overrideprotected void convert(BaseViewHolder helper, WalletBean item) { int pos = outPosition(); if(po{ if(e().equals((pos-1).getTime())){ible(_top,false);(false);}else{ible(_top,true);(true);}}else{ible(_top,true);(true);}}};//重点是这⾥,给Recycler添加⼀个crollListener其中llTop是写在布局⽂件⾥的crollListener(new llListener() { @Overridepublic void onScrollStateChanged(RecyclerView recyclerView, int newState) { llStateChanged(recyclerViemSuspensionHeight = ght();//悬浮条最底部的y} @Overridepublic void onScrolled(RecyclerView recyclerView, int dx, int dy) { lled(recyclerView, dx, dy); //获取最顶部的itemView view = ewByPosition(mCurrentPosition + 1); //获取到的view顶部坐标⼩于悬浮条最底部的y轴坐标就是要重叠了//(mCurrentPosition+1).isTo 表⽰下个item存在悬浮条if(() <= mSuspensionHeight&&(mCurrentPosition+1).isTop()){(-(mSuspensionHeight - ()));}else{(0);} //mCurrentPosition 不是 当前显⽰最顶部的positionif(mCurrentPosition != rstVisibleItemPosition()){mCurrentPosition = rstVisibleItemPosition();(0);}}});lickListener(new kListener() { @Overridepublic void onClick(View v) { // TODO: 2018/3/27 点击事件处理("SHOW");}});下⾯是xml布局⽂件xmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/gray_bg">android:id="@+id/swipeLayout"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical">android:background="@color/color_white"android:id="@+id/rc_list"android:scrollbars="none"android:layout_width="match_parent"android:layout_height="match_parent">android:id="@+id/ll_top"android:background="@color/red"android:paddingRight="20dp"android:paddingLeft="20dp"android:paddingBottom="9dp"android:paddingTop="9dp"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="wrap_content">android:id="@+id/tv_time_day"android:layout_weight="1"android:layout_width="0dp"android:layout_height="wrap_content"android:text="2018年2⽉20⽇"android:textColor="@color/color_rb_checked_false"android:textSize="13sp"/>android:id="@+id/tv_sum_money"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="合计 -1075.51元"android:textColor="@color/color_rb_checked_false"android:textSize="13sp"/>android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:background="@drawable/list_touch_bg">android:id="@+id/ll_top"android:background="@color/red"android:paddingRight="20dp"android:paddingLeft="20dp"android:paddingBottom="9dp"android:paddingTop="9dp"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="wrap_content">android:id="@+id/tv_time_day"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="2018年2⽉20⽇"android:textColor="@color/color_rb_checked_false"android:textSize="13sp" />android:id="@+id/tv_sum_money"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="合计 -1075.51元"android:textColor="@color/color_rb_checked_false"android:textSize="13sp"/>android:paddingBottom="12dp"android:paddingLeft="20dp"android:paddingRight="20dp"android:paddingTop="12dp"android:background="@color/color_white"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="wrap_content">android:id="@+id/tv_time"android:layout_weight="1"android:layout_width="0dp"android:layout_height="wrap_content"android:text="21:44 结算"android:textColor="@color/color_rb_checked_true"android:textSize="17sp"/>android:id="@+id/tv_money"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="-1075.51元"android:textColor="@color/color_rb_checked_true"android:textSize="17sp"/>android:background="@color/gray_bg"android:layout_width="match_parent"android:layout_height="1dp"/>可以看到我在SwipeRefreshLayout中放了⼀个lltop作为悬浮条,每个item中都给了同样布局的悬浮条,但默认隐藏,所以我们的点击事件实际操作的是lltop,⾄于更新lltop中的数据就不⽤说了,滑动到下个悬浮条的时候更新就可以了。RecyclerView 顶部悬浮条的功能也就这样实现了,当然考虑到代码实际性能可能并不如mDecoration,但思路是⼀样的,并且⽀持了悬浮条的点击事件。
发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1689215985a222272.html
评论列表(0条)