android 服务器下发卡片,Android – 建立卡片列表

本文介绍了如何在Android中使用CardView和RecyclerView创建一个列表。通过在row_layout.xml中设置背景属性并定义CardView,然后在适配器中将CardView作为根元素,实现了卡片式的列表展示。内容包括设置点击和焦点行为,以及在RecyclerView中显示数据的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基于你希望有一个CardViews列表….你可以在你的row_layout.xml中定义如下代码:

android:background="?android:attr/selectableItemBackground"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:id="@+id/cv">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:focusable="true"

android:clickable="true"

android:padding="16dp">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/person_photo"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true"

android:layout_marginRight="16dp"

/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/person_name"

android:layout_toRightOf="@+id/person_photo"

android:layout_alignParentTop="true"

android:textSize="30sp"

/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/person_age"

android:layout_toRightOf="@+id/person_photo"

android:layout_below="@+id/person_name"

/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text=" €"

android:id="@+id/textView"

android:layout_marginLeft = "10dp"

android:layout_below="@+id/person_name"

android:layout_toEndOf="@+id/person_age" />

然后你的布局包含RecyclerView:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

xmlns:app="http://schemas.android.com/apk/res-auto"

tools:context=".MainActivity">

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:layout_width="match_parent"

android:id="@+id/swipe"

app:layout_behavior="@string/appbar_scrolling_view_behavior"

android:layout_height="wrap_content">

android:id="@+id/recyclerList"

android:layout_width="match_parent"

android:background="@color/colorAccent"

android:layout_height="wrap_content"/>

在您的适配器中,CardView可以定义为根元素:

public class Adapter extends RecyclerView.Adapter{

List stocks;

public Adapter(List stocks){

this.stocks = stocks;

}

@Override

public StockViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {

View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout,parent,false);

StockViewHolder pvh = new StockViewHolder(v);

return pvh;

}

@Override

public void onBindViewHolder(StockViewHolder holder,int position) {

holder.stockName.setText(stocks.get(position).getName());

holder.stockPrice.setText(stocks.get(position).getPrice());

Ion.with(holder.stockPhoto).error(R.mipmap.ic_launcher).load(stocks.get(position).getPhotoId());

}

@Override

public int getItemCount() {

return stocks.size();

}

@Override

public void onAttachedToRecyclerView(RecyclerView recyclerView) {

super.onAttachedToRecyclerView(recyclerView);

}

public static class StockViewHolder extends RecyclerView.ViewHolder {

CardView cv;

TextView stockName;

TextView stockPrice;

ImageView stockPhoto;

StockViewHolder(View itemView) {

super(itemView);

cv = (CardView)itemView.findViewById(R.id.cv);

stockName = (TextView)itemView.findViewById(R.id.person_name);

stockPrice = (TextView)itemView.findViewById(R.id.person_age);

stockPhoto = (ImageView)itemView.findViewById(R.id.person_photo);

}

}

}

这会给你这个结果:

这是CardViews的实际列表.

希望能帮助到你!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值