对于listView,内部item为这种格式,
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:background="@drawable/list_background" android:orientation="vertical" android:padding="10dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="商品名" android:textColor="@color/black" /> <TextView android:id="@+id/chart_lv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="30sp" android:text="@string/exampling" /> <TextView android:id="@+id/chart_lv_time" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="right" android:text="@string/exampling" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1px" android:layout_marginTop="5dp" android:layout_marginBottom="4dp" android:background="@color/mi_bai" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="收益:(元)" android:textColor="@color/black" /> <TextView android:id="@+id/chart_lv_price" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="@string/exampling" android:textSize="16sp" /> </LinearLayout> </LinearLayout>
通过adapter(适配器)来说
package com.example.math.adapter; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import com.example.math.R; import com.example.math.bean.Commodity; import java.util.List; public class ListAdapter extends BaseAdapter { Context context; List<Commodity> mData; LayoutInflater inflater; public ListAdapter(Context context, List<Commodity> mData) { this.context = context; this.mData = mData; inflater = LayoutInflater.from(context); } @Override public int getCount() { return mData.size(); } @Override public Object getItem(int position) { return mData.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder Holder = null; if (convertView == null) { convertView = inflater.inflate(R.layout.mell_lv, parent, false); Holder = new ViewHolder(convertView); convertView.setTag(Holder); } else { Holder = (ViewHolder) convertView.getTag(); } Commodity commodity = mData.get(position); Holder.typeName.setText(commodity.getName()); Holder.typePrice.setText("¥ " + commodity.getPrice() + ""); Holder.typeTotal.setText(commodity.getTotal() + ""); Holder.typeLeft.setText(commodity.getLeftover() + ""); return convertView; } static class ViewHolder { TextView typeName, typeTotal, typeLeft, typePrice; public ViewHolder(View view) { typeName = view.findViewById(R.id.mall_name); typeTotal = view.findViewById(R.id.mall_total); typeLeft = view.findViewById(R.id.mall_leftover); typePrice = view.findViewById(R.id.mall_price); } } }
标签:02,mData,05,convertView,2023,import,Holder,public,view From: https://www.cnblogs.com/JJTyyds/p/17368443.html