Android基础开发经验—listView自定义

androidUI设计基础测试-模拟新浪微博随便看看栏目(listView自定义)

 效果图:


布局文件:

activity_main.xml

<RelativeLayout 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"
    tools:context=".MainActivity" >
    <!-- 标题栏 -->
<TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:text="@string/name" 
        android:background="#EE4000"
        />
<!-- ListView -->
    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/textView1"
        android:dividerHeight="1px" >
    </ListView>
</RelativeLayout>

list_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="match_parent"
    android:orientation="horizontal" >
    <!-- 左边的图片显示 -->
    <ImageView
        android:id="@+id/photo"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="top"
        android:padding="10dp" />
    <!-- 右边是线性布局 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal" >
            <TextView
                android:id="@+id/name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <TextView
                android:id="@+id/publish"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="right" />
        </LinearLayout>
        <!-- 内容的显示 -->
        <TextView
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:layout_marginTop="3dp"
            />
    </LinearLayout>

</LinearLayout>

编写mainActivity.java

<pre class="java" name="code">package cn.edu.bzu.kt;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.example.sinatest.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class MainActivity extends Activity {
	private ListView listView;
	private List<Map<String, Object>> data;
	SimpleAdapter adapter;
	String [] names= { "photo", "name", "publish","content" }; //定义map中key
	int[] show ={ R.id.photo, R.id.name, R.id.publish, R.id.content };//list_view.xml中的控件id
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题显示
		setContentView(R.layout.activity_main);
		findId();
		setLv();
	}
	/**
	 * @author 
	 *获取控件id
	 */
	public void findId() {
		listView = (ListView) findViewById(R.id.lv);
	}
	
	/**
	 * 使用SimpleAdapter适配器绑定属性
	 */
	public void setLv(){
			data=getData();
			adapter=new SimpleAdapter(this, data,R.layout.list_view,
					names,show);
			listView.setAdapter(adapter);
			listView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> listview, View view,
						int position, long id) {
					// TODO Auto-generated method stub
					  Map<String, Object> item = data.get(position);  
			            Toast.makeText(MainActivity.this, item.get("content").toString(),  
			                    Toast.LENGTH_LONG).show();  //使用Toast消息提示框显示点击内容
				}
			});
		
	}
	/**
	 * @return
	 * 定义Map类型的list对数据的存储
	 */
	public List<Map<String, Object>> getData() {
		List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("photo", R.drawable.a);
		map.put("name", "雨夜");
		map.put("publish", "1分钟前");
		map.put("content", "每天早上,你有两个简单的选择:是睡回笼觉,还是起身追逐梦想。");
		data.add(map);
		map = new HashMap<String, Object>();
		map.put("photo", R.drawable.b);
		map.put("name", " 青春");
		map.put("publish", "10分钟前");
		map.put("content", "我现在,唯一拿的起放不下的是筷子,陷进去出不来的是被窝。");
		data.add(map);
		map = new HashMap<String, Object>();
		map.put("photo", R.drawable.c);
		map.put("name", "小晓");
		map.put("publish", "20分钟前");
		map.put("content", "有些事如果你不去拒绝,别人就会默认你能接受。");
		data.add(map);
		map = new HashMap<String, Object>();
		map.put("photo", R.drawable.d);
		map.put("name", "潇湘");
		map.put("publish", "25分钟前");
		map.put("content", "有人大声表白,有人暗自关怀,爱的方式有千千万万种,能检验它们的,只有时间。");
		data.add(map);
		map = new HashMap<String, Object>();
		map.put("photo", R.drawable.e);
		map.put("name", "Tom");
		map.put("publish", "半小时前");
		map.put("content", "诚实的生活方式,其实是按照自己身体的意愿行事,饿的时候才吃饭,爱的时候不必撒谎。");
		data.add(map);
		map = new HashMap<String, Object>();
		map.put("photo", R.drawable.f);
		map.put("name", "小明");
		map.put("publish", "45分钟前");
		map.put("content", "生命中曾经有过的所有灿烂,终究都需要用寂寞来偿还。");
		data.add(map);
		map = new HashMap<String, Object>();
		map.put("photo", R.drawable.g);
		map.put("name", "牧马人");
		map.put("publish", "一小时前");
		map.put("content", "别人再好,关我什么事;我再不好,关别人什么事。一个人越是炫耀什么,越是说明内心缺少什么。");
		data.add(map);
		return data;

	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

 

本次实验主要使用了SimpleAdapter自定义适配器。

构造函数:

public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

参数:

  • context  SimpleAdapter关联的View的运行环境
  • data    一个Map组成的List。在列表中的每个条目对应列表中的一行,每一个map中应该包含所有在from参数中指定的键
  • resource    一个定义列表项的布局文件的资源ID。布局文件将至少应包含那些在to中定义了的ID
  • from          一个将被添加到Map映射上的键名
  • to     将绑定数据的视图的ID,跟from参数对应,这些应该全是TextView

SimpleAdapter用于ListView,context指的就是上下文一般写this关键字代替data是运用list中Mpa类型进行数据的绑定, resource就是显示你指定的ListView中列表项的布局(比如本次实验的list_View.xml文件,事先定义好一个含有ImageView,TextView,button控件的布局文件, R.id.photo, R.id.name, R.id.publish, R.id.content就是各个控件的id)所以在加载列表的时候,data就是数据源。from对应Map中的Key,to代表的就是组件的id。本次实验中from=

String[]names = { "photo", "name", "publish", "content" };  to=  int[] show = { R.id.photo, R.id.name, R.id.publish, R.id.content };是将map中key为photo的value绑定到R.id.photo......一一映射。



 源码下载: 模拟新浪微博随便看看栏目
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值