Recyclerview 顶部悬停 stick

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 是关键

Recyclerview 顶部悬停  stick
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 顶部悬停  stickRecyclerview 顶部悬停  stick

相关推荐