Skip to content

Commit

Permalink
支持手动点击加载更多
Browse files Browse the repository at this point in the history
  • Loading branch information
lizhxian committed Mar 13, 2019
1 parent a903c6b commit 7e0c28a
Show file tree
Hide file tree
Showing 24 changed files with 1,306 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,16 @@ public interface ILoadMoreFooter {
* 加载更多的View
*/
View getFootView();

void setNetworkErrorViewClickListener(OnNetWorkErrorListener listener);

void setOnClickLoadMoreListener(final OnLoadMoreListener listener);

enum State {
Normal/**正常*/
, ManualLoadMore/**手动点击加载*/
, NoMore/**加载到最底了*/
, Loading/**加载中..*/
, NetWorkError/**网络异常*/
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -567,17 +567,16 @@ public void setOnLoadMoreListener(OnLoadMoreListener listener) {
}

public void setOnNetWorkErrorListener(final OnNetWorkErrorListener listener) {
final LoadingFooter loadingFooter = ((LoadingFooter) mFootView);
loadingFooter.setState(LoadingFooter.State.NetWorkError);
loadingFooter.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mLoadMoreFooter.onLoading();
listener.reload();
}
});
mLoadMoreFooter.setNetworkErrorViewClickListener(listener);
}

/**
* 请使用自定义LoadingFooter的方式实现
* @param loading
* @param noMore
* @param noNetWork
*/
@Deprecated
public void setFooterViewHint(String loading, String noMore, String noNetWork) {
if (mLoadMoreFooter instanceof LoadingFooter) {
LoadingFooter loadingFooter = ((LoadingFooter) mLoadMoreFooter);
Expand All @@ -588,11 +587,13 @@ public void setFooterViewHint(String loading, String noMore, String noNetWork) {
}

/**
* 本方法不再推荐使用,请使用自定义LoadingFooter的方式实现
* 设置Footer文字颜色
* @param indicatorColor
* @param hintColor
* @param backgroundColor
*/
@Deprecated
public void setFooterViewColor(int indicatorColor, int hintColor, int backgroundColor) {
if (mLoadMoreFooter instanceof LoadingFooter) {
LoadingFooter loadingFooter = ((LoadingFooter) mLoadMoreFooter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
Expand All @@ -26,6 +27,8 @@
*/
public class LuRecyclerView extends RecyclerView {
private boolean mLoadMoreEnabled = true;
/** 是否手动点击加载更多 */
private boolean mIsManualLoadMore = false;
private boolean mRefreshing = false;//是否正在下拉刷新
private boolean mLoadingData = false;//是否正在加载数据
private boolean flag = false;//标记是否setAdapter
Expand Down Expand Up @@ -221,6 +224,7 @@ public void setRefreshing(boolean refreshing) {
* @param pageSize 一页加载的数量
*/
public void refreshComplete(int pageSize) {
Log.e("lzx","refreshComplete pageSize " + pageSize );
this.mPageSize = pageSize;
if (mRefreshing) {
isNoMore = false;
Expand Down Expand Up @@ -314,8 +318,31 @@ public void setNoMore(boolean noMore){
mLoadingData = false;
isNoMore = noMore;
if(isNoMore) {
mLoadMoreFooter.onNoMore();
mFootView.setVisibility(VISIBLE);
mLoadMoreFooter.onNoMore();
Log.e("lzx","setNoMore true ");
} else {
mLoadMoreFooter.onComplete();
}
}

/**
* 设置是否已加载全部
* @param noMore
* @param isShowFootView
*/
public void setNoMore(boolean noMore, boolean isShowFootView){
mLoadingData = false;
isNoMore = noMore;
if(isNoMore) {
if (isShowFootView) {
mFootView.setVisibility(VISIBLE);
} else {
mFootView.setVisibility(GONE);
mWrapAdapter.removeFooterView();
}
mLoadMoreFooter.onNoMore();
Log.e("lzx","setNoMore true ");
} else {
mLoadMoreFooter.onComplete();
}
Expand Down Expand Up @@ -365,6 +392,17 @@ public void setLoadMoreEnabled(boolean enabled) {
}
}

/**
* 滑动到底手动点击加载
*/
public void setManualLoadMore(boolean enabled) {
if(mWrapAdapter == null){
throw new NullPointerException("mWrapAdapter cannot be null, please make sure the variable mWrapAdapter have been initialized.");
}
mIsManualLoadMore = enabled;

}

public void setLoadingMoreProgressStyle(int style) {
if (mLoadMoreFooter instanceof LoadingFooter) {
((LoadingFooter) mLoadMoreFooter).setProgressStyle(style);
Expand All @@ -377,15 +415,7 @@ public void setOnLoadMoreListener(OnLoadMoreListener listener) {
}

public void setOnNetWorkErrorListener(final OnNetWorkErrorListener listener) {
final LoadingFooter loadingFooter = ((LoadingFooter) mFootView);
loadingFooter.setState(LoadingFooter.State.NetWorkError);
loadingFooter.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mLoadMoreFooter.onLoading();
listener.reload();
}
});
mLoadMoreFooter.setNetworkErrorViewClickListener(listener);
}

public void setFooterViewHint(String loading, String noMore, String noNetWork) {
Expand Down Expand Up @@ -488,25 +518,36 @@ public void onScrolled(int dx, int dy) {
mLScrollListener.onScrolled(mScrolledXDistance, mScrolledYDistance);
}

if (mLoadMoreListener != null && mLoadMoreEnabled) {
int visibleItemCount = layoutManager.getChildCount();
int totalItemCount = layoutManager.getItemCount();
if (visibleItemCount > 0
&& lastVisibleItemPosition >= totalItemCount - 1
&& (isCritical ? totalItemCount >= visibleItemCount : totalItemCount > visibleItemCount)
&& !isNoMore
&& !mRefreshing) {

mFootView.setVisibility(View.VISIBLE);
if (!mLoadingData) {
mLoadingData = true;
mLoadMoreFooter.onLoading();
mLoadMoreListener.onLoadMore();
//如果想要滑动到底部自动加载更多,mIsManualLoadMore必须为false
if (mIsManualLoadMore) {
if (!isNoMore) {
Log.e("lzx","onScrooo set visible");
mLoadingData = true;
mLoadMoreFooter.setOnClickLoadMoreListener(mLoadMoreListener);
}

} else {
if (mLoadMoreListener != null && mLoadMoreEnabled) {
int visibleItemCount = layoutManager.getChildCount();
int totalItemCount = layoutManager.getItemCount();
if (visibleItemCount > 0
&& lastVisibleItemPosition >= totalItemCount - 1
&& (isCritical ? totalItemCount >= visibleItemCount : totalItemCount > visibleItemCount)
&& !isNoMore
&& !mRefreshing) {

mFootView.setVisibility(View.VISIBLE);
if (!mLoadingData) {
mLoadingData = true;
mLoadMoreFooter.onLoading();
mLoadMoreListener.onLoadMore();
}

}

}

}

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import com.github.jdsjlzx.R;
import com.github.jdsjlzx.interfaces.ILoadMoreFooter;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnNetWorkErrorListener;
import com.github.jdsjlzx.progressindicator.AVLoadingIndicatorView;
import com.github.jdsjlzx.recyclerview.ProgressStyle;

Expand Down Expand Up @@ -133,6 +135,29 @@ public View getFootView() {
return this;
}

@Override
public void setNetworkErrorViewClickListener(final OnNetWorkErrorListener listener) {
setState(State.NetWorkError);
setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setState(State.Loading);
listener.reload();
}
});
}

@Override
public void setOnClickLoadMoreListener(final OnLoadMoreListener listener) {
setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setState(State.Loading);
listener.onLoadMore();
}
});
}

/**
* 设置状态
*
Expand Down Expand Up @@ -239,10 +264,5 @@ public void setState(State status, boolean showView) {
}


public enum State {
Normal/**正常*/
, NoMore/**加载到最底了*/
, Loading/**加载中..*/
, NetWorkError/**网络异常*/
}

}
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ android {

defaultConfig {
applicationId "com.lzx.demo"
minSdkVersion 14
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<activity android:name=".ui.MomentsHeaderActivity"/>
<activity android:name=".ui.CustomLoadingFootActivity"/>
<activity android:name=".ui.SpecialLinearLayoutActivity"/>
<activity android:name=".ui.ClickLoadingFootActivity"/>
</application>

</manifest>
8 changes: 8 additions & 0 deletions app/src/main/java/com/lzx/demo/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.lzx.demo;

import android.content.Intent;
import android.os.Bundle;

import com.lzx.demo.base.BaseMainActivity;
import com.lzx.demo.multitype.MultiTypeActivity;
import com.lzx.demo.ui.CommonActivity;
Expand All @@ -22,4 +25,9 @@ public Class<?>[] getActivitys() {
public String[] getTitles() {
return TITLE;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
Loading

0 comments on commit 7e0c28a

Please sign in to comment.