ListLayout
package com.coral3.common_module.components;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.coral3.common_module.R;
import com.coral3.common_module.adapter.RvAdapter;
import com.coral3.common_module.model.ListModel;
import com.coral3.common_module.utils.LogUtil;
import com.coral3.common_module.utils.ToastUtil;
import java.util.ArrayList;
import java.util.List;
public class ListLayout extends LinearLayout implements BaseQuickAdapter.RequestLoadMoreListener{
private Context mContext;
private View view;
private RecyclerView rv;
private com.coral3.common_module.adapter.RvAdapter RvAdapter;
private List<ListModel> data = new ArrayList<>();
public ListLayout(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
mContext = context;
initView();
initListener();
getData(true);
}
private void initView() {
view = LayoutInflater.from(mContext).inflate(R.layout.layout_list, this);
initRv();
}
private void initListener(){
RvAdapter.setOnLoadMoreListener(this, rv);
RvAdapter.setOnItemClickListener((adapter, view, position) -> {
ToastUtil.showMsg(mContext, "点击了" + data.get(position).getName());
});
}
private void initRv() {
rv = view.findViewById(R.id.recyclerview);
rv.setLayoutManager(new LinearLayoutManager(mContext));
RvAdapter = new RvAdapter(R.layout.layout_recyclerview_item, data);
rv.setAdapter(RvAdapter);
}
@Override
public void onLoadMoreRequested() {
LogUtil.d("加载更多...");
getData(false);
}
private void getData(Boolean isFirstLoad){
if(isFirstLoad){
data.clear();
data.add(new ListModel(R.mipmap.avatar, "l西y瓜slyslyslyslyslys", "这是很久很久很久很久很久很久的故事..."));
data.add(new ListModel(R.mipmap.avatar, "s西y瓜l", "这是很久很久很久很久很久很久的故事..."));
data.add(new ListModel(R.mipmap.avatar, "w西t瓜y", "这是很久很久很久很久很久很久的故事..."));
data.add(new ListModel(R.mipmap.avatar, "y西t瓜w", "这是很久很久很久很久很久很久的故事..."));
data.add(new ListModel(R.mipmap.avatar, "s西y瓜l", "这是很久很久很久很久很久很久的故事..."));
data.add(new ListModel(R.mipmap.avatar, "s西y瓜l", "这是很久很久很久很久很久很久的故事..."));
data.add(new ListModel(R.mipmap.avatar, "s西y瓜l", "这是很久很久很久很久很久很久的故事..."));
data.add(new ListModel(R.mipmap.avatar, "s西y瓜l", "这是很久很久很久很久很久很久的故事..."));
data.add(new ListModel(R.mipmap.avatar, "s西y瓜l", "这是很久很久很久很久很久很久的故事..."));
data.add(new ListModel(R.mipmap.avatar, "s西y瓜l", "这是很久很久很久很久很久很久的故事..."));
data.add(new ListModel(R.mipmap.avatar, "s西y瓜l", "这是很久很久很久很久很久很久的故事..."));
data.add(new ListModel(R.mipmap.avatar, "s西y瓜l", "这是很久很久很久很久很久很久的故事..."));
data.add(new ListModel(R.mipmap.avatar, "s西y瓜l", "这是很久很久很久很久很久很久的故事..."));
data.add(new ListModel(R.mipmap.avatar, "s西y瓜l", "这是很久很久很久很久很久很久的故事..."));
RvAdapter.notifyDataSetChanged();
RvAdapter.loadMoreComplete();
}else{
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
((Activity)mContext).runOnUiThread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 6; i++) {
data.add(new ListModel(R.mipmap.avatar, "l西y瓜s", "这是很久很久很久很久很久很久的故事..."));
}
RvAdapter.notifyDataSetChanged();
if(data.size() > 60) {
LogUtil.d("加载结束...");
RvAdapter.loadMoreEnd();
}else{
RvAdapter.loadMoreComplete();
}
}
});
}
}).start();
}
}
}
layout_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include layout="@layout/layout_recyclerview"/>
</LinearLayout>
</LinearLayout>
RvAdapter
package com.coral3.common_module.adapter;
import androidx.annotation.Nullable;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.loadmore.LoadMoreView;
import com.coral3.common_module.R;
import com.coral3.common_module.model.ListModel;
import java.util.List;
public class RvAdapter extends BaseQuickAdapter<ListModel, BaseViewHolder> {
public RvAdapter(int layoutResId, @Nullable List<ListModel> data) {
super(layoutResId, data);
this.setLoadMoreView(new LoadMoreView() {
@Override
public int getLayoutId() {
return R.layout.layout_custom_bottom_loading;
}
@Override
protected int getLoadingViewId() {
return R.id.tv_loading;
}
@Override
protected int getLoadFailViewId() {
return R.id.tv_loading_err;
}
@Override
protected int getLoadEndViewId() {
return R.id.tv_loading_end;
}
});
}
@Override
protected void convert(BaseViewHolder helper, ListModel item) {
helper.setImageResource(R.id.iv_img_view, item.getImageId());
helper.setText(R.id.tv_base_name, item.getName());
helper.setText(R.id.tv_base_content, item.getContent());
}
}
ListModel
package com.coral3.common_module.model;
import java.io.Serializable;
public class ListModel implements Serializable {
private int imageId;
private String name;
private String content;
public String getName() {
return name;
}
public int getImageId() {
return imageId;
}
public String getContent() {
return content;
}
public ListModel(int imageId, String title, String content){
this.imageId = imageId;
this.name = title;
this.content = content;
}
}
使用
<com.coral3.common_module.components.ListLayout
android:id="@+id/work_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
|