[Android] replacing ListView with RecyclerView (3)

Source: Internet
Author: User

[Android] replacing ListView with RecyclerView (3)
Modify the layout as follows: 1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 3 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 4 android: orientation = "vertical" 5 android: layout_width = "match_parent" 6 android: layout_height = "match_parent"> 7 8 <android. support. v7.widget. toolbar 9 android: id = "@ + id/recycler_view_pinned_toolbar" 10 android: layout_height = "wrap_content" 11 android: layout_width = "match_parent" 12 android: background = "? Attr/colorPrimary "13/> 14 <android. support. v4.widget. swipeRefreshLayout15 android: id = "@ + id/recycler_view_pinned_srl" 16 android: layout_width = "match_parent" 17 android: layout_height = "wrap_content" 18> 19 20 <com. wangjie. androidbucket. support. recyclerview. pinnedlayout. pinnedRecyclerViewLayout21 android: id = "@ + id/recycler_view_pinned_layout" 22 android: layout_width = "match_parent" android: layout_height = "mat Ch_parent "> 23 <android. support. v7.widget. recyclerView24 android: id = "@ + id/paper" 25 android: scrollbars = "vertical" 26 android: layout_width = "match_parent" 27 android: layout_height = "match_parent" 28 android: background = "# bbccaa" 29/> 30 <Button31 android: id = "@ + id/recycler_view_pinned_add_btn" 32 android: layout_width = "wrap_content" android: layout_height = "wrap_content" 33 android: layout_center Vertical = "true" 34 android: background = "# abcabc" 35 android: text = "add" 36/> 37 38 </com. wangjie. androidbucket. support. recyclerview. pinnedlayout. pinnedRecyclerViewLayout> 39 40 </android. support. v4.widget. swipeRefreshLayout> 41 42 </LinearLayout> you can see that the RecyclerView is Ayout/PinnedRecyclerViewLayout. java) contained in. This is in the project AndroidBucket (https://github.com/wangjiegulu/AndroidBucket. Let's take a look at how to use it in the Code. The specific implementation will be discussed later. 1 pinnedLayout. initRecyclerPinned (recyclerView, layoutManager, LayoutInflater. from (context ). inflate (R. layout. recycler_view_item_float, null); 2 pinnedLayout. setOnRecyclerViewPinnedViewListener (this); As shown above, the usage is simple: Line1: Initialize and bind PinnedRecyclerViewLayout and RecyclerView, and set the pinnedView Line2 to be topped: Set OnRecyclerViewPinnedViewListener, the function is to call back and re-render the data when the top part is replaced. The incoming OnRecyclerViewPinnedViewListener is this. Obviously, this Activi Ty implements this interface and the implementation code is as follows: 1 // render pinnedView data 2 @ Override 3 public void Preview (pinnedrejavaserviewlayout, View pinnedView, int position) {4 switch (pinnedrepolicerviewlayout. getId () {5 case R. id. recycler_view_pinned_layout: 6 TextView nameTv = (TextView) pinnedView. findViewById (R. id. recycler_view_item_float_name_ TV); 7 nameTv. setText (personList. get (position ). getName () ); 8 TextView ageTv = (TextView) pinnedView. findViewById (R. id. recycler_view_item_float_age_ TV); 9 ageTv. setText (personList. get (position ). getAge () + ""); 10 break; 11} 12} then let's see how PinnedRecyclerViewLayout is implemented. 1/** 2 * Author: wangjie 3 * Email: tiantian.china.2@gmail.com 4 * Date: 2/2/15. 5 */6 public class pinnedrepolicerviewlayout extends RelativeLayout {7 8 private static final String TAG = pinnedrepolicerviewlayout. class. getSimpleName (); 9 10 public static interface OnRecyclerViewPinnedViewListener {11 void onPinnedViewRender (pinnedrepolicerviewlayout, View pinnedView, int Position); 12} 13 14 private OnRecyclerViewPinnedViewListener onRecyclerViewPinnedViewListener; 15 16 public void listener (OnRecyclerViewPinnedViewListener onRecyclerViewPinnedViewListener) {17 this. onRecyclerViewPinnedViewListener = onRecyclerViewPinnedViewListener; 18} 19 20 public pinnedrejavaserviewlayout (Context context) {21 super (context); 22 init (context); 23} 24 25 public PinnedRecyclerViewLayout (Context context, AttributeSet attrs) {26 super (context, attrs); 27 init (context); 28} 29 30 public PinnedRecyclerViewLayout (Context context, AttributeSet attrs, int defStyleAttr) {31 super (context, attrs, defStyleAttr); 32 init (context); 33} 34 35 private void init (Context context) {36} 37 38 private View pinnedView; 39 private ABaseLinearLayoutManager LayoutManager; 40 41 public void initRecyclerPinned (RecyclerView recyclerView, ABaseLinearLayoutManager layoutManager, View pinnedView) {42 this. pinnedView = pinnedView; 43 this. layoutManager = layoutManager; 44 this. addView (this. pinnedView); 45 RelativeLayout. layoutParams lp = new RelativeLayout. layoutParams (ViewGroup. layoutParams. MATCH_PARENT, ViewGroup. layoutParams. WRAP_CONTENT); 46 this. pinn EdView. setLayoutParams (lp); 47 layoutManager. getRecyclerViewScrollManager (). addScrollListener (recyclerView, new listener () {48 @ Override 49 public void listener (RecyclerView recyclerView, int newState) {50} 51 52 @ Override 53 public void onScrolled (RecyclerView recyclerView, int dx, int dy) {54 refreshPinnedView (); 55} 56}); 57 pinnedView. setVisibility (GONE ); 58} 59 60 // save the last position 61 private int lastPosition = RecyclerView. NO_POSITION; 62 63 public void refreshPinnedView () {64 if (null = pinnedView | null = layoutManager) {65 Logger. e (TAG, "Please init pinnedView and layoutManager with initRecyclerPinned method first! "); 66 return; 67} 68 if (VISIBLE! = PinnedView. getVisibility () {69 pinnedView. setVisibility (VISIBLE); 70} 71 int curPosition = layoutManager. findFirstVisibleItemPosition (); 72 if (RecyclerView. NO_POSITION = curPosition) {73 return; 74} 75 View curItemView = layoutManager. findViewByPosition (curPosition); 76 if (null = curItemView) {77 return; 78} 79 // if the current curPosition is different from the previous lastPosition, the data needs to be refreshed again, avoid refresh Same data 80 if (curPosition! = LastPosition) {81 if (null! = OnRecyclerViewPinnedViewListener) {82 onRecyclerViewPinnedViewListener. onPinnedViewRender (this, pinnedView, curPosition); 83} 84 lastPosition = curPosition; 85} 86 87 int displayTop; 88 int itemHeight = curItemView. getHeight (); 89 int curTop = curItemView. getTop (); 90 int floatHeight = pinnedView. getHeight (); 91 if (curTop <floatHeight-itemHeight) {92 displayTop = itemHeight + curTop-flo AtHeight; 93} else {94 displayTop = 0; 95} 96 RelativeLayout. layoutParams lp = (LayoutParams) pinnedView. getLayoutParams (); 97 lp. topMargin = displayTop; 98 pinnedView. setLayoutParams (lp); 99 pinnedView. invalidate (); 100} 101 102 103} The pinnedrepolicerviewlayout inherits RelativeLayout, because we need to add a top pinnedView to it and overwrite the RecyclerView. Line44: add the imported pinnedView to PinnedRecyclerViewLayout. Line47 ~ 56: Add a rolling listener to ABaseLinearLayoutManager, because we need to dynamically change the position of the pinnedView when rolling, so as to simulate the top effect. And call refreshPinnedView when scrolling to refresh the position of the pinnedView. Line57: When the initRecyclerPinned method is called, The RecyclerView may not have a data source, so you do not need to display this pinnedView. You can display it again when it is actually scrolling. The refreshPinnedView () method is used to refresh the position and displayed data of pinnedView while scrolling: Line71 ~ 78: Use layoutManager to get the position of the first display data, and then obtain the View of the first display according to the position. Line79 ~ 85: if the current curPosition is different from the last lastPosition, the data needs to be refreshed again to avoid refresh the same data repeatedly when the curPosition is the same. Line87 ~ 95: based on the current first View displayed, the distance to be moved up in pinnedView is calculated based on its top, its height, and the height of pinnedView (a geometric chart is clearly displayed ). Line96 ~ 99: refresh the position of pinnedView

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.