Commit 0f2a0b2f by gao.chao

基本列表页

parent 98a97219
apply plugin: 'com.android.application' apply plugin: 'com.android.application'
android { android {
compileSdkVersion 28 compileSdkVersion build_versions.compile_sdk
defaultConfig { defaultConfig {
applicationId "com.mayi.demo" applicationId "com.mayi.demo"
minSdkVersion 21 minSdkVersion build_versions.min_sdk
targetSdkVersion 28 targetSdkVersion build_versions.target_sdk
versionCode 1 versionCode 1
versionName "1.0" versionName "1.0"
// 2、启用Multidex // 2、启用Multidex
......
...@@ -47,9 +47,6 @@ ...@@ -47,9 +47,6 @@
android:theme="@style/AppThemeLaunch" /> android:theme="@style/AppThemeLaunch" />
<activity android:name=".page.SlidingRoofPlacementActivity" /> <activity android:name=".page.SlidingRoofPlacementActivity" />
<activity android:name=".view.ListLazyLoadingActivity2" />
<activity android:name=".view.ListLazyLoadingActivity" />
<activity android:name=".view.TestListLazyLoadingActivity" />
<activity android:name=".function.PlayerActivity" /> <activity android:name=".function.PlayerActivity" />
<activity android:name=".function.PlayerActivtiy2" /> <activity android:name=".function.PlayerActivtiy2" />
<activity android:name=".function.FunctionActivity" /> <activity android:name=".function.FunctionActivity" />
......
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);
}
}
}
...@@ -18,8 +18,8 @@ import com.gc.call.CallReturnListener; ...@@ -18,8 +18,8 @@ import com.gc.call.CallReturnListener;
import com.mayi.demo.R; import com.mayi.demo.R;
import com.mayi.demo.page.FragmentInteractiveActivity; import com.mayi.demo.page.FragmentInteractiveActivity;
import com.mayi.demo.page.FragmentInteractiveActivity2; import com.mayi.demo.page.FragmentInteractiveActivity2;
import com.mayi.demo.page.UserInfo;
import com.mayi.fastdevelop.base.BaseActivity; import com.mayi.fastdevelop.base.BaseActivity;
import com.mayi.fastdevelop.bean.UserInfo;
import com.mayi.fastdevelop.comnon.Constant; import com.mayi.fastdevelop.comnon.Constant;
import com.mayi.fastdevelop.comnon.RequestCode; import com.mayi.fastdevelop.comnon.RequestCode;
import com.mayi.fastdevelop.http.NetWorkBuilder; import com.mayi.fastdevelop.http.NetWorkBuilder;
......
package com.mayi.fastdevelop.commonpage; package com.mayi.demo.page;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
...@@ -8,7 +8,6 @@ import androidx.annotation.Nullable; ...@@ -8,7 +8,6 @@ import androidx.annotation.Nullable;
import com.mayi.fastdevelop.base.BaseActivity; import com.mayi.fastdevelop.base.BaseActivity;
import com.mayi.fastdevelop.comnon.Key; import com.mayi.fastdevelop.comnon.Key;
import com.mayi.fastdevelop.comnon.UserManager;
import com.mayi.fastdevelop.util.SpUtil; import com.mayi.fastdevelop.util.SpUtil;
......
package com.mayi.demo.page; package com.mayi.demo.page;
import com.mayi.demo.view.ViewActviity; import com.mayi.demo.view.ViewActviity;
import com.mayi.fastdevelop.commonpage.BaseStartActivity;
public class StartActivity extends BaseStartActivity<ViewActviity,ViewActviity,ViewActviity>{ public class StartActivity extends BaseStartActivity<ViewActviity,ViewActviity,ViewActviity>{
@Override @Override
......
package com.mayi.fastdevelop.bean; package com.mayi.demo.page;
import java.io.Serializable; import java.io.Serializable;
......
package com.mayi.fastdevelop.comnon; package com.mayi.demo.page;
import android.text.TextUtils; import android.text.TextUtils;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.mayi.fastdevelop.bean.UserInfo; import com.mayi.fastdevelop.comnon.Key;
import com.mayi.fastdevelop.util.SpUtil; import com.mayi.fastdevelop.util.SpUtil;
public class UserManager { public class UserManager {
......
package com.mayi.demo.view;
import android.os.Bundle;
import android.os.Handler;
import com.mayi.demo.MyListLazyLoadingAdapter;
import com.mayi.demo.R;
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.view;
import android.os.Bundle;
import android.os.Handler;
import com.mayi.demo.MyListLazyLoadingAdapter;
import com.mayi.demo.R;
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.view; package com.mayi.demo.view;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.NonNull; import com.mayi.demo.ItemBean;
import androidx.annotation.Nullable; import com.mayi.fastdevelop.base.BaseListActivity;
import androidx.recyclerview.widget.LinearLayoutManager; import com.mayi.fastdevelop.util.DateUtil;
import androidx.recyclerview.widget.RecyclerView;
import com.mayi.demo.R;
import com.mayi.fastdevelop.base.BaseActivity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
/** /**
* git 地址:https://github.com/scwang90/SmartRefreshLayout * git 地址:https://github.com/scwang90/SmartRefreshLayout
*/ */
public class SmartRefreshLayoutActivity extends BaseActivity { public class SmartRefreshLayoutActivity extends BaseListActivity<ItemBean, SmartRefreshLayoutVH> {
private int size = 50;
Handler handler;
int max = 80;
@Override @Override
protected void onCreate(@Nullable Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_smartrefreshlayout); title.setTextCenter("列表");
handler = new Handler(); }
final SmartRefreshLayout layout = findViewById(R.id.layout);
final RecyclerView list = findViewById(R.id.list);
list.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
list.setAdapter(new MyAdapter());
layout.setEnableRefresh(true); @Override
layout.setEnableLoadmore(true); protected SmartRefreshLayoutVH createVH(ViewGroup parent, int viewType) {
layout.setOnLoadmoreListener(new OnLoadmoreListener() { SmartRefreshLayoutVH vh = new SmartRefreshLayoutVH(new TextView(parent.getContext()));
vh.setLayoutVHListener(new SmartRefreshLayoutVH.SmartRefreshLayoutVHListener() {
@Override @Override
public void onLoadmore(RefreshLayout refreshlayout) { public void onItemClick(ItemBean itemBean) {
handler.postDelayed(new Runnable() { showToast(itemBean.getText());
@Override
public void run() {
size = size + 10;
list.getAdapter().notifyDataSetChanged();
layout.finishLoadmore();//完成加载
if (size > 100) {
layout.setLoadmoreFinished(true);//数据全部加载完成
}
}
}, 1000);
} }
}); });
layout.setOnRefreshListener(new OnRefreshListener() { return vh;
}
@Override
protected void onRefreshData() {
dismssLoadMsg();
stopLoadMoreData();
handler.postDelayed(new Runnable() {
@Override @Override
public void onRefresh(RefreshLayout refreshlayout) { public void run() {
handler.postDelayed(new Runnable() { list.clear();
@Override addData();
public void run() { startLoadMoreData();
size = 50; notifyData();
list.getAdapter().notifyDataSetChanged(); finishRefresh();
layout.finishRefresh();//完成刷新
}
}, 1000);
} }
}); }, 1000);
} }
class MyAdapter extends RecyclerView.Adapter<MyVH> { private void addData() {
for (int i = 0; i < 20; i++) {
@NonNull list.add(new ItemBean(list.size() + " " + DateUtil.getCurrentDate(), null));
@Override
public MyVH onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
return new MyVH(new TextView(viewGroup.getContext()));
}
@Override
public void onBindViewHolder(@NonNull MyVH viewHolder, int i) {
viewHolder.textView.setText("i=" + i);
}
@Override
public int getItemCount() {
return size;
} }
} }
class MyVH extends RecyclerView.ViewHolder { @Override
protected void loadMoreData(int pageNumber) {
TextView textView; handler.postDelayed(new Runnable() {
@Override
public MyVH(@NonNull View itemView) { public void run() {
super(itemView); addData();
textView = (TextView) itemView; notifyData();
} isLoadMore = false;
if (list.size() >= max) {
stopLoadMoreData();
showLoadMsg("数据加载完成");
} else {
dismssLoadMsg();
}
}
}, 1000);
} }
} }
package com.mayi.demo.view;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import com.mayi.demo.ItemBean;
import com.mayi.demo.R;
import com.mayi.fastdevelop.base.BaseViewHolder;
import com.mayi.fastdevelop.view.OnMultiClickListener;
public class SmartRefreshLayoutVH extends BaseViewHolder<ItemBean> {
protected TextView tv;
private SmartRefreshLayoutVHListener layoutVHListener;
public SmartRefreshLayoutVH(@NonNull View itemView) {
super(itemView);
tv = (TextView) itemView;
tv.setTextSize(itemView.getContext().getResources().getDimensionPixelSize(R.dimen.text_size_20));
}
public void setLayoutVHListener(SmartRefreshLayoutVHListener layoutVHListener) {
this.layoutVHListener = layoutVHListener;
}
@Override
public void onBindView(ItemBean itemBean) {
tv.setText(itemBean.getText());
tv.setOnClickListener(new OnMultiClickListener() {
@Override
public void onMultiClick(View v) {
if (layoutVHListener != null) {
layoutVHListener.onItemClick(itemBean);
}
}
});
}
public interface SmartRefreshLayoutVHListener {
public void onItemClick(ItemBean itemBean);
}
}
package com.mayi.demo.view;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import com.mayi.demo.R;
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);
}
}
}
...@@ -157,12 +157,6 @@ public class ViewActviity extends BaseActivity { ...@@ -157,12 +157,6 @@ public class ViewActviity extends BaseActivity {
gotoActivity(OnMultiClickActivity.class); gotoActivity(OnMultiClickActivity.class);
} }
}); });
findViewById(R.id.b14).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gotoActivity(TestListLazyLoadingActivity.class);
}
});
findViewById(R.id.b15).setOnClickListener(new View.OnClickListener() { findViewById(R.id.b15).setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
......
...@@ -8,8 +8,8 @@ ...@@ -8,8 +8,8 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:padding="@dimen/size_10" android:padding="@dimen/size_10"
android:textColor="@color/color_666666" android:text="流式布局"
android:text="流式布局"/> android:textColor="@color/color_666666" />
<com.google.android.material.internal.FlowLayout <com.google.android.material.internal.FlowLayout
...@@ -90,7 +90,7 @@ ...@@ -90,7 +90,7 @@
android:id="@+id/b12" android:id="@+id/b12"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="下拉刷新,加载更多" /> android:text="下拉刷新,懒加载" />
<Button <Button
android:id="@+id/b13" android:id="@+id/b13"
...@@ -99,12 +99,6 @@ ...@@ -99,12 +99,6 @@
android:text="防止多次点击" /> android:text="防止多次点击" />
<Button <Button
android:id="@+id/b14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="懒加载" />
<Button
android:id="@+id/b15" android:id="@+id/b15"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
......
// Top-level build file where you can add configuration options common to all sub-projects/modules. // Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript { buildscript {
apply from: 'versions.gradle'
ext.kotlin_version = '1.3.61' ext.kotlin_version = '1.3.61'
repositories { repositories {
google() google()
jcenter() jcenter()
...@@ -29,3 +29,4 @@ allprojects { ...@@ -29,3 +29,4 @@ allprojects {
task clean(type: Delete) { task clean(type: Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
def min = 23
apply plugin: 'com.android.library' apply plugin: 'com.android.library'
android { android {
compileSdkVersion 28 compileSdkVersion build_versions.compile_sdk
defaultConfig { defaultConfig {
minSdkVersion 21 minSdkVersion build_versions.min_sdk
targetSdkVersion 28 targetSdkVersion build_versions.target_sdk
versionCode 3 versionCode 3
versionName "1.2" versionName "1.2"
ndk { ndk {
...@@ -48,7 +48,6 @@ dependencies { ...@@ -48,7 +48,6 @@ dependencies {
api 'com.orhanobut:logger:2.2.0' api 'com.orhanobut:logger:2.2.0'
api 'com.alibaba:fastjson:1.2.12' api 'com.alibaba:fastjson:1.2.12'
api files('libs/zixing-core-3.2.0.jar') api files('libs/zixing-core-3.2.0.jar')
// api project(':call')
api 'com.gc:call:1.0.0' api 'com.gc:call:1.0.0'
api 'com.google.android.exoplayer:exoplayer:2.10.5' api 'com.google.android.exoplayer:exoplayer:2.10.5'
api 'com.tencent:mmkv:1.0.23' api 'com.tencent:mmkv:1.0.23'
......
package com.mayi.fastdevelop.base;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.mayi.fastdevelop.R;
import com.mayi.fastdevelop.view.CustomTitleBar;
import com.mayi.fastdevelop.view.OnMultiClickListener;
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;
//base列表页
public abstract class BaseListActivity<T, VH extends BaseViewHolder<T>> extends BaseActivity {
protected TextView loadMsg;
protected CustomTitleBar title;
protected RecyclerView recyclerView;
protected SmartRefreshLayout refreshLayout;
protected Handler handler;
protected int pageNumber = 1;//页码
protected boolean isLoadMore;//是否正在加载更多
protected LinearLayoutManager manager;
protected long loadMoreInterval = 1000;
protected List<T> list = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.base_list_activity);
handler = new Handler();
loadMsg = findViewById(R.id.tv_load_msg);
title = findViewById(R.id.title);
recyclerView = findViewById(R.id.recycler_view);
refreshLayout = findViewById(R.id.refresh_layout);
manager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
recyclerView.setLayoutManager(manager);
recyclerView.setAdapter(new RecyclerView.Adapter<VH>() {
@NonNull
@Override
public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return createVH(parent, viewType);
}
@Override
public void onBindViewHolder(@NonNull VH holder, int position) {
holder.onBindView(list.get(position));
}
@Override
public int getItemViewType(int position) {
return getItemType(list.get(position));
}
@Override
public int getItemCount() {
return list.size();
}
});
refreshLayout.setEnableRefresh(true);
refreshLayout.setEnableLoadmore(false);
refreshLayout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(RefreshLayout refreshlayout) {
onRefreshData();
}
});
title.setOnClickLeftViewListener(new OnMultiClickListener() {
@Override
public void onMultiClick(View v) {
finish();
}
});
startLoadMoreData();
}
public void showLoadMsg(String msg) {
loadMsg.setVisibility(View.VISIBLE);
loadMsg.setText(msg);
}
public void dismssLoadMsg() {
if (loadMsg.getVisibility() == View.VISIBLE) {
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
alphaAnimation.setDuration(2000);
alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
loadMsg.setVisibility(View.GONE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
loadMsg.startAnimation(alphaAnimation);
}
}
//创建VH
protected abstract VH createVH(ViewGroup parent, int viewType);
//刷新数据
public void notifyData() {
recyclerView.getAdapter().notifyDataSetChanged();
}
//完成刷新
public void finishRefresh() {
refreshLayout.finishRefresh();
}
public void startLoadMoreData() {
stopLoadMoreData();
handler.post(new Runnable() {
@Override
public void run() {
Log.i("11111", "loading");
if (!isLoadMore && recyclerView.getScrollState() == RecyclerView.SCROLL_STATE_IDLE) {
if (manager.findLastVisibleItemPosition() + 5 > recyclerView.getAdapter().getItemCount()) {
isLoadMore = true;
showLoadMsg("加载更多数据中。。。");
loadMoreData(pageNumber);
}
if (manager.findLastVisibleItemPosition() == recyclerView.getAdapter().getItemCount()) {
} else {
}
}
handler.postDelayed(this, loadMoreInterval);
}
});
}
public void stopLoadMoreData() {
handler.removeCallbacksAndMessages(null);
}
//根据item获取type,默认不需要重写
public int getItemType(T t) {
return 0;
}
//刷新数据
protected abstract void onRefreshData();
//根据页码加载更多数据
protected abstract void loadMoreData(int pageNumber);
@Override
protected void onDestroy() {
stopLoadMoreData();
super.onDestroy();
}
}
package com.mayi.fastdevelop.base;
import android.os.Bundle;
import androidx.lifecycle.ViewModel;
import java.lang.reflect.ParameterizedType;
public class BaseVMActivity<T extends ViewModel> extends BaseActivity{
protected T viewmodel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
viewmodel= getDefaultViewModelProviderFactory().create
((Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]);
}
}
package com.mayi.fastdevelop.base;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public abstract class BaseViewHolder<T> extends RecyclerView.ViewHolder {
public BaseViewHolder(@NonNull View itemView) {
super(itemView);
}
public abstract void onBindView(T t);
}
package com.mayi.fastdevelop.bean;
import java.io.Serializable;
/**
* 首页表格item
*/
public class HomeItem implements Serializable {
private String name; //item名字
private String img; //item图片
private String goPage; //点击跳转页面
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
public String getGoPage() {
return goPage;
}
public void setGoPage(String goPage) {
this.goPage = goPage;
}
}
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"?>
<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"
android:orientation="vertical">
<com.mayi.fastdevelop.view.CustomTitleBar
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="@dimen/size_50"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/refresh_layout"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
<TextView
android:id="@+id/tv_load_msg"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@color/alpha_40_white"
android:gravity="center"
android:padding="@dimen/size_10"
android:text="全部加载完成"
android:textColor="@color/black"
android:textSize="@dimen/text_size_24"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
...@@ -2,12 +2,11 @@ apply plugin: 'com.android.application' ...@@ -2,12 +2,11 @@ apply plugin: 'com.android.application'
apply plugin: 'kotlin-android' apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-android-extensions'
android { android {
compileSdkVersion 28 compileSdkVersion build_versions.compile_sdk
defaultConfig { defaultConfig {
minSdkVersion 23 minSdkVersion build_versions.min_sdk
targetSdkVersion 28 targetSdkVersion build_versions.target_sdk
versionCode 1 versionCode 1
versionName "1.0" versionName "1.0"
......
...@@ -6,11 +6,12 @@ import androidx.lifecycle.Observer ...@@ -6,11 +6,12 @@ import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.mayi.fastdevelop.base.BaseActivity import com.mayi.fastdevelop.base.BaseActivity
import com.mayi.fastdevelop.base.BaseVMActivity
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : BaseActivity() { class MainActivity : BaseVMActivity<MainViewModel>() {
private val viewmodel: MainViewModel by viewModels<MainViewModel>() // private val viewmodel: MainViewModel by viewModels<MainViewModel>()
private var itemAdapter: ItemAdapter? = null private var itemAdapter: ItemAdapter? = null
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
......
apply plugin: 'com.android.library' apply plugin: 'com.android.library'
android { android {
compileSdkVersion 28 compileSdkVersion build_versions.compile_sdk
defaultConfig { defaultConfig {
minSdkVersion 21 minSdkVersion build_versions.min_sdk
targetSdkVersion 28 targetSdkVersion build_versions.target_sdk
versionCode 1 versionCode 1
versionName "1.0" versionName "1.0"
} }
......
def build_versions = [:]
build_versions.min_sdk = 23
build_versions.compile_sdk = 28
build_versions.target_sdk = 28
ext.build_versions = build_versions
\ No newline at end of file
apply plugin: 'com.android.library' apply plugin: 'com.android.library'
android { android {
compileSdkVersion 28 compileSdkVersion build_versions.compile_sdk
defaultConfig { defaultConfig {
minSdkVersion 21 minSdkVersion build_versions.min_sdk
targetSdkVersion 28 targetSdkVersion build_versions.target_sdk
versionCode 2 versionCode 2
versionName "1.1" versionName "1.1"
} }
......
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