首先XRecyclerView的依赖
implementation 'com.jcodecraeer:xrecyclerview:1.5.9' //XRecyclerView的依赖
implementation 'com.android.support:recyclerview-v7:28.0.0' //recyclerView的依赖
implementation 'com.github.bumptech.glide:glide:4.8.0' //glide图片加载的依赖
implementation 'com.google.code.gson:gson:2.8.5' //gson解析的依赖
网络权限
<uses-permission android:name="android.permission.INTERNET"/>
Activity
public class MainActivity extends AppCompatActivity implements XRecyclerView.LoadingListener,IBaseView<List<User>>{
EditText editText;
ToggleButton togg_qh;
Button button;
RadioGroup radioGroup;
XRecyclerView xRecyclerView;
GoodsListAdapter goodsListAdapter;
GoodsListPresenter goodsListPresenter = new GoodsListPresenter(this);
LinearLayoutManager linearLayoutManager;
GridLayoutManager gridLayoutManager;
String name = "手机";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//寻找控件
editText = findViewById(R.id.shop_name);
button = findViewById(R.id.toggle);
togg_qh = findViewById(R.id.toggle_qh);
radioGroup = findViewById(R.id.radioGroup);
xRecyclerView = findViewById(R.id.xrv);
//创建布局
gridLayoutManager = new GridLayoutManager(MainActivity.this, 2, GridLayoutManager.VERTICAL, false);
linearLayoutManager = new LinearLayoutManager(MainActivity.this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//先设置线性布局
xRecyclerView.setLayoutManager(linearLayoutManager);
xRecyclerView.setLoadingListener(this);
//创建适配器
goodsListAdapter = new GoodsListAdapter(MainActivity.this);
//添加适配器
xRecyclerView.setAdapter(goodsListAdapter);
//加载数据
xRecyclerView.refresh();
//点击事件
togg_qh.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
//如果选中,设置瀑布流
xRecyclerView.setLayoutManager(gridLayoutManager);
goodsListAdapter.setViewType(GoodsListAdapter.TYPE_STAGG_IMAGE);
} else {
//否则线性布局
xRecyclerView.setLayoutManager(linearLayoutManager);
goodsListAdapter.setViewType(GoodsListAdapter.TYPE_LINLAY_IMAGE);
}
//刷新布局
goodsListAdapter.notifyDataSetChanged();
}
});
//搜索点击事件
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取输入框中的数据
name = editText.getText().toString().trim();
//重新向接口中传入关键字name
goodsListPresenter.request(true, name);
//刷新
goodsListAdapter.notifyDataSetChanged();
}
});
}
@Override
public void onRefresh() {
goodsListPresenter.request(true,name);
}
@Override
public void onLoadMore() {
goodsListPresenter.request(false,name);
}
public void success(List<User> data) {
xRecyclerView.refreshComplete();
xRecyclerView.loadMoreComplete();
if (goodsListPresenter.isResresh()) {
//上拉刷新清空数据
goodsListAdapter.clearList();
}
//下拉加载添加数据
goodsListAdapter.addAll(data);
goodsListAdapter.notifyDataSetChanged();
}
@Override
public void fial(Result result) {
Toast.makeText(this, ""+result.getMsg(), Toast.LENGTH_SHORT).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
//解除绑定,防止内存泄漏
goodsListPresenter.unBindCall();
}
}
View层
public interface IBaseView<T> {
void success(T data);
void fial(Result result);
}
BasePresenter抽象类
public abstract class BasePresenter {
IBaseView iBaseView;
public BasePresenter(IBaseView iBaseView) {
this.iBaseView = iBaseView;
}
private Handler mHandle = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Result result= (Result) msg.obj;
iBaseView.success(result.getData());
}
};
public void request(final Object...args){
new Thread(new Runnable() {
@Override
public void run() {
Message message = mHandle.obtainMessage();
message.obj = getData(args);
mHandle.sendMessage(message);
}
}).start();
};
protected abstract Object getData(Object...args);
//解除绑定的方法
public void unBindCall(){
this.iBaseView = null;
}
}
Presenter层
public class GoodsListPresenter extends BasePresenter {
private boolean isRefresh = true;
private int page = 1;
public GoodsListPresenter(IBaseView iBaseView) {
super(iBaseView);
}
@Override
protected Object getData(Object... args) {
isRefresh = (boolean) args[0];//是否需要刷新
if (isRefresh) {
page = 1;
} else {
page++;
}
Result result = GoodsListModel.goodsList(page + "", args[1] + "");
return result;
}
public boolean isResresh() {
return isRefresh;
}
}
Model
public class GoodsListModel {
public static Result goodsList(String page, String name) {
String resultString = Utils.get("http://www.zhaoapi.cn/product/searchProducts?keywords=" + name + "&page=" + page);
Gson gson = new Gson();
Type type = new TypeToken<Result<List<User>>>(){}.getType();
Result result = gson.fromJson(resultString, type);
return result;
}
}
适配器Adapter
public class GoodsListAdapter extends RecyclerView.Adapter<GoodsListAdapter.GoodHolderView> {
public static final int TYPE_LINLAY_IMAGE = 0; //线性布局
public static final int TYPE_STAGG_IMAGE = 1; //网格布局
private int viewType = TYPE_LINLAY_IMAGE; //第一次加载线性布局
private List<User> mList = new ArrayList<>();
private Context context;
public GoodsListAdapter(Context context) {
this.context = context;
}
@Override
public int getItemViewType(int position) {
//向onCreateViewHolder返回布局类型
return viewType;
}
public void setViewType(int viewType) {
//将返回的布局类型设置给viewType
this.viewType = viewType;
}
@NonNull
@Override
public GoodHolderView onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
View view = null;
//判断返回的布局类型
if (viewType == TYPE_LINLAY_IMAGE) {
//获取线性布局
view = View.inflate(context, R.layout.activity_shop_lin, null);
} else {
//获取网格布局
view = View.inflate(context, R.layout.activity_shop_glid, null);
}
return new GoodHolderView(view);
}
@Override
public void onBindViewHolder(@NonNull GoodHolderView holder, final int i) {
holder.textView_name.setText(mList.get(i).getTitle());
holder.textView_money.setText("¥" + mList.get(i).getPrice());
//由于我们的数据图片提供的不标准,所以我们需要切割得到图片
String[] split = mList.get(i).getImages().split("\\|");
if (split.length > 0) {
String replace = split[0].replace("https", "http");
Glide.with(context).load(replace).into(holder.imageView);
}
}
public int getItemCount() {
return mList.size();
}
public void clearList() {
//刷新数据
mList.clear();
}
public void addAll(List<User> data) {
//添加数据
mList.addAll(data);
}
class GoodHolderView extends RecyclerView.ViewHolder {
ImageView imageView;
TextView textView_name, textView_money;
public GoodHolderView(@NonNull View itemView) {
super(itemView);
textView_name = itemView.findViewById(R.id.text_name);
textView_money = itemView.findViewById(R.id.text_price);
imageView = itemView.findViewById(R.id.image_shop);
}
}
}
布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
tools:context=".activity.MainActivity">
<EditText
android:id="@+id/shop_name"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:hint="搜索"
android:layout_marginTop="5dp"
android:layout_marginLeft="30dp"
android:padding="5dp"
android:background="@drawable/sele_edi"/>
<Button
android:id="@+id/toggle"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="290dp"
android:layout_marginTop="5dp"
android:background="@drawable/search" />
<ToggleButton
android:id="@+id/toggle_qh"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:layout_alignParentRight="true"
android:background="@drawable/classification"
android:textOff=""
android:textOn=""/>
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/shop_name"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/comprehensive"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="综合"
android:checked="true"
android:button="@null"
android:layout_weight="1"/>
<RadioButton
android:id="@+id/sales"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="销量"
android:button="@null"
android:layout_weight="1"/>
<RadioButton
android:id="@+id/price"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="价格"
android:button="@null"
android:layout_weight="1"/>
<RadioButton
android:id="@+id/screening"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="筛选"
android:button="@null"
android:layout_weight="1"/>
</RadioGroup>
<com.jcodecraeer.xrecyclerview.XRecyclerView
android:id="@+id/xrv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/radioGroup"
android:layout_marginTop="10dp"/>
</RelativeLayout>
线性布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.MainActivity">
<ImageView
android:id="@+id/image_shop"
android:layout_margin="10dp"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:minHeight="50dp"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/text_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="aa"
android:layout_toRightOf="@+id/image_shop"
android:padding="10dp"/>
<TextView
android:id="@+id/text_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="aa"
android:textColor="#f00"
android:layout_marginTop="20dp"
android:layout_below="@+id/text_name"
android:layout_toRightOf="@+id/image_shop"
android:padding="10dp"/>
</RelativeLayout>
网格布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.MainActivity">
<ImageView
android:id="@+id/image_shop"
android:layout_margin="10dp"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:minHeight="50dp"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/text_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="aa"
android:layout_below="@+id/image_shop"
android:padding="10dp"/>
<TextView
android:id="@+id/text_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="aa"
android:textColor="#f00"
android:layout_marginTop="10dp"
android:layout_below="@+id/text_name"
android:padding="10dp"/>
</RelativeLayout>
效果图