高级控件ListView的使用

<?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.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="match_parent"
        tools:context=".MainActivity">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


        <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/lv_main_lv1"
            ></ListView>

    </android.support.constraint.ConstraintLayout>

ListView_item.xml文件(项资源)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/iv_listviewitem_image"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:padding="10dp"
        android:scaleType="fitXY"
        android:src="@drawable/book1" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="6"
        android:orientation="vertical"
        android:padding="10dp" >

        <TextView
            android:id="@+id/tv_listviewitme_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="狂人摄影日记"
            android:textColor="@color/red"
            android:textSize="20sp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:text="书本作者:"
                android:textColor="@color/black" />

            <TextView
                android:id="@+id/tv_listviewitme_author"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="3"
                android:text="阿刘" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:text="书本价格:"
                android:textColor="@color/black" />

            <TextView
                android:id="@+id/tv_listviewitme_price"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="3"
                android:text="$123"
                android:textColor="@color/black" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:text="    出版社:"
                android:textColor="@color/black" />

            <TextView
                android:id="@+id/tv_listviewitme_publish"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="3"
                android:text="电子出版社"
                android:textColor="@color/black" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:text="书本简介:"
                android:textColor="@color/black" />

            <TextView
                android:id="@+id/tv_listviewitme_remark"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="3"
                android:ellipsize="end"
                android:maxLines="2"
                android:text="很冷的一个冬天很冷的一个冬天很冷的一个冬天很冷的一个冬天很冷的一个冬天很冷的一个冬天很冷的一个冬天很冷的一个冬天很冷的一个冬天很冷的一个冬天"
                android:textColor="@color/black" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:orientation="horizontal" >
	
            <ImageButton
                android:id="@+id/bt_listviewitme_btn1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:layout_weight="1"
                android:src="@drawable/btn_shopping" />

            <ImageButton
                android:id="@+id/bt_listviewitme_btn2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:layout_weight="1"
                android:src="@drawable/btn_accounts" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

BaseAdapter:是所有适配器类的父类,可以对列表项进行最大限度的定制
1.1 自定义适配器中的方法
getCount
getView
getItem
getItemId
1.2 LayoutInflater(布局解析器)
–LayoutInflater有三种获得方式,资料中有详细介绍
用来把layout布局文件解析成一个View对象,不可以new,需要使用系统服务获得
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
使用ConvertView重用组件
即拖动时被遮住、看不见的控件,重用它,而非每次创建一个新的对象

2.2 使用内部类ViewHolder+ConvertView.setTag()保存控件,而不用每次查找
ViewHolder(视图的持有者)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值