男人要么穿上軍裝保家衛(wèi)國,要么穿上西裝運(yùn)籌帷幄
最近看戰(zhàn)狼二,看的是激動不已,今天也趁熱打鐵總結(jié)下recyclerVeiw中一些常見的小知識點(diǎn)。
如果在開發(fā)中要嵌套scrollview怎么辦?
其實(shí)在開發(fā)中嵌套scrollview網(wǎng)上有的說重寫LayoutManager等等,效果不是很明顯,你可以去搜搜。現(xiàn)在給出的解決方案是
NestedScrollView 包裹RecyclerView。媽媽再也不用擔(dān)心嵌套滑動的問題。然后在代碼中加上:
my_recyler.setNestedScrollingEnabled(false);
這就是解決嵌套recyclerVeiw的終極解決方法。
RecyclerView.OnScrollListener
(1)滾動事件分類
列表的滾動一般分為兩種:
1.手指按下 -> 手指拖拽列表移動 -> 手指停止拖拽 -> 抬起手指
2.手指按下 -> 手指快速拖拽后抬起手指 -> 列表繼續(xù)滾動 -> 停止?jié)L動
上面的過程的狀態(tài)變化如下:
1.靜止 -> 被迫拖拽移動 -> 靜止
2.靜止 -> 被迫拖拽移動 -> 自己滾動 -> 靜止
OnScrollListener
/**
* An OnScrollListener can be added to a RecyclerView to receive messages when a scrolling event
* has occurred on that RecyclerView.
* <p>
* @see RecyclerView#addOnScrollListener(OnScrollListener)
* @see RecyclerView#clearOnChildAttachStateChangeListeners()
*
*/
public abstract static class OnScrollListener {
/**
* Callback method to be invoked when RecyclerView's scroll state changes.
*
* @param recyclerView The RecyclerView whose scroll state has changed.
* @param newState The updated scroll state. One of {@link #SCROLL_STATE_IDLE},
* {@link #SCROLL_STATE_DRAGGING} or {@link #SCROLL_STATE_SETTLING}.
*/
public void onScrollStateChanged(RecyclerView recyclerView, int newState){}
/**
* Callback method to be invoked when the RecyclerView has been scrolled. This will be
* called after the scroll has completed.
* <p>
* This callback will also be called if visible item range changes after a layout
* calculation. In that case, dx and dy will be 0.
*
* @param recyclerView The RecyclerView which scrolled.
* @param dx The amount of horizontal scroll.
* @param dy The amount of vertical scroll.
*/
public void onScrolled(RecyclerView recyclerView, int dx, int dy){}
}
onScrollStateChanged
回調(diào)的兩個(gè)變量的含義:
- recyclerView: 當(dāng)前在滾動的RecyclerView
- newState: 當(dāng)前滾動狀態(tài)
newState 狀態(tài)碼
/**
* The RecyclerView is not currently scrolling.(靜止沒有滾動)
*/
public static final int SCROLL_STATE_IDLE = 0;
/**
* The RecyclerView is currently being dragged by outside input such as user touch input.
*(正在被外部拖拽,一般為用戶正在用手指滾動)
*/
public static final int SCROLL_STATE_DRAGGING = 1;
/**
* The RecyclerView is currently animating to a final position while not under outside control.
*(自動滾動)
*/
public static final int SCROLL_STATE_SETTLING = 2;
狀態(tài)發(fā)生改變的時(shí)候會調(diào)用。接下來針對兩種滑動過程具體onScrollStateChanged 是怎么打印信息的
- 1.靜止 -> 被迫拖拽移動 -> 靜止

- 2.靜止 -> 被迫拖拽移動 -> 自己滾動 -> 靜止

onScrolled()
回調(diào)的三個(gè)變量含義:
recyclerView : 當(dāng)前滾動的view
dx : 水平滾動距離
dy : 垂直滾動距離
dx > 0 時(shí)為手指向左滾動,列表滾動顯示右面的內(nèi)容
dx < 0 時(shí)為手指向右滾動,列表滾動顯示左面的內(nèi)容
dy > 0 時(shí)為手指向上滾動,列表滾動顯示下面的內(nèi)容
dy < 0 時(shí)為手指向下滾動,列表滾動顯示上面的內(nèi)容
具體的日志信息就不打印了。
不過這個(gè)方法值判斷recyclerVeiw有么有滑動到底部的方法
判斷滑動到底部的方法
方法一:第一個(gè)可見item的位置 + 當(dāng)前可見的item個(gè)數(shù) >= item的總個(gè)數(shù)
loadingMoreListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dy > 0) //向下滾動
{
int visibleItemCount = mLinearLayoutManager.getChildCount();//該方法是得到當(dāng)前可見的子視圖的個(gè)數(shù)
int totalItemCount = mLinearLayoutManager.getItemCount();//返回的是當(dāng)前所有的item的個(gè)數(shù)
int pastVisiblesItems = mLinearLayoutManager.findFirstVisibleItemPosition();//返回的是從頂部開始劃出屏幕是個(gè)數(shù),即已經(jīng)可見的視圖的個(gè)數(shù)
if (!loading && (visibleItemCount + pastVisiblesItems) >= totalItemCount) {
loading = true;
loadMoreDate();
}
}
}
};

方法二:通過canScrollVertically 來判斷
loadingMoreListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if(!loading && !recyclerView.canScrollVertically(1)){
loading = true;
loadMoreDate();
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
// if (dy > 0) //向下滾動
// {
// int visibleItemCount = mLinearLayoutManager.getChildCount();
// int totalItemCount = mLinearLayoutManager.getItemCount();
// int pastVisiblesItems = mLinearLayoutManager.findFirstVisibleItemPosition();
//
// if (!loading && (visibleItemCount + pastVisiblesItems) >= totalItemCount) {
// loading = true;
// loadMoreDate();
// }
// }
}
};
方法三:
RecyclerView.OnScrollListener listener = new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
Log.i("jimmy","onScrollStateChanged"+"滑動狀態(tài)"+newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
// Log.i("jimmy","onScrolled"+"滑動dx的值="+dx+"滑動dy的值"+dy);
if (dy > 0) //向下滾動
{
// int visibleItemCount = recyclerView.getLayoutManager().getChildCount();
int totalItemCount = recyclerView.getLayoutManager().getItemCount();
// int pastVisiblesItems = manager.findFirstVisibleItemPosition();
int lastvisibleitemcount = manager.findLastVisibleItemPosition();
// Log.i("jimmy","onScrolled==visibleItemCount的值=="+visibleItemCount+"totalItemCount的值為==="+totalItemCount+"pastVisiblesItems的值為==="+pastVisiblesItems);
Log.i("jimmy","onScrolled===lastvisibleitemcount的值=="+lastvisibleitemcount);
// if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
// Log.i("jimmy","onScrolled滑動到底部了");
// }
if ((lastvisibleitemcount + 1) >= totalItemCount) {
Log.i("jimmy","onScrolled滑動到底部了");
}
}
}
};

最后還要說說adapter的封裝問題,水平有限
推薦個(gè)學(xué)習(xí)鏈接:https://github.com/CymChad/BaseRecyclerViewAdapterHelper