模拟新浪微博的随便看看栏目(ListView自定义)

来源:互联网 发布:玫瑰花数 c语言 编辑:程序博客网 时间:2024/05/21 11:23

效果图:

布局文件:

activity_list_view.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="fill_parent"        android:layout_height="fill_parent"         /></RelativeLayout>

list_item.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="horizontal" >    <!-- 左边是图片 -->    <ImageView        android:id="@+id/photo"       android:layout_gravity="top"        android:layout_width="50dp"        android:layout_height="50dp"        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="match_parent"            android:orientation="horizontal" >            <TextView                android:id="@+id/name"                android:layout_width="wrap_content"                android:layout_height="wrap_content" >            </TextView>            <TextView                android:id="@+id/publish"                android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:gravity="right" >            </TextView>        </LinearLayout>        <!-- 发布内容 -->        <TextView            android:id="@+id/content"            android:layout_width="fill_parent"            android:layout_height="wrap_content" />    </LinearLayout></LinearLayout>

ListViewActivity.java

package cn.bzu.listviewselfdefination;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.MenuItem;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;import android.support.v4.app.NavUtils;public class ListViewActivity extends Activity {private ListView listView;private List<Map<String, ?>> data;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_list_view);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 {@Overridepublic void onItemClick(AdapterView<?> listview, View view,int position, long id) {Map<String, ?> item = data.get(position);Toast.makeText(ListViewActivity.this, item.get("content").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", "念");item.put("publish", "1分钟前");item.put("content","杯子寂寞,被倒进开水,滚烫的感觉,这就是恋爱的感觉。水变温了,杯子很舒服,想这是生活的感觉。水变凉了,杯子害怕,也许这就是失去的感觉。水彻底的凉,杯子难受,把水倒出。杯子很舒服,杯子也掉在地上摔成一片一片的。发现每一片都有水的痕迹,它知道心里还爱着水,想再爱一次水,却不可能了。");data.add(item);item = new HashMap<String, Object>();item.put("photo", R.drawable.b);item.put("name", "向往美好生活");item.put("publish", "5分钟前");item.put("content","哭的时候没人哄,我学会了坚强;怕的时候没人陪,我学会了勇敢;烦的时候没人问,我学会了承受;累的时候没人可以依靠,我学会了自立。就这样我找到了自己,原来我很优秀,更可贵的是,世界上,只有一个我!渐渐地,我成熟了,因为没有更大的不如意,所以现在偶尔的不如意也是幸福的!");data.add(item);item = new HashMap<String, Object>();item.put("photo", R.drawable.c);item.put("name", "好好的");item.put("publish", "10分钟前");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", "love");item.put("publish", "15分钟前");item.put("content","因为不想受伤,我们戴上面具,穿上武裝,设法把自己脆弱的一面藏起來。可是,我们隠藏的愈多,被发现的时候,暴露的就更多。我们愈不敢面对伤痛,被刺伤的时候,伤口就愈痛。最不容易受伤的人,不是最坚强的人,而是最坦诚面对自己的人。");data.add(item);item = new HashMap<String, Object>();item.put("photo", R.drawable.f);item.put("name", "美好生活");item.put("publish", "5分钟前");item.put("content","别人拥有的,你不必羡慕,只要努力,你也会拥有;自己拥有的,你不必炫耀,因为别人也在奋斗,也会拥有。多一点快乐,少一点烦恼,不论富或穷,地位高或低,知识浅或深。");data.add(item);return data;}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.activity_list_view, menu);return true;}}


 

原创粉丝点击