【领券联盟】笔记:视频27-编写分类详情的item内容
破小布的小花狗
发表于
2020-03-30 04:05
1711
课堂笔记
课堂笔记
- 给 HomePagerFragment 布局添加 RecyclerView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/home_pager_content_list" />
</LinearLayout>
- 给 RecyclerView mContentList 设置布局管理器和适配器
@Override
protected void initView() {
mContentList.setLayoutManager(new LinearLayoutManager(getContext()));
mHomePagerContentAdapter = new HomePagerContentAdapter();
mContentList.setAdapter(mHomePagerContentAdapter);
}
- 创建 HomePagerContentAdapter
public class HomePagerContentAdapter extends RecyclerView.Adapter<HomePagerContentAdapter.InnerHolder> {
@NonNull
@Override
public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return null;
}
@Override
public void onBindViewHolder(@NonNull InnerHolder holder, int position) {
}
@Override
public int getItemCount() {
return 0;
}
public class InnerHolder extends RecyclerView.ViewHolder {
public InnerHolder(@NonNull View itemView) {
super(itemView);
}
}
}
- 把分类页面数据给 HomePagerContentAdapter
@Override
public void onContentLoaded(List<HomePagerContent.DataBean> contents) {
mHomePagerContentAdapter.setData(contents);
setUpState(State.SUCCESS);
}
- HomePagerContentAdapter 创建集合保存数据
private List<HomePagerContent.DataBean> mData = new ArrayList<>();
public void setData(List<HomePagerContent.DataBean> contents) {
mData.clear();
mData.addAll(contents);
notifyDataSetChanged();
}
- 创建 itemView 布局 item_home_pager_content (和老师的有些出入)
<?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"
android:gravity="center_vertical">
<ImageView
android:id="@+id/goods_cover"
android:layout_width="85dp"
android:layout_height="85dp"
android:scaleType="fitCenter"
android:src="@mipmap/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/goods_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:textStyle="bold"
android:text="看见拉开距离阿斯利康就按当时看了就啊撒旦立刻解十点十分放埃里克森"
android:textSize="12sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/shape_tag_bg"
android:id="@+id/tv_saved_prise"
android:padding="2dp"
android:text="省15元"
android:textColor="@color/white"
android:textSize="12dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="券后价:"
android:textColor="@color/colorPrimary" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="19.9"
android:id="@+id/tv_final_prise"
android:textColor="@color/colorPrimary" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="¥23"
android:id="@+id/tv_original_prise"
android:textSize="10dp"
android:layout_marginStart="10dp"
android:textColor="@color/colorGray" />
<TextView
android:layout_marginStart="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1000人已经购买"
android:id="@+id/tv_sell_count"
android:textColor="@color/colorGray" />
</LinearLayout>
</LinearLayout>
</LinearLayout>