activity布局   ll_top代表要悬停的部分  这里面我放了 图片和文本

 1 <?xml version="1.0" encoding="utf-8"?>
2 <RelativeLayout
3 xmlns:android="http://schemas.android.com/apk/res/android"
4 xmlns:tools="http://schemas.android.com/tools"
5 android:layout_width="match_parent"
6 android:layout_height="match_parent"
7 android:paddingBottom="@dimen/activity_vertical_margin"
8 android:paddingLeft="@dimen/activity_horizontal_margin"
9 android:paddingRight="@dimen/activity_horizontal_margin"
10 android:paddingTop="@dimen/activity_vertical_margin"
11 tools:context="com.demon.testservice.ui.RelistActivity">
12
13 <FrameLayout
14 android:layout_width="match_parent"
15 android:layout_height="match_parent">
16
17 <android.support.v7.widget.RecyclerView
18 android:id="@+id/recyclerView"
19 android:layout_width="match_parent"
20 android:layout_height="match_parent">
21
22 </android.support.v7.widget.RecyclerView>
23
24
25 <LinearLayout
26 android:id="@+id/ll_top"
27 android:layout_width="match_parent"
28 android:layout_height="wrap_content"
29 android:orientation="vertical">
30
31
32 <LinearLayout
33 android:layout_width="match_parent"
34 android:layout_height="wrap_content"
35 android:background="@color/white"
36 android:gravity="center_vertical"
37 android:orientation="horizontal">
38
39 <ImageView
40 android:layout_width="30dp"
41 android:layout_height="30dp"
42 android:src="@drawable/ic_launcher"/>
43
44 <TextView
45 android:id="@+id/tv_top"
46 android:layout_width="wrap_content"
47 android:layout_height="wrap_content"
48 android:text="name"/>
49
50 </LinearLayout>
51
52
53 <View
54 android:id="@+id/top_divider"
55 android:layout_width="match_parent"
56 android:layout_height="0.2dp"
57 android:background="#33000000"/>
58
59 </LinearLayout>
60
61
62 </FrameLayout>
63
64
65 </RelativeLayout>

item布局    上面和ll_top一样,下面部分是显示一张大图片

 1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="wrap_content"
5 android:orientation="vertical"
6 >
7
8 <LinearLayout
9 android:layout_width="match_parent"
10 android:layout_height="wrap_content"
11 android:gravity="center_vertical"
12 android:orientation="horizontal"
13 android:background="@color/white"
14 >
15
16 <ImageView
17 android:layout_width="30dp"
18 android:layout_height="30dp"
19 android:src="@drawable/ic_launcher"/>
20
21 <TextView
22 android:layout_width="wrap_content"
23 android:layout_height="wrap_content"
24 android:text="name"/>
25
26 </LinearLayout>
27
28 <View
29
30 android:layout_width="match_parent"
31 android:layout_height="0.2dp"
32 android:background="#33000000" />
33
34
35 <ImageView
36 android:paddingTop="5dp"
37 android:layout_width="wrap_content"
38 android:layout_height="wrap_content"
39 android:src="@drawable/user"/>
40
41
42 </LinearLayout>

activity   设置addOnScrollListener 是关键

1 public class RelistActivity extends AppCompatActivity {
2
3 @Bind(R.id.recyclerView)
4 RecyclerView recyclerView;
5
6 @Bind(R.id.ll_top)
7 LinearLayout ll_top;
8
9 @Bind(R.id.tv_top)
10 TextView tv_top;
11
12 private int height;
13 private int currentPosition = 0;
14
15 @Override
16 protected void onCreate(Bundle savedInstanceState) {
17 super.onCreate(savedInstanceState);
18 setContentView(R.layout.activity_relist);
19 ButterKnife.bind(this);
20
21 final LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
22 RelistAdapter adapter=new RelistAdapter();
23
24 recyclerView.setLayoutManager(linearLayoutManager);
25 recyclerView.setAdapter(adapter);
26
27
28 recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
29 @Override
30 public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
31 super.onScrollStateChanged(recyclerView, newState);
32
33 height=ll_top.getHeight();
34
35 }
36
37 @Override
38 public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
39 super.onScrolled(recyclerView, dx, dy);
            if (currentPosition!=linearLayoutManager.findFirstVisibleItemPosition()){
51 currentPosition=linearLayoutManager.findFirstVisibleItemPosition();
52 ll_top.setY(0);
53
54 tv_top.setText(" "+currentPosition);
55 }
40 View view=linearLayoutManager.findViewByPosition(currentPosition+1); 41 if (view==null)return; 42 43 if (view.getTop()<=height){ 44 ll_top.setY(-(height-view.getTop())); 45 }else { 46 ll_top.setY(0); 47 } 48 49 50 56 } 57 }); 58 59 } 60 61 62 63 64 65 66 67 class RelistAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{ 68 69 70 @Override 71 public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 72 View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_re,parent,false); 73 return new ReHolder(view); 74 } 75 76 @Override 77 public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 78 79 } 80 81 @Override 82 public int getItemCount() { 83 return 10; 84 } 85 86 class ReHolder extends RecyclerView.ViewHolder{ 87 88 public ReHolder(View itemView) { 89 super(itemView); 90 ButterKnife.bind(this,itemView); 91 } 92 } 93 } 94 95 96 }

Recyclerview 顶部悬停 stick的更多相关文章

  1. Android滑动到顶部悬停

    无图说卵,先上图 jianshu-top.gif 查阅资料后,发现网上大部分都是用这种方法实现的: 多写一个和需要悬浮的部分一模一样的layout,先把浮动区域的可见性设置为gone.当浮动区域滑动到 ...

  2. Android ScrollView滚动实现大众点评、网易云音乐评论悬停效果

    今天听着网易云音乐,写着代码,真是爽翻了. http://blog.csdn.net/linshijun33/article/details/47910833 网易云音乐这个产品亮点应该在评论这一模块 ...

  3. RecyclerView+FloatingActionButton应用

    一.效果图 二.实现步骤 1.XML布局-添加依赖 <LinearLayout android:id="@+id/layout" android:layout_width=& ...

  4. collectionview cell吸顶效果

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "Hiragino Sans GB"; color: #cf8724 } ...

  5. android动画源码合集、动态主题框架、社交app源码等

    Android精选源码 仿MIUI果冻视图-BouncingJellyView   一个快速易用的动态主题框架   android动画效果集合源码   android使用Kotlin开发的Dribbb ...

  6. 前端面试题总结(js、html、小程序、React、ES6、Vue、算法、全栈热门视频资源)

    写在前面 参考答案及资源在看云平台发布,如果大家想领取资源以及查看答案,可直接前去购买.一次购买永久可看,文档长期更新!有什么意见与建议欢迎您及时联系作者或留言回复! 文档描述 本文是关注微信小程序的 ...

  7. NestedScrollView

    参考文章: Android滑动到顶部悬停 NestedScrollView的使用 效果图: 实现步骤: 将需要悬浮的layout放到CollapsingToolbarLayout之外,AppBarLa ...

  8. 折叠表格思路及遇到的问题(tableView:viewForHeaderInSection:的section从1开始,不是从0开始)

    项目需要做了一个类似qq联系人的折叠表格,思路很简单:设置每个section的header,在header上显示组名等信息,然后根据折叠与否,设置每个section中cell的数量,如果折叠,则将之设 ...

  9. 【iOS开发-58】tableView初识:5个重要方法的使用和2种样式的差别

    创建一个tableView,直接拖拽放在storyboard里面就可以. (1)先创建一个数据模型类WSCarGroup,在WSCarGroup.h文件里: #import <Foundatio ...

随机推荐

  1. Python 1-2模块的循环导入问题

    run.py文件: import m1 # 第一次导入 # 验证解决方案一: ''' 正在导入m1 正在导入m2 ''' # print(m1.x) # print(m1.y) # 验证解决方案二: ...

  2. <vue>…… v-if 与 v-show ……//

    #v-if 用法: 根据表达式的值的真假条件渲染元素.在切换时元素及它的数据绑定 / 组件被销毁并重建.如果元素是 <template> ,将提出它的内容作为条件块. 当条件变化时该指令触 ...

  3. hdu 3943 经典数位dp好题

    /* 题意:求出p-q的第j个nya数 数位dp,求出p-q的所有nya数的个数很好求,但是询问求出最终那个第j个值时是我不会求了看了下别人的思路 具体就是把p-q的第j个转化成0-q的第low+j个 ...

  4. 【NOIP2017练习】怎样学习哲学(计数,DP)

    题意:OI大师抖儿在夺得银牌之后,顺利保送pku.这一天,抖儿问长者:“虽然我已经保送了,但是我还要参加学考.马上就要考政治了,请问应该怎样学习哲学,通过政治考试?”  长者回答:“你啊,Too Yo ...

  5. 2015山东信息学夏令营 Day5T3 路径

    问题描述: 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小费用. 输入: 第一行包含一个正整数n,表示点数. 接下来 ...

  6. Thinkphp5.0 的实践一

    Thinkphp5.0 的实践一 tp5.0默认没有__SELF__,需要定义, define('__SELF__',strip_tags($_SERVER['REQUEST_URI'])); tp5 ...

  7. 深入理解hadoop(一)

    hadoop 前世今生  hadoop最早起源于开源收缩引擎nutch,由dong cutting 贡献,但由于nutch最初的设计不能解决数10亿级别的文件存储和索引而遇到了严重的可扩展性问题,直到 ...

  8. sql语句执行的很慢

    一个 SQL 执行的很慢,我们要分两种情况讨论: 1.大多数情况下很正常,偶尔很慢,则有如下原因 (1).数据库在刷新脏页,例如 redo log 写满了需要同步到磁盘. (2).执行的时候,遇到锁, ...

  9. omnidazzle是mac的画笔工具

    先使用命令 brew cask install omnidazzle 试试,不行参考下面: http://macappstore.org/omnidazzle/

  10. Logstash学习系列之插件介绍

    Logstash插件获取方式 插件获取地址: https://github.com/logstash-plugins  在线安装: /plugin install logstash-input-jdb ...