Android之UI学习篇十二:ListView控件学习(二)
Android之UI学习篇十二:ListView控件学习(二)
yolinfeng 发表于3年前
Android之UI学习篇十二:ListView控件学习(二)
  • 发表于 3年前
  • 阅读 23
  • 收藏 0
  • 点赞 0
  • 评论 0

新睿云服务器60天免费使用,快来体验!>>>   

上篇利用LIstView实现了一个简单的例子,这篇以一个更贴近项目的例子来分享一下ListView的使用方法,模拟疼痛的微信客户端list界面.

先看看实现效果:



工程结构目录:



这个案例中使用自定义的item.xml布局文件,在Activity中使用simpleAdapter和它的setViewBinder()方法来配置和绑定数据。

以下是源代码:

首先看一下布局文件

main.xml

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

   <ListView
       android:paddingTop="10dip"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/MyListView"/>

</LinearLayout>

item.xml (采用线性布局,利用weight权重属性控制显示比例)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/MyListItem"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="5dip"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/ItemPic"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="left" 
        android:layout_weight="5">
        <ImageView 
            android:id="@+id/image"
            android:layout_width="wrap_content"
        	android:layout_height="wrap_content"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="1">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:layout_weight="1">

            <TextView
                android:id="@+id/name"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textSize="15dip"
                android:textColor="@android:color/white"
                android:layout_weight="1"/>
            <TextView
                android:id="@+id/time"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textSize="12dip"
                android:textColor="@android:color/tertiary_text_light"
                android:layout_weight="3"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:layout_weight="1">
            <TextView
                android:id="@+id/content"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textColor="@android:color/tertiary_text_light"
                android:textSize="12dip" >
            </TextView>
        </LinearLayout>
    </LinearLayout>

</LinearLayout>



MyData.java (放置模拟数据)

package com.test.activity;

public class MyData {
	//头像数组
	public static int[] images = new int[]{
			R.drawable.pic001,R.drawable.pic002,R.drawable.pic003,R.drawable.pic004,
			R.drawable.pic005,R.drawable.pic006,R.drawable.pic007,R.drawable.pic008,
			R.drawable.pic009,R.drawable.pic010,R.drawable.pic011,R.drawable.pic012,
			R.drawable.pic013,R.drawable.pic014,R.drawable.pic015,R.drawable.pic016
	};
	//昵称数组
	public static String[] names = new String[]{
		"爱~未字","心海贝因","初音未来","麻子","英子","飞来耶老师","萌海连上","lovvww",
		"love story","功夫只","海盗","玩那个","青蛙昂子","暮烟","孩子们","钢琴师"
	};
	//时间数组
	public static String[] times = new String[]{
		"凌晨00:23","19:00","19:04","昨天14:20","下午15:56","中午12:43","周一 晚上","周一 晚上",
		"昨天10:23","周四 19:00","周五19:04","昨天14:20","凌晨01:56","中午11:43","周一 早上","周四中午"
	};
	
	//会话数组
	public static String[] contents = new String[]{
		"我喜欢你!","晚上好在,在忙什么呢?","不可以,我会所了","这是一场永不不停息的战争","嗯,行!","OK的啦","加油告白","开心快乐",
		"新年快乐!恭喜发财","恭喜发财!快拿红包来","命运的签或者改变不了","不能说的秘密","I Gusss","Say it right","NONNO","周周年"
	};
}


MainActivity.java (重头戏)

package com.test.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.test.activity.*;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleAdapter.ViewBinder;

public class MainActivity extends Activity {
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ListView listView = (ListView)findViewById(R.id.MyListView);
        System.out.println(listView);
        Log.i("MainActivity", "111");
        //生成适配器,数组
        ArrayList<HashMap<String,Object>> dataList = new ArrayList<HashMap<String,Object>>();
        for(int i=0;i<16;i++)  
        {  
            HashMap<String, Object> map = new HashMap<String, Object>();  
            Resources res=getResources(); 
            //将Drawable图片资源转化为Bitmap对象
            Bitmap bmp=BitmapFactory.decodeResource(res, MyData.images[i]); 
            map.put("image", bmp);  
            map.put("name", MyData.names[i]);  
            map.put("time", MyData.times[i]);
            map.put("content", MyData.contents[i]);  
            dataList.add(map);  
        }  
        //生成适配器,数组
        SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this, dataList, R.layout.item,
        		new String[]{"image","name","time","content"}, new int[]{R.id.image,R.id.name,R.id.time,R.id.content});
        
        //绑定数据到listView
        simpleAdapter.setViewBinder(new ViewBinder() {   
        	public boolean setViewValue(View view, Object data,   
        	String textRepresentation) {   
        	//判断是否为我们要处理的对象   
        	if(view instanceof ImageView && data instanceof Bitmap){   
        	ImageView iv = (ImageView) view;   
        	iv.setImageBitmap((Bitmap) data);   
        	return true;   
        	}else   
        	return false;   
        	}   
        	});   
        
        listView.setAdapter(simpleAdapter);
    }

}



  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 11
博文 196
码字总数 11946
×
yolinfeng
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: