Commit 9e815192 by gao.chao

懒加载

parent b06a8f29
......@@ -44,6 +44,10 @@
<activity
android:name=".StartActivity"
android:theme="@style/AppThemeLaunch" />
<activity android:name=".ListLazyLoadingActivity2" />
<activity android:name=".ListLazyLoadingActivity" />
<activity android:name=".TestListLazyLoadingActivity" />
<activity android:name=".PlayerActivity" />
<activity android:name=".PlayerActivtiy2" />
<activity android:name=".FunctionActivity" />
......
package com.mayi.demo;
import android.os.Bundle;
import android.os.Handler;
import com.mayi.fastdevelop.base.BaseActivity;
import com.mayi.fastdevelop.view.lazyLoading.ListLazyLoadingView;
import java.util.ArrayList;
import java.util.List;
public class ListLazyLoadingActivity extends BaseActivity implements ListLazyLoadingView.ListLazyLoadingViewListener {
private ListLazyLoadingView view;
private MyListLazyLoadingAdapter adapter;
private Handler handler;
private List<Integer> list = new ArrayList<>();
private int addItemCount, maxItemCount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_1);
addItemCount = getIntent().getIntExtra("addItemCount", 10);
maxItemCount = getIntent().getIntExtra("maxItemCount", 50);
for (int i = 0; i < 20; i++) {
list.add(0);
}
handler = new Handler();
view = findViewById(R.id.view);
view.setListLazyLoadingViewListener(this);
adapter = new MyListLazyLoadingAdapter(list);
view.setListLazyLoadingAdapter(adapter);
}
@Override
public void onLoadMore() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
for (int i = 0; i < addItemCount; i++) {
list.add(0);
}
if (list.size() >= maxItemCount) {
view.setLoadingAll(true);
}
view.finishLoadMore();
adapter.notifyDataSetChanged();
}
}, 1000);
}
}
package com.mayi.demo;
import android.os.Bundle;
import android.os.Handler;
import com.mayi.fastdevelop.base.BaseActivity;
import com.mayi.fastdevelop.view.lazyLoading.ListLazyLoadingView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import java.util.ArrayList;
import java.util.List;
public class ListLazyLoadingActivity2 extends BaseActivity implements ListLazyLoadingView.ListLazyLoadingViewListener {
private ListLazyLoadingView view;
private MyListLazyLoadingAdapter adapter;
private Handler handler;
private List<Integer> list = new ArrayList<>();
private int addItemCount, maxItemCount;
private SmartRefreshLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_2);
addItemCount = getIntent().getIntExtra("addItemCount", 10);
maxItemCount = getIntent().getIntExtra("maxItemCount", 50);
for (int i = 0; i < 20; i++) {
list.add(0);
}
handler = new Handler();
layout = findViewById(R.id.layout);
view = findViewById(R.id.view);
view.setListLazyLoadingViewListener(this);
adapter = new MyListLazyLoadingAdapter(list);
view.setListLazyLoadingAdapter(adapter);
layout.setEnableRefresh(true);
layout.setEnableLoadmore(false);
layout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(RefreshLayout refreshlayout) {
list.clear();
for (int i = 0; i < 20; i++) {
list.add(0);
}
layout.finishRefresh();//完成刷新
view.setLoadingAll(false);
adapter.notifyDataSetChanged();
}
});
}
@Override
public void onLoadMore() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
for (int i = 0; i < addItemCount; i++) {
list.add(0);
}
if (list.size() >= maxItemCount) {
view.setLoadingAll(true);
}
view.finishLoadMore();
adapter.notifyDataSetChanged();
}
}, 1000);
}
}
package com.mayi.demo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.mayi.fastdevelop.view.lazyLoading.ListLazyLoadingAdapter;
import java.util.List;
public class MyListLazyLoadingAdapter extends ListLazyLoadingAdapter<MyListLazyLoadingAdapter.TVVH> {
private List<Integer> list;
public MyListLazyLoadingAdapter(List<Integer> list) {
this.list = list;
}
@Override
public void onBindItemVH(@NonNull TVVH holder, int position) {
holder.tv.setText("第"+position+"个");
}
@Override
public List getList() {
return list;
}
@Override
public TVVH getCreateViewHolder(@NonNull ViewGroup parent,int viewType) {
return new TVVH(LayoutInflater.from(parent.getContext()).
inflate(R.layout.item_tv,parent,false));
}
public class TVVH extends RecyclerView.ViewHolder {
TextView tv;
public TVVH(@NonNull View itemView) {
super(itemView);
tv = itemView.findViewById(R.id.tv);
}
}
}
package com.mayi.demo;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import com.mayi.fastdevelop.base.BaseActivity;
public class TestListLazyLoadingActivity extends BaseActivity {
private EditText et, et2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_1);
et = findViewById(R.id.et1);
et2 = findViewById(R.id.et2);
findViewById(R.id.bt).setOnClickListener(this);
findViewById(R.id.bt1).setOnClickListener(this);
}
@Override
protected void onMultiClick(View v) {
Bundle bundle = new Bundle();
String s = et.getText().toString();
int i = Integer.valueOf(s);
bundle.putInt("addItemCount", i);
s = et2.getText().toString();
i = Integer.valueOf(s);
bundle.putInt("maxItemCount", i);
if (v.getId() == R.id.bt) {
gotoActivity(ListLazyLoadingActivity.class, bundle);
} else if (v.getId() == R.id.bt1) {
gotoActivity(ListLazyLoadingActivity2.class, bundle);
}
}
}
......@@ -159,6 +159,13 @@ public class ViewActviity extends BaseActivity {
gotoActivity(OnMultiClickActivity.class);
}
});
findViewById(R.id.b14).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gotoActivity(TestListLazyLoadingActivity.class);
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="@dimen/im_dp_5"
android:text="加载中。。。"
android:textColor="@color/black"
android:textSize="@dimen/txt_size_28px" />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<com.mayi.fastdevelop.view.lazyLoading.ListLazyLoadingView
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<com.scwang.smartrefresh.layout.SmartRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.mayi.fastdevelop.view.lazyLoading.ListLazyLoadingView
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转懒加载+下拉刷新控件展示页,"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转懒加载控件展示页"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/bt1" />
<EditText
android:id="@+id/et1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="每次加载个数"
android:inputType="number"
android:text="5"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/bt" />
<EditText
android:id="@+id/et2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="最多个数"
android:inputType="number"
android:text="50"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et1" />
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
......@@ -90,5 +90,11 @@
android:layout_height="wrap_content"
android:text="防止多次点击" />
<Button
android:id="@+id/b14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="懒加载" />
</LinearLayout>
</ScrollView>
\ No newline at end of file
......@@ -30,6 +30,10 @@ android {
dependencies {
api fileTree(include: ['*.jar'], dir: 'libs')
api 'androidx.constraintlayout:constraintlayout:1.1.3'
api 'androidx.recyclerview:recyclerview:1.0.0'
api 'com.yanzhenjie:permission:2.0.0-rc12'//权限申请
api 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.3'//下拉刷新,加载更多
api 'com.youth.banner:banner:1.4.10' //图片轮播控件
......@@ -39,10 +43,8 @@ dependencies {
api 'com.tencent.bugly:nativecrashreport:3.6.0.1'
api 'com.orhanobut:logger:2.2.0'
api 'com.alibaba:fastjson:1.2.12'
api 'androidx.recyclerview:recyclerview:1.0.0'
api files('libs/zixing-core-3.2.0.jar')
// api project(':call')
api 'com.gc:call:1.0.0'
api 'com.google.android.exoplayer:exoplayer:2.10.5'
}
}
\ No newline at end of file
package com.mayi.fastdevelop.view.lazyLoading;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import com.mayi.fastdevelop.R;
import java.util.List;
import static androidx.recyclerview.widget.RecyclerView.*;
public abstract class ListLazyLoadingAdapter<T extends ViewHolder> extends ListLazyLoadingView.Adapter {
public static final int LOADMORE_VIEWTYPE = -1;
private boolean isShowLoadMore = true;
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (viewType == LOADMORE_VIEWTYPE) {
return getLoadMoreVH(parent);
} else {
return getCreateViewHolder( parent,viewType);
}
}
//获取加载更多VH,子类可重现
public ViewHolder getLoadMoreVH(@NonNull ViewGroup parent) {
return new ViewHolder(LayoutInflater.from(parent.getContext()).
inflate(R.layout.item_list_lazy_loading_load_more, parent, false)){
};
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
if (position < getList().size()) {
onBindItemVH((T) holder, position);
}
}
@Override
public int getItemCount() {
if (getList() == null) {
return 0;
} else {
if (isShowLoadMore) {
return getList().size() + 1;
} else {
return getList().size();
}
}
}
public abstract void onBindItemVH(@NonNull T holder, int position);
public abstract List getList();
public abstract T getCreateViewHolder(@NonNull ViewGroup parent,int viewType);
@Override
public int getItemViewType(int position) {
if (isShowLoadMore && position + 1 == getItemCount()) {
return LOADMORE_VIEWTYPE;
}
return super.getItemViewType(position);
}
public boolean isShowLoadMore() {
return isShowLoadMore;
}
public void setShowLoadMore(boolean showLoadMore) {
isShowLoadMore = showLoadMore;
}
}
package com.mayi.fastdevelop.view.lazyLoading;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
/**
* 列表懒加载控件
*/
public class ListLazyLoadingView extends RecyclerView {
private LinearLayoutManager manager;
private boolean isLoadingAll = false;//是否加载全部
private int surplusItemCount = 5;
private ListLazyLoadingViewListener listLazyLoadingViewListener;
private boolean isLoading;//是否加载数据中
public ListLazyLoadingView(@NonNull Context context) {
super(context);
init();
}
public ListLazyLoadingView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public ListLazyLoadingView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private int lastAction;
private void init() {
manager = new LinearLayoutManager(getContext(), VERTICAL, false);
setLayoutManager(manager);
setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
lastAction = event.getAction();
return false;
}
});
addOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (!isLoadingAll && !isLoading) {
if (newState == SCROLL_STATE_IDLE && lastAction == MotionEvent.ACTION_UP) {
//获取最后一个完全显示的ItemPosition
int lastVisibleItem = manager.findLastCompletelyVisibleItemPosition();
int totalItemCount = manager.getItemCount();
if (totalItemCount - lastVisibleItem <= surplusItemCount) {
//加载更多功能的代码
if (listLazyLoadingViewListener != null) {
isLoading = true;
listLazyLoadingViewListener.onLoadMore();
}
}
}
}
}
});
}
public void setListLazyLoadingAdapter(@Nullable ListLazyLoadingAdapter adapter) {
setAdapter(adapter);
}
public boolean isLoadingAll() {
return isLoadingAll;
}
public void setLoadingAll(boolean loadingAll) {
isLoadingAll = loadingAll;
Adapter adapter = getAdapter();
if (adapter != null && adapter instanceof ListLazyLoadingAdapter) {
((ListLazyLoadingAdapter<ViewHolder>) adapter).setShowLoadMore(!loadingAll);
}
}
public void setListLazyLoadingViewListener(ListLazyLoadingViewListener listLazyLoadingViewListener) {
this.listLazyLoadingViewListener = listLazyLoadingViewListener;
}
public interface ListLazyLoadingViewListener {
public void onLoadMore();
}
//完成加载更多
public void finishLoadMore() {
isLoading = false;
}
}
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="@dimen/im_dp_5"
android:textSize="@dimen/txt_size_28px"
android:textColor="@color/black"
android:text="加载中。。。" />
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment