Android中的自定义ListView

Android中ListView是我们进行安卓APP开发中最常用和最难用的控件,因此我们必须熟练掌握,今天我就来给大家介绍一下它的高级用法。效果如下

1.首先介绍ListView的工作原理,看一下它的简单用法,

首先需要一个String数组例如:

private String[] data = {"Apple","Orange"};

然而数组中的数据是无法直接传递给ListView的,需要借助于适配器来完成,一般用ArrayAdapter。

我们新建一个适配器

ArrayAdapter<String>adpater = new ArraysAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,data);

其中MainActivity.this是我们ListView的父对象,android.R.layout.simple_list_item_1是安卓自己提供的listview的模板,我们可以进行自定义一个layout进行绑定,data就是对应于layout的数据。

然后获取listview对象,绑定适配器即可。

ListView listView = (ListView)findViewById(R.id.list_view);

listView.setAdapter(adpater);

就可以显示了。

2.我们需要做的就是

(1).自定义一个数据类型dataType对应于上面的String类型。

(2).自定义一个layout对应于上面的android.R.layout.simple_list_item_1。

(3).自定义一个包含dataType类型的数组array,对应于上面的data。

a.新建的类中应该包含三个String分别储存title,data,h和blog。 一个int类型存储图片的id。新建如下:

public class FirstPage {
    private String title;
    private String time;
    private String blog;
    private int imageId;
    public FirstPage(String title,String  time,String blog,int imageId){
        this.title = title;
        this.time = time;
        this.blog = blog;
        this.imageId = imageId;
    }
    public String getBlog() {
        return blog;
    }
    public int getImageId() {
        return imageId;
    }
    public String getTime() {
        return time;
    }
    public String getTitle() {
        return title;
    }
    public void setImageId(int imageId) {
        this.imageId = imageId;
    }
    public void setBlog(String blog) {
        this.blog = blog;
    }
    public void setTime(String time) {
        this.time = time;
    }
    public void setTitle(String title) {
        this.title = title;
    }
}
b.新建一个xml文件为firstpage_layout.xml整个布局分为两部分,左边为三个TextView 右边一个ImageView
因此最外边采用LinearLayout方向用水平分布,内部包含两个LinearLayout,第一个内部采用竖直布局,放三个TextView
第二个内部放一个ImageView代码如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="100dp">
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:text="I am title"
            android:id="@+id/title"
            android:textStyle="bold"
            android:textSize="20sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:maxLines="2"
            />
        <TextView
            android:text="I am time"
            android:id="@+id/time"
            android:textSize="15sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:lines="1"
            android:ellipsize="end"
            android:layout_weight="2" />
        <TextView
            android:text="I am blog"
            android:id="@+id/blog"
            android:textSize="15sp"
            android:lines="2"
            android:ellipsize="end"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="4" />
    </LinearLayout>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_weight="3"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

</LinearLayout>
我们看到效果blog部分最多显示两行,之后的不加以显示。使用代码
android:lines="2"
android:ellipsize="end"
即可实现
title部分采用
android:textStyle="bold"
android:maxLines="2"
实现加粗,且最多显示两行的效果

c.建立数组
在MainActicity中新建数组
private List<FirstPage> firstList = new ArrayList<>();
在主程序中实例化几个FirstPage类后加入到数组中,绑定适配器即可实现,代码如下:

FirstPage f = new FirstPage("旅游胜地","2019年4月19日","说到地标性建筑小伙伴们都能张口就来,像上海的东方明珠、广州的小蛮腰、北京的鸟巢等.说到地标性建筑小伙伴们都能张口就来,像上海的东方明珠、广州的小蛮腰、北京的鸟巢等",R.drawable.tmm);
firstList.add(f);
FirstPage f2 = new FirstPage("旅游胜地2","2019年4月19日","说到地标性建筑小伙伴们都能张口就来,像上海的东方明珠、广州的小蛮腰、北京的鸟巢等.说到地标性建筑小伙伴们都能张口就来,像上海的东方明珠、广州的小蛮腰、北京的鸟巢等",R.drawable.tmm);
firstList.add(f2);
FirstPage f3 = new FirstPage("旅游胜地3","2019年4月19日","说到地标性建筑小伙伴们都能张口就来,像上海的东方明珠、广州的小蛮腰、北京的鸟巢等.说到地标性建筑小伙伴们都能张口就来,像上海的东方明珠、广州的小蛮腰、北京的鸟巢等",R.drawable.tmm);
firstList.add(f3);
FirstPage f4 = new FirstPage("旅游胜地4","2019年4月19日","说到地标性建筑小伙伴们都能张口就来,像上海的东方明珠、广州的小蛮腰、北京的鸟巢等.说到地标性建筑小伙伴们都能张口就来,像上海的东方明珠、广州的小蛮腰、北京的鸟巢等",R.drawable.tmm);
firstList.add(f4);
FirstPage f5 = new FirstPage("旅游胜地5","2019年4月19日","说到地标性建筑小伙伴们都能张口就来,像上海的东方明珠、广州的小蛮腰、北京的鸟巢等.说到地标性建筑小伙伴们都能张口就来,像上海的东方明珠、广州的小蛮腰、北京的鸟巢等",R.drawable.tmm);
firstList.add(f5);
FirstPage f6 = new FirstPage("旅游胜地6","2019年4月19日","说到地标性建筑小伙伴们都能张口就来,像上海的东方明珠、广州的小蛮腰、北京的鸟巢等.说到地标性建筑小伙伴们都能张口就来,像上海的东方明珠、广州的小蛮腰、北京的鸟巢等",R.drawable.tmm);
firstList.add(f6);
FirstPage f7 = new FirstPage("旅游胜地7","2019年4月19日","说到地标性建筑小伙伴们都能张口就来,像上海的东方明珠、广州的小蛮腰、北京的鸟巢等.说到地标性建筑小伙伴们都能张口就来,像上海的东方明珠、广州的小蛮腰、北京的鸟巢等",R.drawable.tmm);
firstList.add(f7);
FirstPageAdapter adpter = new FirstPageAdapter(MainActivity.this,R.layout.firstpage_layout,firstList);
ListView listView = (ListView)findViewById(R.id.list_view);
listView.setAdapter(adpter);
即可实现自定义LIstView。

同时根据此原理我们可以设计出各种各样类型的ListView

 

下载链接:https://download.csdn.net/download/qq_34020487/11128906

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值