ListView使用技巧

1.head(foot)View

通过给ListView添加各种head/foot可以实现各种自定义功能,主要用法如下(Kotlin):

//需要注意的是,只能添加布局,不能添加单个控件,比如Button
val headView = LayoutInflater.from(this).inflate(R.layout.item_head_blank, null) as LinearLayout
mLVOrderList.addHeaderView(headView)

//setAdapter一定要在添加布局之后
mLVOrderList.adapter = orderAdapter

用一段空白做个简单的例子:

<?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="20dp"
    android:background="@color/colorInfoBg"
    android:orientation="vertical">

    //没有子控件时,height指定多少都不显示

</LinearLayout>
<?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:background="@color/colorInfoBg"
    android:orientation="vertical">

    //这才是添加空白的正确姿势
    <com.zhy.autolayout.AutoLinearLayout
        android:layout_width="match_parent"
        android:layout_height="20px"/>

</LinearLayout>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容