标签: recyclerview-瀑布流
2016-06-15 17:29 1587人阅读 收藏 举报
http://www.imooc.com/qadetail/91200 我在这个地址搜索到了,如何监听RecyclerView滑动到底的状态
而在实现完瀑布流后,觉得滑动到底部时,最后一行的高度,没有占满外部View,感觉不太好。(真正的瀑布流应该是条目数近乎无穷,可以一直加载更多)
既然是瀑布流,那么就选用StaggeredGridLayoutManager。
mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL)); //纵向3列
import android.content.Context;import android.support.annotation.Nullable;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget.RecyclerView;import android.support.v7.widget.StaggeredGridLayoutManager;import android.util.AttributeSet;import android.view.View;import android.view.ViewGroup;import java.util.Arrays;/** * 实现了滑动到底部的处理,暂未添加自定义监听器 * 实现了当LayoutManger是StaggeredGridLayoutManager时,滑到底部的那一行上的子view高度占满RecyclerView * * author : stone * email : aa86799@163.com * time : 16/5/6 14 23 */public class RichRecyclerView extends RecyclerView { public RichRecyclerView(Context context) { this(context, null); } public RichRecyclerView(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public RichRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onMeasure(int widthSpec, int heightSpec) { super.onMeasure(widthSpec, heightSpec); } @Override public void onScrollStateChanged(int state) { if (state == RecyclerView.SCROLL_STATE_IDLE) { LayoutManager layoutManager = getLayoutManager(); if (layoutManager instanceof StaggeredGridLayoutManager) { StaggeredGridLayoutManager lm = (StaggeredGridLayoutManager) layoutManager; int columnCount = lm.getSpanCount(); int positions[] = new int[columnCount]; lm.findLastVisibleItemPositions(positions); System.out.println("----" +Arrays.toString(positions)); for (int i = 0; i < positions.length; i++) { } for (int i = 0; i < positions.length; i++) { /** * 判断lastItem的底边到recyclerView顶部的距离 * 是否小于recyclerView的高度 * 如果小于或等于 说明滚动到了底部 */ if (positions[i] >= lm.getItemCount() - columnCount) { View child = lm.findViewByPosition(positions[i]); ViewGroup.LayoutParams layoutParams = child.getLayoutParams(); layoutParams.height = getHeight() - child.getBottom() + child.getHeight(); child.setLayoutParams(layoutParams); } } int[] into = new int[columnCount]; lm.findFirstCompletelyVisibleItemPositions(into); for (int i = 0; i < into.length; i++) { System.out.println("首次完全可见的view位置:" + into[i]); } lm.findFirstVisibleItemPositions(into); for (int i = 0; i < into.length; i++) { System.out.println("首次可见的view位置(即使部份可见):" + into[i]); } } else if (layoutManager instanceof LinearLayoutManager){ LinearLayoutManager lm = (LinearLayoutManager) layoutManager; int position = lm.findLastVisibleItemPosition(); if (position + 1 == lm.getItemCount()) { System.out.println("滑动到底了2"); } } } super.onScrollStateChanged(state); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
相应的adapter:
/** * author : stone * email : aa86799@163.com * time : 16/4/9 16 22 */public class StaggeredAdapter extends BaseRecyclerViewAdapter<String> { private List<Integer> mWHs; private int mOrientation; public StaggeredAdapter(Context context, int layoutID, List<String> datas, int orientation) { super(context, layoutID, datas); this.mWHs = new ArrayList<>(); this.mOrientation = orientation; for (int i = 0, len = datas.size(); i < len; i++) { if (mOrientation == StaggeredGridLayoutManager.VERTICAL) { mWHs.add((int) (100 + Math.random() * 100)); } else { mWHs.add((int) (80 + Math.random() * 100)); } } } @Override public void onBindViewHolderSetData(@NonNull BaseRecyclerViewHolder holder, int position) { holder.itemView.setBackgroundColor(getColor()); String str = getItem(position); holder.setText(R.id.tv_text, str); ViewGroup.LayoutParams params = holder.itemView.getLayoutParams(); if (params == null) { params = new ViewGroup.LayoutParams(-2, -2); } if (mOrientation == StaggeredGridLayoutManager.VERTICAL) { params.height = mWHs.get(position); } else { params.width = mWHs.get(position); } holder.itemView.setLayoutParams(params); } /** * 随机颜色 * * @return */ @CheckResult private int getColor() { StringBuilder sb = new StringBuilder(); Random random = new Random(); String temp; for (int i = 0; i < 3; i++) { temp = Integer.toHexString(random.nextInt(0xFF)); if (temp.length() == 1) { temp = "0" + temp; } sb.append(temp); } return Color.parseColor("#" + sb.toString()); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
item_adapter.xml:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/sel_item_back"> <TextView android:id="@+id/tv_text" android:layout_width="match_parent" android:layout_height="match_parent" android:focusable="true" android:focusableInTouchMode="true" android:gravity="center"/></RelativeLayout>