移动互联网开发作业之recycleview设计
项目要求
在原有作业1(见上一篇博文)上选择一个tab添加recycleview显示。显示内容不做限定,可以是新闻、商品、球队、明星、快递时间轴、漫画等。
项目源码地址
码云仓库地址:https://gitee.com/si–yue/MyWeChatDemo/tree/master/
recycleview设计
- 向tab01界面中拖进RecyclerView控件,创建依赖并且添加布局。
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#BBBBBB"/>
- 创建一个item.xml文件,为RecyclerView中的元素设定样式
<?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:background="#ffffff">
<LinearLayout
android:id="@+id/ll_1"
android:layout_width="wrap_content"
android:layout_height="125dp"
android:gravity="center">
<ImageView
android:id="@+id/item_goods_img"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:src="@mipmap/picture1"
android:background="#4D2BD5"/>
</LinearLayout>
<LinearLayout
android:id="@+id/ll_2"
android:layout_width="match_parent"
android:layout_height="125dp"
android:orientation="vertical">
<LinearLayout
android:id="@+id/ll_2_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp">
<TextView
android:id="@+id/item_goods_nametitle"
android:layout_width="90dp"
android:layout_height="25dp"
android:text=""
android:textSize="16sp"
android:textColor="#000000" />
<TextView
android:id="@+id/item_goods_name"
android:layout_width="190dp"
android:layout_height="25dp"
android:text=""
android:textSize="20sp"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll_2_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<TextView
android:id="@+id/item_goods_pricetitle"
android:layout_width="90dp"
android:layout_height="25dp"
android:text=""
android:textSize="16sp"
android:textColor="#000000" />
<TextView
android:id="@+id/item_goods_price"
android:layout_width="190dp"
android:layout_height="25dp"
android:text=""
android:textSize="20sp"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
- 创建适配器adapter
class LinearAdapter extends RecyclerView.Adapter<LinearAdapter.myViewHolder> {
private OnItemClickListener onItemClickListener;
private Context context;
private ArrayList<GoodsEntity> goodsEntities;
public LinearAdapter(Context context,ArrayList<GoodsEntity> goodsEntities){
this.context = context;
this.goodsEntities = goodsEntities;
}
@NonNull
@Override
public LinearAdapter.myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = View.inflate(context, R.layout.layout_linear_item,null);
return new myViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull LinearAdapter.myViewHolder holder, int position) {
GoodsEntity data = goodsEntities.get(position);
holder.mItemGoodsName.setText(data.goodsName);
holder.mItemGoodsPrice.setText(data.goodsPrice);
holder.mItemGoodsNameTitle.setText(data.goodsNameTitle);
holder.mItemGoodsPriceTitle.setText(data.goodsPriceTitle);
}
@Override
public int getItemCount() {
return goodsEntities.size();
}
class myViewHolder extends RecyclerView.ViewHolder {
private ImageView mItemGoodsImg;
private TextView mItemGoodsName;
private TextView mItemGoodsPrice;
private TextView mItemGoodsNameTitle;
private TextView mItemGoodsPriceTitle;
public myViewHolder(@NonNull View itemView) {
super(itemView);
mItemGoodsImg = itemView.findViewById(R.id.item_goods_img);
mItemGoodsName = itemView.findViewById(R.id.item_goods_name);
mItemGoodsPrice = itemView.findViewById(R.id.item_goods_price);
mItemGoodsNameTitle = itemView.findViewById(R.id.item_goods_nametitle);
mItemGoodsPriceTitle = itemView.findViewById(R.id.item_goods_pricetitle);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onItemClickListener!=null){
onItemClickListener.OnItemClick(v,goodsEntities.get(getLayoutPosition()));
}
}
});
}
}
public interface OnItemClickListener {
public void OnItemClick(View view, GoodsEntity data);
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
}
- 创建一个java文件,编写获取页面信息的函数(获取文字和图片信息)
class GoodsEntity implements Serializable {
public String imgPath;
public String goodsName;
public String goodsPrice;
public String goodsNameTitle;
public String goodsPriceTitle;
public GoodsEntity() {
}
public GoodsEntity(String imgPath, String goodsName, String goodsPrice,String goodsNameTitle,String goodsPriceTitle) {
this.imgPath = imgPath;
this.goodsName = goodsName;
this.goodsPrice = goodsPrice;
this.goodsNameTitle = goodsNameTitle;
this.goodsPriceTitle = goodsPriceTitle;
}
public String getImgPath() {
return imgPath;
}
public void setImgPath(String imgPath) {
this.imgPath = imgPath;
}
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
public String getGoodsNameTitle(){
return goodsNameTitle;
}
public void setGoodsNameTitle(String goodsNameTitle){
this.goodsNameTitle = goodsNameTitle;
}
public String getGoodsPriceTitle(){
return goodsPriceTitle;
}
public void setGoodsPriceTitle(String goodsPriceTitle){
this.goodsPriceTitle = goodsPriceTitle;
}
public String getGoodsPrice() {
return goodsPrice;
}
public void setGoodsPrice(String goodsPrice) {
this.goodsPrice = goodsPrice;
}
@Override
public String toString() {
return "GoodsEntity{" +
"imgPath='" + imgPath + '\'' +
", goodsName='" + goodsName + '\'' +
", goodsNameTitle='" + goodsNameTitle + '\'' +
", goodsPrice='" + goodsPrice + '\'' +
", goodsPriceTitle='" + goodsPriceTitle + '\'' +
'}';
}
}
- 在MainActivity.java文件中获取RecyclerView对象,调用获取图片和文字信息的函数,实例化适配器以及设置LayoutManager和Adapter。
参考网址:
1、https://www.jianshu.com/p/c4cfe38a91ed
2、https://developer.android.google.cn/guide/topics/ui/layout/recyclerview#java