美文网首页Android开发
《第二行代码》 第一章ListView,GridView

《第二行代码》 第一章ListView,GridView

作者: 你的益达233 | 来源:发表于2021-09-17 11:54 被阅读0次

    列表控件,如果每个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就对了

    二、GridView

    它比ListView多了个几个属性,其他的都一样

        android:horizontalSpacing="1dp"
        android:numColumns="3"
        android:verticalSpacing="1dp"

    相关文章

      网友评论

        本文标题:《第二行代码》 第一章ListView,GridView

        本文链接:https://www.haomeiwen.com/subject/eubjgltx.html