Skip to content

Commit

Permalink
1.优化接口;
Browse files Browse the repository at this point in the history
2.优化代码。
  • Loading branch information
jdsjlzx committed Sep 30, 2016
1 parent b78753f commit e00e2d3
Show file tree
Hide file tree
Showing 21 changed files with 468 additions and 541 deletions.
5 changes: 2 additions & 3 deletions LRecyclerview_library/LRecyclerview_library.iml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/annotations" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/bundles" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" />
Expand Down Expand Up @@ -131,15 +130,15 @@
<orderEntry type="library" exported="" name="animated-vector-drawable-24.2.0" level="project" />
<orderEntry type="library" exported="" name="design-24.2.0" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="jsr305-2.0.1" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="espresso-core-2.2.2" level="project" />
<orderEntry type="library" exported="" name="support-media-compat-24.2.0" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="espresso-core-2.2.2" level="project" />
<orderEntry type="library" exported="" name="recyclerview-v7-24.2.0" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="exposed-instrumentation-api-publish-0.5" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="rules-0.5" level="project" />
<orderEntry type="library" exported="" name="support-core-ui-24.2.0" level="project" />
<orderEntry type="library" exported="" name="support-core-utils-24.2.0" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="javax.annotation-api-1.2" level="project" />
<orderEntry type="library" exported="" name="appcompat-v7-24.2.0" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="javax.annotation-api-1.2" level="project" />
<orderEntry type="library" exported="" scope="TEST" name="javax.inject-1" level="project" />
<orderEntry type="library" exported="" name="support-annotations-24.2.0" level="project" />
<orderEntry type="library" exported="" name="support-vector-drawable-24.2.0" level="project" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.github.jdsjlzx.interfaces;

/**
* 加载更多事件
* @author jdsjlzx
* @created 2016/9/30 14:09
*
*/
public interface OnLoadMoreListener {

void onLoadMore();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.github.jdsjlzx.interfaces;

/**
* 下拉刷新事件
* @author jdsjlzx
* @created 2016/9/30 14:17
*
*/

public interface OnRefreshListener {
void onRefresh();
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import android.view.View;
import android.view.ViewParent;

import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.util.RecyclerViewStateUtils;
import com.github.jdsjlzx.view.ArrowRefreshHeader;
import com.github.jdsjlzx.view.LoadingFooter;
Expand All @@ -24,6 +26,8 @@
*/
public class LRecyclerView extends RecyclerView {
private boolean pullRefreshEnabled = true;
private OnRefreshListener mRefreshListener;
private OnLoadMoreListener mLoadMoreListener;
private LScrollListener mLScrollListener;
private ArrowRefreshHeader mRefreshHeader;
private View mEmptyView;
Expand Down Expand Up @@ -181,8 +185,8 @@ public boolean onTouchEvent(MotionEvent ev) {
mLastY = -1; // reset
if (isOnTop() && pullRefreshEnabled && appbarState == AppBarStateChangeListener.State.EXPANDED) {
if (mRefreshHeader.releaseAction()) {
if (mLScrollListener != null) {
mLScrollListener.onRefresh();
if (mRefreshListener != null) {
mRefreshListener.onRefresh();
}
}
}
Expand Down Expand Up @@ -258,31 +262,35 @@ public void setArrowImageView(int resId) {
}
}

public void setOnRefreshListener(OnRefreshListener listener) {
mRefreshListener = listener;
}

public void setOnLoadMoreListener(OnLoadMoreListener listener) {
mLoadMoreListener = listener;
}

public void setLScrollListener(LScrollListener listener) {
mLScrollListener = listener;
}

public interface LScrollListener {

void onRefresh();//pull down to refresh

void onScrollUp();//scroll down to up

void onScrollDown();//scroll from up to down

void onBottom();//load next page

void onScrolled(int distanceX, int distanceY);// moving state,you can get the move distance

void onScrollStateChanged(int state);
}

public void setRefreshing(boolean refreshing) {
if (refreshing && pullRefreshEnabled && mLScrollListener != null) {
if (refreshing && pullRefreshEnabled && mRefreshListener != null) {
mRefreshHeader.setState(ArrowRefreshHeader.STATE_REFRESHING);
mRefreshHeaderHeight = mRefreshHeader.getMeasuredHeight();
mRefreshHeader.onMove(mRefreshHeaderHeight);
mLScrollListener.onRefresh();
mRefreshListener.onRefresh();
}
}

Expand All @@ -291,68 +299,69 @@ public void forceToRefresh() {
if(state == LoadingFooter.State.Loading) {
return;
}
if (pullRefreshEnabled && mLScrollListener != null) {
if (pullRefreshEnabled && mRefreshListener != null) {
scrollToPosition(0);
mRefreshHeader.setState(ArrowRefreshHeader.STATE_REFRESHING);
mRefreshHeader.onMove(mRefreshHeaderHeight);
mLScrollListener.onRefresh();
mRefreshListener.onRefresh();
}
}


@Override
public void onScrolled(int dx, int dy) {
super.onScrolled(dx, dy);
if (null != mLScrollListener) {
int firstVisibleItemPosition = 0;
RecyclerView.LayoutManager layoutManager = getLayoutManager();

if (layoutManagerType == null) {
if (layoutManager instanceof LinearLayoutManager) {
layoutManagerType = LayoutManagerType.LinearLayout;
} else if (layoutManager instanceof GridLayoutManager) {
layoutManagerType = LayoutManagerType.GridLayout;
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
layoutManagerType = LayoutManagerType.StaggeredGridLayout;
} else {
throw new RuntimeException(
"Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager");
}
}

switch (layoutManagerType) {
case LinearLayout:
firstVisibleItemPosition = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
break;
case GridLayout:
firstVisibleItemPosition = ((GridLayoutManager) layoutManager).findFirstVisibleItemPosition();
lastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
break;
case StaggeredGridLayout:
StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
if (lastPositions == null) {
lastPositions = new int[staggeredGridLayoutManager.getSpanCount()];
}
staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions);
lastVisibleItemPosition = findMax(lastPositions);
staggeredGridLayoutManager.findFirstCompletelyVisibleItemPositions(lastPositions);
firstVisibleItemPosition = findMax(lastPositions);
break;
}
int firstVisibleItemPosition = 0;
RecyclerView.LayoutManager layoutManager = getLayoutManager();

// 根据类型来计算出第一个可见的item的位置,由此判断是否触发到底部的监听器
// 计算并判断当前是向上滑动还是向下滑动
calculateScrollUpOrDown(firstVisibleItemPosition, dy);
// 移动距离超过一定的范围,我们监听就没有啥实际的意义了
mScrolledXDistance += dx;
mScrolledYDistance += dy;
mScrolledXDistance = (mScrolledXDistance < 0) ? 0 : mScrolledXDistance;
mScrolledYDistance = (mScrolledYDistance < 0) ? 0 : mScrolledYDistance;
if (mIsScrollDown && (dy == 0)) {
mScrolledYDistance = 0;
if (layoutManagerType == null) {
if (layoutManager instanceof LinearLayoutManager) {
layoutManagerType = LayoutManagerType.LinearLayout;
} else if (layoutManager instanceof GridLayoutManager) {
layoutManagerType = LayoutManagerType.GridLayout;
} else if (layoutManager instanceof StaggeredGridLayoutManager) {
layoutManagerType = LayoutManagerType.StaggeredGridLayout;
} else {
throw new RuntimeException(
"Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager");
}
//Be careful in here
}

switch (layoutManagerType) {
case LinearLayout:
firstVisibleItemPosition = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
break;
case GridLayout:
firstVisibleItemPosition = ((GridLayoutManager) layoutManager).findFirstVisibleItemPosition();
lastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
break;
case StaggeredGridLayout:
StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
if (lastPositions == null) {
lastPositions = new int[staggeredGridLayoutManager.getSpanCount()];
}
staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions);
lastVisibleItemPosition = findMax(lastPositions);
staggeredGridLayoutManager.findFirstCompletelyVisibleItemPositions(lastPositions);
firstVisibleItemPosition = findMax(lastPositions);
break;
}

// 根据类型来计算出第一个可见的item的位置,由此判断是否触发到底部的监听器
// 计算并判断当前是向上滑动还是向下滑动
calculateScrollUpOrDown(firstVisibleItemPosition, dy);
// 移动距离超过一定的范围,我们监听就没有啥实际的意义了
mScrolledXDistance += dx;
mScrolledYDistance += dy;
mScrolledXDistance = (mScrolledXDistance < 0) ? 0 : mScrolledXDistance;
mScrolledYDistance = (mScrolledYDistance < 0) ? 0 : mScrolledYDistance;
if (mIsScrollDown && (dy == 0)) {
mScrolledYDistance = 0;
}
//Be careful in here
if (null != mLScrollListener) {
mLScrollListener.onScrolled(mScrolledXDistance, mScrolledYDistance);
}

Expand All @@ -365,47 +374,50 @@ public void onScrollStateChanged(int state) {

if (mLScrollListener != null) {
mLScrollListener.onScrollStateChanged(state);
}

if (currentScrollState == RecyclerView.SCROLL_STATE_IDLE || currentScrollState == RecyclerView.SCROLL_STATE_SETTLING) {
if (mLoadMoreListener != null) {
if (currentScrollState == RecyclerView.SCROLL_STATE_IDLE) {
RecyclerView.LayoutManager layoutManager = getLayoutManager();
int visibleItemCount = layoutManager.getChildCount();
int totalItemCount = layoutManager.getItemCount();
if (visibleItemCount > 0
&& lastVisibleItemPosition >= totalItemCount - 1
&& totalItemCount > visibleItemCount
&& !isNoMore
&& !mIsScrollDown
//&& !mIsScrollDown
&& mRefreshHeader.getState() != ArrowRefreshHeader.STATE_REFRESHING) {
mLScrollListener.onBottom();
mLoadMoreListener.onLoadMore();
}

}

}


}

/**
* 计算当前是向上滑动还是向下滑动
*/
private void calculateScrollUpOrDown(int firstVisibleItemPosition, int dy) {
if (firstVisibleItemPosition == 0) {
if (!mIsScrollDown) {
mIsScrollDown = true;
mLScrollListener.onScrollDown();
}
} else {
if (mDistance > HIDE_THRESHOLD && mIsScrollDown) {
mIsScrollDown = false;
mLScrollListener.onScrollUp();
mDistance = 0;
} else if (mDistance < -HIDE_THRESHOLD && !mIsScrollDown) {
mIsScrollDown = true;
mLScrollListener.onScrollDown();
mDistance = 0;
if (null != mLScrollListener) {
if (firstVisibleItemPosition == 0) {
if (!mIsScrollDown) {
mIsScrollDown = true;
mLScrollListener.onScrollDown();
}
} else {
if (mDistance > HIDE_THRESHOLD && mIsScrollDown) {
mIsScrollDown = false;
mLScrollListener.onScrollUp();
mDistance = 0;
} else if (mDistance < -HIDE_THRESHOLD && !mIsScrollDown) {
mIsScrollDown = true;
mLScrollListener.onScrollDown();
mDistance = 0;
}
}
}

if ((mIsScrollDown && dy > 0) || (!mIsScrollDown && dy < 0)) {
mDistance += dy;
}
Expand Down
Loading

0 comments on commit e00e2d3

Please sign in to comment.