模仿 新浪微博--随便看看

看一下运行图先:

主要源代码:

MainActivity.java

package com.example.sina;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
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, ?>> data;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// 查找组件
		listView = (ListView) this.findViewById(R.id.listView);
		data = getData();
		SimpleAdapter adapter = new SimpleAdapter(this, data,
				R.layout.list_item, new String[] { "photo", "name", "publish",
						"content" }, new int[] { R.id.photo, R.id.name,
						R.id.publish, R.id.content });
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new ListViewHandler());
	}

	private class ListViewHandler implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> listView, View view, int position,
				long id) {
               Map<String,?>  item=data.get(position);
               Toast.makeText(MainActivity.this,item.get("name").toString(), Toast.LENGTH_LONG).show();
		}

	}

	private List<Map<String, ?>> getData() {
		List<Map<String, ?>> data = new ArrayList<Map<String, ?>>();
		Map<String, Object> item = new HashMap<String, Object>();
		item.put("photo", R.drawable.a);
		item.put("name", "45度的心情");
		item.put("publish", "一分钟前");
		item.put("content", " 等待并不可怕,可怕的是不知道何时是个尽头");
		data.add(item);

		item = new HashMap<String, Object>();
		item.put("photo", R.drawable.b);
		item.put("name", "myMint");
		item.put("publish", "二分钟前");
		item.put("content", "如果世界上曾经有那个人出现过,其他人都会变成将就");
		data.add(item);

		item = new HashMap<String, Object>();
		item.put("photo", R.drawable.c);
		item.put("name", "哈哈哈");
		item.put("publish", "三分钟前");
		item.put("content", "平静是因为已经有所决定");
		data.add(item);

		item = new HashMap<String, Object>();
		item.put("photo", R.drawable.d);
		item.put("name", "嘿嘿嘿");
		item.put("publish", "四分钟前");
		item.put("content", "悄悄,是离别的笙箫,沉默,是今晚的康桥.");
		data.add(item);

		item = new HashMap<String, Object>();
		item.put("photo", R.drawable.e);
		item.put("name", "呵呵呵");
		item.put("publish", "十分钟前");
		item.put("content", "有些人的伤口是在时间中慢慢痊愈,如我。 有些人的伤口是在时间中慢慢溃烂,如他。");
		data.add(item);

		item = new HashMap<String, Object>();
		item.put("photo", R.drawable.f);
		item.put("name", "嘻嘻嘻");
		item.put("publish", "二十分钟前");
		item.put("content", "等待并不可怕,可怕的是不知道何时是个尽头");
		data.add(item);

		item = new HashMap<String, Object>();
		item.put("photo", R.drawable.h);
		item.put("name", "呦呦呦");
		item.put("publish", "三十分钟前");
		item.put("content", "其实等待与时间无关,它是一种习惯,它自由生长,而他无法抑制");
		data.add(item);

		item = new HashMap<String, Object>();
		item.put("photo", R.drawable.i);
		item.put("name", "想想");
		item.put("publish", "四十分钟前");
		item.put("content", "结局已经如此,原因已经不再重要了");
		data.add(item);

		item = new HashMap<String, Object>();
		item.put("photo", R.drawable.a);
		item.put("name", "二子");
		item.put("publish", "五十分钟前");
		item.put("content", "没有缓冲,跳掉了所有的过程,却跳不掉分离多年造成的生疏和难解的心结");
		data.add(item);

		item = new HashMap<String, Object>();
		item.put("photo", R.drawable.b);
		item.put("name", "丸子");
		item.put("publish", "一个小时前");
		item.put("content", "一人花开,一人花落,这些年从头到尾,无人问询");
		data.add(item);

		return data;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
}
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="48dp"
        android:layout_height="48dp" 
        android:padding="10dp"/>

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


        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            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="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="right" />
        </LinearLayout>

        <TextView
            android:id="@+id/content"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>

</LinearLayout>
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" >
    <ListView 
        android:id="@+id/listView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</RelativeLayout>
strings.xml
<resources>

    <string name="app_name">Sina</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">新浪微博—随便看看</string>

</resources>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值