android布局置顶_Android RecyclerView 实现可点击的顶部悬浮条效果

之前的项目中有需要开发一个悬浮置顶的效果,今天看到一篇文章,也是讲到这么一个效果,但是实现方法不同,明显比自己的更有技术成分,所以就恬不知耻的观摩了一下,下面双手奉上链接

然后是我自己实现的一个效果,看上去是一模一样的,但是实现方式却不同,甩你一脸图

因为电脑色差问题,我把悬浮条改成了异常鲜艳的红色,请自觉忽略。

下面附上关键代码//随意添加的一些数据,其中用top字段判断是否存在悬浮条,用日期分类,每个日期的第一条top为true

datas = new ArrayList<>();

datas.add(new WalletBean("20180101",100,false));

datas.add(new WalletBean("20180101",100,false));

datas.add(new WalletBean("20180101",100,false));

datas.add(new WalletBean("20180102",100,false));

datas.add(new WalletBean("20180102",100,false));

datas.add(new WalletBean("20180102",100,false));

datas.add(new WalletBean("20180102",100,false));

datas.add(new WalletBean("20180103",100,false));

datas.add(new WalletBean("20180103",100,false));

datas.add(new WalletBean("20180103",100,false));

datas.add(new WalletBean("20180103",100,false));

datas.add(new WalletBean("20180104",100,false));

datas.add(new WalletBean("20180105",100,false));

linearLayoutManager = new LinearLayoutManager(getActivity());

rcList.setLayoutManager(linearLayoutManager);        //BaseQuickAdapter是我使用的一个快速适配器框架,把代码放入Recycler的adapter中都可以使用

mAdapter = new BaseQuickAdapter(R.layout.item_wallet, datas) {            @Override

protected void convert(BaseViewHolder helper, WalletBean item) {                int pos = helper.getLayoutPosition();                if(pos>=1){                    if(item.getTime().equals(datas.get(pos-1).getTime())){

helper.setVisible(R.id.ll_top,false);

item.setTop(false);

}else{

helper.setVisible(R.id.ll_top,true);

item.setTop(true);

}

}else{

helper.setVisible(R.id.ll_top,true);

item.setTop(true);

}

}

};//重点是这里,给Recycler添加一个crollListener其中llTop是写在布局文件里的

rcList.addOnScrollListener(new RecyclerView.OnScrollListener() {            @Override

public void onScrollStateChanged(RecyclerView recyclerView, int newState) {                super.onScrollStateChanged(recyclerView, newState);

mSuspensionHeight = llTop.getHeight();//悬浮条最底部的y

}            @Override

public void onScrolled(RecyclerView recyclerView, int dx, int dy) {                super.onScrolled(recyclerView, dx, dy);                //获取最顶部的item

View view = linearLayoutManager.findViewByPosition(mCurrentPosition + 1);                    //获取到的view顶部坐标小于悬浮条最底部的y轴坐标就是要重叠了

//datas.get(mCurrentPosition+1).isTo 表示下个item存在悬浮条

if(view.getTop() <= mSuspensionHeight&&datas.get(mCurrentPosition+1).isTop()){

llTop.setY(-(mSuspensionHeight - view.getTop()));

}else{

llTop.setY(0);

}                //mCurrentPosition 不是 当前显示最顶部的position

if(mCurrentPosition != linearLayoutManager.findFirstVisibleItemPosition()){

mCurrentPosition = linearLayoutManager.findFirstVisibleItemPosition();

llTop.setY(0);

}

}

});

llTop.setOnClickListener(new View.OnClickListener() {            @Override

public void onClick(View v) {                // TODO: 2018/3/27 点击事件处理

ToastUtils.show("SHOW");

}

});

下面是xml布局文件

xmlns:android="http://schemas.android.com/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 顶部悬浮条的功能也就这样实现了,当然考虑到代码实际性能可能并不如recyclerView.addItemDecoration,但思路是一样的,并且支持了悬浮条的点击事件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值