Android初试--自定义ListView的使用

来源:互联网 发布:家用万兆网络 编辑:程序博客网 时间:2024/05/03 02:36

Android初试--自定义ListView的使用

        上一篇文章中我们介绍了,关于ListView的简单使用,现在我们就来看看如何自定义ListView的使用。

        1.将使用到的图添加到res/drawable-hdpi的文件夹中。

        2.在res/layout中定义listview中每一项的布局文件“listview_item.xml”。

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent" >

<ImageView 

    android:id="@+id/touxiang"

    android:layout_width="wrap_content"   

        android:layout_height="wrap_content"

        android:layout_marginTop="10dip" 

        android:layout_marginLeft="10dip"

        android:layout_marginRight="10dip"

    />

<TextView 

    android:id="@+id/username"

    android:layout_width="wrap_content"   

        android:layout_height="wrap_content" 

        android:layout_alignTop="@+id/touxiang"

        android:layout_toRightOf="@+id/touxiang"

        android:textColor="#3333ff"

        android:textSize="25sp"

    />

<TextView 

    android:id="@+id/fabiaotime"

    android:layout_width="wrap_content"   

        android:layout_height="wrap_content" 

        android:layout_alignTop="@+id/username"

        android:layout_marginRight="10dip"

        android:layout_alignParentRight="true"

        android:textColor="#bfbfbf"

        android:textSize="15sp"

    />

<TextView 

    android:id="@+id/info"

    android:layout_width="match_parent"   

        android:layout_height="wrap_content" 

        android:layout_below="@+id/touxiang"

        android:layout_alignLeft="@+id/username"

        android:layout_marginRight="10dip"

        android:textColor="#000000"

        android:textSize="20sp"

    />

<ImageView

    android:id="@+id/contenttupiao"

    android:layout_width="200dip"   

        android:layout_height="200dip" 

        android:layout_below="@+id/info"

        android:layout_alignLeft="@+id/username"

         android:layout_marginTop="10dip"

    />

<TextView 

    android:id="@+id/laiyuan"

    android:layout_width="match_parent"   

        android:layout_height="wrap_content" 

        android:layout_alignLeft="@+id/username"

        android:layout_below="@+id/contenttupiao"

        android:layout_marginTop="10dip"

        android:textColor="#bfbfbf"

        android:textSize="15sp"

    />

</RelativeLayout>

        3.在res/layout中修改主布局文件“activity_main.xml”,这个布局中将会包含一个listview组件。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent" 

    android:orientation="vertical"

    >

<ListView 

    android:id="@+id/mainlistview"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    >

</ListView>

</LinearLayout>

        4.编写主Activity文件。

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.HashMap;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ListView;

import android.widget.SimpleAdapter;

import android.widget.Toast;

public class MainActivity extends Activity {

    private  ListView  listview=null;

    private   ArrayList<HashMap<String, Object>> listItem =null;

    public  String  getTime(){

      SimpleDateFormat  sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

         String nowtime= sdf.format(new Date());

     return nowtime;

    }

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        //得到listview组件

        listview=(ListView)findViewById(R.id.mainlistview);

        //组织显示在listview中组件的数据内容

        //添加第一个数据

        HashMap<String,Object>  data1=new HashMap<String,Object>();     

        data1.put("touxiang",R.drawable.tx1);

        data1.put("username", "司马懿");

        data1.put("nowtime", getTime());

        data1.put("contentinfo", "难道真的是天命难违?");

        data1.put("contenttupian",R.drawable.contenttupian1);

        data1.put("datafrom","来自    我的微博");

        //添加第二个数据

        HashMap<String,Object>  data2=new HashMap<String,Object>();     

        data2.put("touxiang",R.drawable.tx2);

        data2.put("username", "诸葛亮");

        data2.put("nowtime", getTime());

        data2.put("contentinfo", "观今夜之天象,知天下之大事");

        data2.put("contenttupian",R.drawable.contenttupian2);

        data2.put("datafrom","来自    我的微博");

        //将准备好的数据添加进ArrayList

       listItem = new ArrayList<HashMap<String, Object>>();  

        listItem.add(data1);

        listItem.add(data2);

        //创建ListView的适配器

        SimpleAdapter listItemAdapter=new SimpleAdapter(

         this,

         listItem,

         R.layout.list_item,

         new String[]{"touxiang","username","nowtime","contentinfo","contenttupian","datafrom"},

         new int[]{R.id.touxiang,R.id.username,R.id.fabiaotime,R.id.info,R.id.contenttupiao,R.id.laiyuan}

         );

        //将适配器添加到listview

        listview.setAdapter(listItemAdapter);

      //添加点击  

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,

long arg3) {

String  name=(String)listItem.get(arg2).get("username");

    Toast.makeText(MainActivity.this, "大家好,我是"+name, 1000).show();

}

});

    }

}

        测试效果:

下拉后可以看见下一条,如图:

当点击某一条后,会弹出信息:

0 0
原创粉丝点击