- 设计列表布局:
定义了一个简单的列表界面,其中LinearLayoutCompat
作为外层布局容器,RecyclerView
用于显示可滚动的列表项。列表项的布局由right_list_item
定义
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rightlist"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/right_list_item">
</androidx.recyclerview.widget.RecyclerView>
</androidx.appcompat.widget.LinearLayoutCompat>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="wrap_content"
android:layout_margin="8dp"
android:background="@drawable/edittext_background2"
android:elevation="3dp">
<com.google.android.material.imageview.ShapeableImageView
android:id="@+id/pro_img"
android:layout_width="130dp"
android:layout_height="85dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.App.circleImageView"
tools:scaleType="centerCrop"
tools:srcCompat="@drawable/login" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toStartOf="@+id/pro_detail"
app:layout_constraintTop_toTopOf="@+id/pro_img"
app:srcCompat="@drawable/favorite" />
<TextView
android:id="@+id/pro_title"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_marginStart="8dp"
android:background="@drawable/light_green_background"
android:gravity="center"
android:paddingStart="8dp"
android:paddingEnd="8dp"
android:text="Aparment"
android:textColor="@color/violet"
android:textSize="12sp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="@+id/pro_img"
app:layout_constraintTop_toTopOf="@+id/pro_img" />
<TextView
android:id="@+id/score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="5"
android:textSize="12sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/imageView4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/pro_title" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="@+id/pro_title"
app:layout_constraintEnd_toStartOf="@+id/score"
app:layout_constraintTop_toTopOf="@+id/pro_title"
app:srcCompat="@drawable/star" />
<TextView
android:id="@+id/pro_detail"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="title"
android:textColor="@color/violet"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/pro_title"
app:layout_constraintTop_toBottomOf="@+id/pro_title" />
<ImageView
android:id="@+id/imageView5"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="@+id/pro_detail"
app:layout_constraintTop_toBottomOf="@+id/pro_detail"
app:srcCompat="@drawable/location" />
<TextView
android:id="@+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="address"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="@+id/imageView5"
app:layout_constraintStart_toEndOf="@+id/imageView5"
app:layout_constraintTop_toTopOf="@+id/imageView5" />
<TextView
android:id="@+id/pro_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="¥0"
android:textColor="@color/violet"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/textView10"
app:layout_constraintEnd_toStartOf="@+id/textView10"
app:layout_constraintTop_toTopOf="@+id/textView10" />
<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="/Month"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="@+id/address"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/address"
app:layout_constraintVertical_bias="0.0" />
<View
android:layout_width="match_parent"
android:layout_height="10dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
创建Adapter类:
在java/
你的包名/
你的应用名/
下创建一个新的Java类,通常命名为MyAdapter.java
。
这个类继承自RecyclerView.Adapter
,并定义了数据的持有方式和视图的绑定逻辑。
以下代码定义了一个名为RightListAdapter
的RecyclerView.Adapter
,用于在一个RecyclerView
组件中显示产品信息列表
public class RightListAdapter extends RecyclerView.Adapter<RightListAdapter.MyHolder> {
private List<ProductInfo> mProductInfo = new ArrayList<>();
private RightListOnClickItemListener mRightListOnClickItemListener;
public void setListData(List<ProductInfo> list) {
this.mProductInfo = list;
//刷新
notifyDataSetChanged();
}
@NonNull
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.right_list_item, null);
return new MyHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyHolder holder,int position) {
//绑定数据
ProductInfo productInfo = mProductInfo.get(position);
holder.pro_img.setImageResource(productInfo.getImg());
holder.pro_title.setText(productInfo.getTitle());
holder.address.setText(productInfo.getAddress());
holder.type.setText(productInfo.getType());
holder.pro_price.setText("¥" + productInfo.getPrice()); //int+String 变成文本类型
//点击事件
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(null!=mRightListOnClickItemListener){
mRightListOnClickItemListener.onItemClick(productInfo,position);
}
}
});
}
@Override
public int getItemCount() {
return mProductInfo.size();
}
static class MyHolder extends RecyclerView.ViewHolder {
ImageView pro_img;
TextView pro_title;
TextView pro_detail;
TextView address;
TextView pro_price;
TextView type;
public MyHolder(@NonNull View itemView) {
super(itemView);
pro_img = itemView.findViewById(R.id.pro_img);
pro_title = itemView.findViewById(R.id.pro_detail);
address = itemView.findViewById(R.id.address);
pro_price = itemView.findViewById(R.id.pro_price);
type= itemView.findViewById(R.id.pro_title);
}
}
public RightListOnClickItemListener getmRightListOnClickItemListener() {
return mRightListOnClickItemListener;
}
public void setmRightListOnClickItemListener(RightListOnClickItemListener mRightListOnClickItemListener) {
this.mRightListOnClickItemListener = mRightListOnClickItemListener;
}
public interface RightListOnClickItemListener {
void onItemClick(ProductInfo productInfo,int position);
}
}
综上,关于列表创建主要就是布局文件和对应适配器的代码编写。上述的代码是用于显示产品信息列表的RecyclerView列表项布局和Adapter类的实现示例,包括如何设置布局、绑定数据、处理点击事件以及更新数据源等关键逻辑。