用到的依赖
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.android.support:recyclerview-v7:28.0.0-alpha3'
implementation('com.jcodecraeer:xrecyclerview:1.5.9') {
exclude group: 'com.android.support'
}
implementation files('libs/universal-image-loader-1.9.5.jar')
封装OK
public class OK {
private static OK ok;
private static OkHttpClient client;
private static Handler handler;
protected static Handler getHandler() {
return handler;
}
private OK() {
client = new OkHttpClient.Builder()
//TODO 拦截器,打印日志
.build();
handler = new Handler();
}
/**
* 获得单例
*
* @return
*/
public static OK getOk() {
if (ok == null) {
synchronized (OK.class) {
if (ok == null) {
ok = new OK();
}
}
}
return ok;
}
/**
* GET方法
*/
public void doGet(String url, Callback callback) {
//组装请求
Request request = new Request.Builder()
.url(url)
.build();
//初始化调用类
Call call = client.newCall(request);
//加入请求队列,回调结果
call.enqueue(callback);
}
/**
* POST方法
*/
public void doPost() {
}
}
封装的OkCallback
public abstract class OkCallback implements Callback{
public abstract void onUI(String json);//更新UI
public abstract void onFailed(String json);//返回错误信息
@Override
public void onFailure(Call call,final IOException e) {
OK.getHandler().post(new Runnable() {
@Override
public void run() {
onFailed(e.getMessage());
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String json=response.body().string();//注意
OK.getHandler().post(new Runnable() {
@Override
public void run() {
onUI(json);
}
});
}
}
1.Model
1》bean类
public class Shop {
public double bargainPrice;
public String createtime;
public String detailUrl;
public String images;
public int itemtype;
public int pid;
public String price;
public int pscid;
public int salenum;
public int sellerid;
public String subhead;
public String title;
}
2》Data类
public class ShopData {
public int stutas;
public String message;
public List<Shop> data;
}
3》ITask类接口 (定义请求数据接口)
public interface ITask {
public void getShopList(String url, OkCallback callback);
}
4》Task实现ITask方法
public class Task implements ITask{
@Override
public void getShopList(String url, OkCallback callback) {
OK.getOk().doGet(url,callback);
}
}
2。Presenter层
1》基类
public interface BasePre {
void onDestroy();
}
2》p层的接口
public interface IPresenter extends BasePre{
public void getData(String key,int page);
}
3》创建P层类实现P层的接口
public class MyPresenter implements IPresenter {
ITask task;
IView view;
public MyPresenter(SecondActivity activity){
task=new Task();
view=activity;
}
@Override
public void getData(String key,int page) {
//拼接字符串
String keyword = URLEncoder.encode(key);//记得中文编码
String url = "http://www.zhaoapi.cn/product/searchProducts?keywords=" + keyword + "&page=" + page + "&sort=0";
task.getShopList(url, new OkCallback() {
@Override
public void onUI(String json) {
Gson gson=new Gson();
ShopData shopData = gson.fromJson(json, ShopData.class);
view.showData(shopData.data);
}
@Override
public void onFailed(String json) {
}
});
}
@Override
public void onDestroy() {
//销毁视图
view=null;
}
}
3.View层
1》
public interface IView {
void showData(List<Shop> shops);
}
2》适配器
public class ShopAdapter extends RecyclerView.Adapter<ShopAdapter.ShopHolder> {
private Context context;
private List<Shop> list;
private OnItemClickListener onItemClickListener;
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
public ShopAdapter(Context context) {
this.context = context;
list=new ArrayList<>();
}
public void clearData(){
this.list.clear();
}
@NonNull
@Override
public ShopHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view= LayoutInflater.from(context).inflate(R.layout.layout_recycler,viewGroup,false);
return new ShopHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ShopHolder shopHolder, int i) {
final Shop shop=list.get(i);
String imgUrl= shop.images.split("\\|")[0];
ImageLoader.getInstance().displayImage(imgUrl,shopHolder.shop_img, App.getOptions());
shopHolder.shop_name.setText(shop.title);
shopHolder.shop_price.setText(shop.price+"");
shopHolder.shop_createtime.setText(shop.createtime);
shopHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (onItemClickListener!=null){
onItemClickListener.ItemClickListener(shop);
}
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public void addData(List<Shop> shops) {
this.list.addAll(shops);
notifyDataSetChanged();
}
class ShopHolder extends RecyclerView.ViewHolder{
ImageView shop_img;
TextView shop_name,shop_price,shop_createtime;
public ShopHolder(@NonNull View itemView) {
super(itemView);
shop_img=itemView.findViewById(R.id.shop_img);
shop_name=itemView.findViewById(R.id.shop_name);
shop_price=itemView.findViewById(R.id.shop_price);
shop_createtime=itemView.findViewById(R.id.shop_createtime);
}
}
public interface OnItemClickListener{
void ItemClickListener(Shop shop);
}
}
4.xml
主的xml
<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=".SecondActivity">
<ImageView
android:id="@+id/image_fan"
android:layout_width="40dp"
android:layout_height="35dp"
android:src="@drawable/fanhui"
android:layout_marginTop="8dp"
/>
<EditText
android:id="@+id/editText2"
android:layout_width="330dp"
android:layout_height="30dp"
android:layout_alignTop="@+id/image_fan"
android:layout_toEndOf="@+id/image_fan"
android:background="@drawable/shape"
android:hint=" 搜索" />
<ImageView
android:id="@+id/image2"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignLeft="@+id/editText2"
android:layout_alignTop="@+id/editText2"
android:layout_alignBottom="@+id/editText2"
android:src="@drawable/sousuo" />
<ImageView
android:id="@+id/image_que"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentEnd="true"
android:layout_marginTop="4dp"
android:src="@drawable/qiehuan" />
<com.jcodecraeer.xrecyclerview.XRecyclerView
android:id="@+id/xrecyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/editText2"
android:layout_marginTop="5dp"
></com.jcodecraeer.xrecyclerview.XRecyclerView>
</RelativeLayout>
展示条目的xml
<ImageView
android:id="@+id/shop_img"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="12dp"
android:layout_marginTop="11dp"
android:src="@drawable/ic_launcher_background" />
<TextView
android:id="@+id/shop_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignTop="@+id/shop_img"
android:layout_marginStart="123dp"
android:hint="2"
android:text="TextView"
android:textColor="#000"
android:textSize="18dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/shop_createtime"
android:layout_alignStart="@+id/shop_name"
android:text="¥:"
android:textColor="#FF0000"
android:textSize="18dp" />
<TextView
android:id="@+id/shop_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView2"
android:layout_centerHorizontal="true"
android:text="TextView"
android:textColor="#FF0000"
android:textSize="18dp" />
<TextView
android:id="@+id/shop_createtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/shop_img"
android:layout_alignStart="@+id/shop_name"
android:text="TextView" />
5.activity
public class SecondActivity extends AppCompatActivity implements IView{
IPresenter presenter;
XRecyclerView xRecyclerView;
ShopAdapter shopAdapter;
private int page=0;
private String key;
EditText editText2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
xRecyclerView=findViewById(R.id.xrecyclerview);
editText2=findViewById(R.id.editText2);
//获取传值
key = getIntent().getStringExtra("key");
editText2.setText(key);
//初始化presenter
presenter=new MyPresenter(this);
presenter.getData(key,page);
xRecyclerView.setLayoutManager(new LinearLayoutManager(this));
xRecyclerView.addItemDecoration(new DividerItemDecoration(this, OrientationHelper.VERTICAL));
shopAdapter=new ShopAdapter(this);
xRecyclerView.setAdapter(shopAdapter);
shopAdapter.setOnItemClickListener(new ShopAdapter.OnItemClickListener() {
@Override
public void ItemClickListener(Shop shop) {
Intent it=new Intent(SecondActivity.this,InfoActivity.class);
it.putExtra("pid",shop.pid);
it.putExtra("title",shop.title);
it.putExtra("price",shop.price);
it.putExtra("img",shop.images);
startActivity(it);
}
});
xRecyclerView.setPullRefreshEnabled(true);
xRecyclerView.setLoadingMoreEnabled(true);
xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
@Override
public void onRefresh() {
page = 1;
shopAdapter.clearData();
presenter.getData(key,page);
}
@Override
public void onLoadMore() {
page++;
presenter.getData(key,page);
}
});
}
@Override
public void showData(List<Shop> shops) {
shopAdapter.addData(shops);
xRecyclerView.loadMoreComplete();
xRecyclerView.refreshComplete();
}
@Override
public void showCart(List<Seller> sellers) {
}
}
6.点击条目跳到详情页面
依赖
implementation 'com.youth.banner:banner:1.4.10' //最新版本 //导入Glide版本记得使用此版本 implementation 'com.github.bumptech.glide:glide:3.8.0'
1.xml
<com.youth.banner.Banner android:layout_margin="8dp" android:id="@+id/banner" android:layout_width="match_parent" android:layout_height="300dp"></com.youth.banner.Banner> <TextView android:id="@+id/text_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentStart="true" android:layout_marginBottom="210dp" android:layout_marginStart="15dp" android:text="TextView" android:textColor="#000" android:textSize="20dp" tools:layout_editor_absoluteX="29dp" tools:layout_editor_absoluteY="284dp" /> <Button android:id="@+id/shoppingcar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentEnd="true" android:layout_marginBottom="12dp" android:layout_marginEnd="16dp" android:background="#FF0000" android:text="加入购物车" android:textSize="16dp" tools:layout_editor_absoluteX="293dp" tools:layout_editor_absoluteY="514dp" /> <TextView android:id="@+id/text_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignStart="@+id/text_title" android:layout_marginBottom="171dp" android:text="TextView" android:textSize="18dp" android:textColor="#FF0000" />
2.activity
public class InfoActivity extends AppCompatActivity { Button shoppingcar; TextView text_title,text_price; Banner banner; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_info); text_title=findViewById(R.id.text_title); text_price=findViewById(R.id.text_price); shoppingcar=findViewById(R.id.shoppingcar); banner=findViewById(R.id.banner); String title = getIntent().getStringExtra("title"); String price = getIntent().getStringExtra("price"); text_title.setText(title); text_price.setText(price); shoppingcar.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent it=new Intent(InfoActivity.this,ShoppingCarActivity.class); startActivity(it); } }); String img = getIntent().getStringExtra("img"); List<String> strings = Arrays.asList(img.split("\\|")); banner.setBannerStyle(BannerConfig.CENTER); banner.setImageLoader(new MyLoader()); banner.setImages(strings); banner.setDelayTime(1000); banner.isAutoPlay(true); banner.setIndicatorGravity(BannerConfig.CENTER); banner.start(); } private class MyLoader extends ImageLoader { public void displayImage(Context context, Object path, ImageView imageView) { Glide.with(context).load((String) path).into(imageView); } } }