《第二行代碼》 第一章ListView

列表控件,如果每個item顯示的內容一樣,可以使用ListView來實現

它的關鍵是Adapter

示例規範的GoodsAdapter

public class GoodsAdapter extends BaseAdapter {

private Context context;
//如果構造方法中有傳入,不用初始化也可以
private List<GoodsBean> goodsBeanList ;

public GoodsAdapter(Context context, List<GoodsBean> goodsBeanList) {
    this.context = context;
    this.goodsBeanList = goodsBeanList;
}

@Override
public int getCount() {
    return goodsBeanList.size();
}

@Override
public GoodsBean getItem(int position) {
    return goodsBeanList.get(position);
}

/**
 * 一般用於bean中有id的,我們這個展示的沒有就返回0即可
 * @param position
 * @return
 */
@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if (convertView == null){
        convertView = LayoutInflater.from(context).inflate(R.layout.item_lv, parent, false);
        holder = new ViewHolder(convertView);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder)convertView.getTag();
    }

    holder.tvName.setText(goodsBeanList.get(position).getGoodsName());

    return convertView;
}

//用靜態
static class ViewHolder {
    ImageView img;
    TextView tvName;
    public ViewHolder(View view){
        img = view.findViewById(R.id.iv_good);
        tvName = view.findViewById(R.id.tv_name);
    }
}
}

相關的類:
GoodsBean.class

public class GoodsBean {
private String goodsUrl;
private String goodsName;

public GoodsBean(String goodsUrl, String goodsName) {
    this.goodsUrl = goodsUrl;
    this.goodsName = goodsName;
}

public String getGoodsUrl() {
    return goodsUrl;
}

public void setGoodsUrl(String goodsUrl) {
    this.goodsUrl = goodsUrl;
}

public String getGoodsName() {
    return goodsName;
}

public void setGoodsName(String goodsName) {
    this.goodsName = goodsName;
}
}  

item_lv.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">

<ImageView
    android:id="@+id/iv_good"
    android:layout_width="200dp"
    android:layout_height="148dp"
    android:scaleType="centerCrop"
    android:src="@mipmap/img_apply_talent_bg"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"/>

<TextView
    android:id="@+id/tv_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toTopOf="@id/iv_good"
    app:layout_constraintBottom_toBottomOf="@id/iv_good"
    app:layout_constraintLeft_toRightOf="@id/iv_good"
    android:layout_marginLeft="20dp"
    tools:text="路飛123"
    />
<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#eeeeee"
    app:layout_constraintTop_toBottomOf="@id/iv_good"/>

</androidx.constraintlayout.widget.ConstraintLayout>  

記得避免複用產生的問題,有if就有else就對了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章