android:textSize=“20sp”
android:textStyle=“bold” />
<RelativeLayout
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_alignParentRight=“true”
android:layout_alignParentBottom=“true”
android:layout_marginTop=“10dp”
android:layout_marginBottom=“4dp”
android:orientation=“horizontal”>
<TextView
android:id=“@+id/tv_reduce_goods_num”
android:layout_width=“24dp”
android:layout_height=“24dp”
android:background=“@drawable/bg_reduce_goods_num”
android:gravity=“center”
android:text=“—”
android:textColor=“#000”
android:textSize=“16sp” />
<TextView
android:id=“@+id/tv_goods_num”
android:layout_width=“40dp”
android:layout_height=“24dp”
android:layout_marginLeft=“-0.5dp”
android:layout_marginRight=“-0.5dp”
android:layout_toRightOf=“@+id/tv_reduce_goods_num”
android:background=“@drawable/bg_goods_num”
android:gravity=“center”
android:text=“1”
android:textColor=“#000”
android:textSize=“16sp” />
<TextView
android:id=“@+id/tv_increase_goods_num”
android:layout_width=“24dp”
android:layout_height=“24dp”
android:layout_toRightOf=“@+id/tv_goods_num”
android:background=“@drawable/bg_increase_goods_num”
android:gravity=“center”
android:text=“+”
android:textColor=“#000”
android:textSize=“16sp” />
item_store.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=“wrap_content”
android:layout_marginBottom=“12dp”
android:background=“@drawable/bg_white_8”
android:orientation=“vertical”
android:padding=“12dp”>
<LinearLayout
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_marginBottom=“12dp”
android:gravity=“center_vertical”>
<ImageView
android:id=“@+id/iv_checked_store”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginRight=“12dp”
android:src=“@drawable/ic_check” />
<TextView
android:id=“@+id/tv_store_name”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“店铺名”
android:textColor=“#000”
android:textSize=“16sp”
android:textStyle=“bold” />
<androidx.recyclerview.widget.RecyclerView
android:id=“@+id/rv_goods”
android:layout_width=“match_parent”
android:layout_height=“wrap_content” />
OK,布局这边就写完了。
配置项目的依赖和网络使用情况,首先在AndroidManifest.xml中增加网络访问权限的配置
然后修改一下styles.xml中的样式
然后就是配置项目的依赖库了,首先在工程的build.gradle下配置
maven { url “https://jitpack.io” }
然后在app下的build.gradle中配置。
//Gson解析
implementation ‘com.google.code.gson:gson:2.8.6’
//RecyclerView
implementation ‘androidx.recyclerview:recyclerview:1.1.0’
//RecyclerView的好搭档
implementation ‘com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.22’
//热门强大的图片加载器
implementation ‘com.github.bumptech.glide:glide:4.9.0’
annotationProcessor ‘com.github.bumptech.glide:compiler:4.9.0’
配置好之后点击右上角的Sync Now进行同步依赖库。同步好之后进入第四步,渲染数据。
列表的渲染自然是离不开适配器的,那么一个购物车里面可能有多个店铺,一个店铺有多个商品,那么就是两个列表,也需要两个适配器,店铺适配器和商品适配器。
首先是店铺适配器,在com.llw.cart下创建一个adapter包,包下新建一个StoreAdapter类,里面的代码如下:
package com.llw.cart.adapter;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.llw.cart.bean.CarResponse;
import com.llw.cart.R;
import java.util.List;
/**
-
店铺适配器
-
@author llw
*/
public class StoreAdapter extends BaseQuickAdapter<CarResponse.OrderDataBean, BaseViewHolder> {
private RecyclerView rvGood;
public StoreAdapter(int layoutResId, @Nullable List<CarResponse.OrderDataBean> data) {
super(layoutResId, data);
}
@Override
protected void convert(BaseViewHolder helper, CarResponse.OrderDataBean item) {
rvGood = helper.getView(R.id.rv_goods);
helper.setText(R.id.tv_store_name,item.getShopName());
final GoodsAdapter goodsAdapter = new GoodAdapter(R.layout.item_good,item.getCartlist());
rvGood.setLayoutManager(new LinearLayoutManager(mContext));
rvGood.setAdapter(goodAdapter);
}
}
然后是商品适配器,在新建一个GoodsAdapter类,里面的代码如下:
package com.llw.cart.adapter;
import android.widget.ImageView;
import androidx.annotation.Nullable;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.llw.cart.bean.CarResponse;
import com.llw.cart.R;
import java.util.List;
/**
-
商品适配器
-
@author llw
*/
public class GoodsAdapter extends BaseQuickAdapter<CarResponse.OrderDataBean.CartlistBean, BaseViewHolder> {
public GoodsAdapter(int layoutResId, @Nullable List<CarResponse.OrderDataBean.CartlistBean> data) {
super(layoutResId, data);
}
@Override
protected void convert(BaseViewHolder helper, CarResponse.OrderDataBean.CartlistBean item) {
helper.setText(R.id.tv_good_name, item.getProductName())
.setText(R.id.tv_good_color,item.getColor())
.setText(R.id.tv_good_size,item.getSize())
.setText(R.id.tv_goods_price,item.getPrice()+“”)
.setText(R.id.tv_goods_num,item.getCount()+“”);
ImageView goodImg = helper.getView(R.id.iv_goods);
Glide.with(mContext).load(item.getDefaultPic()).into(goodImg);
}
}
适配器都写好了,那么该去Activity中去显示数据了。
进入MainActivity,创建一些变量
public static final String TAG = “MainActivity”;
private RecyclerView rvStore;
private List<CarResponse.OrderDataBean> mList = new ArrayList<>();
private StoreAdapter storeAdapter;
然后新建一个initView方法,在这里进行数据的解析,然后赋值,后面设置到适配器中。
/**
- 初始化
*/
private void initView() {
//设置亮色状态栏模式 systemUiVisibility在Android11中弃用了,可以尝试一下。
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
rvStore = findViewById(R.id.rv_store);
CarResponse carResponse = new Gson().fromJson(Constant.CAR_JSON, CarResponse.class);
mList.addAll(carResponse.getOrderData());
storeAdapter = new StoreAdapter(R.layout.item_store, mList);
rvStore.setLayoutManager(new LinearLayoutManager(this));
rvStore.setAdapter(storeAdapter);
}
最后在onCreate方法中调用initView方法即可。