//导入依赖
implementation 'com.android.support:design:28.0.0'
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
//如果导入失败 在工作空间中配置
allprojects {
repositories {
google()
jcenter()
maven { url "https://jitpack.io" }
}
}
//布局文件中
<?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:orientation="horizontal">
<ImageView
android:id="@+id/iv_icon"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@mipmap/ic_launcher"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="200dp"
android:orientation="vertical"
>
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="标题"
android:textSize="25sp"
/>
<TextView
android:id="@+id/tv_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="价格"
android:textSize="30sp"
android:textColor="#f00"
/>
</LinearLayout>
</LinearLayout>
//M层中
OkGo.<String>get(Constart.SHOPPING_URL).execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
String responseDate=response.body().toString();
Gson gson=new Gson();
Jsonbean jsonbean=gson.fromJson(responseDate,Jsonbean.class);
callBack.onCallBack(jsonbean);
}
});
//主页面showData方法中
ArrayList<Jsonbean.DataBean> beanList = (ArrayList<Jsonbean.DataBean>) jsonbean.getData();
//瀑布流StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
// 线性LinearLayoutManager manager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
//网格GridLayoutManager manager = new GridLayoutManager(context, 2,GridLayoutManager.VERTICAL,false);
rvContainer.setLayoutManager(manager);
ShowAdapter showAdapter=new ShowAdapter(R.layout.recyclerview_item_layout,beanList);
rvContainer.setAdapter(showAdapter);
//适配器中 Jsonbean.DataBean,是自己写的bean类
继承BaseQuickAdapter<Jsonbean.DataBean,BaseViewHolder>
@Override
protected void convert(BaseViewHolder helper, Jsonbean.DataBean item) {
if (item.getList().size()!=0){
if (null!=item.getList().get(0).getTitle()){
helper.setText(R.id.tv_title,item.getList().get(0).getTitle());
}
if (null!=item.getList().get(0).getPrice()+""){
helper.setText(R.id.tv_price,item.getList().get(0).getPrice()+"");
}
if (null!= item.getList().get(0).getImages()){
String images = item.getList().get(0).getImages();
String[] imageDetails = images.split("\\|");
//获取第一张图片
Glide.with(mContext).load(imageDetails[0]).into((ImageView) helper.getView(R.id.iv_icon));
}
}
}