模拟新浪微博随便看看栏目

来源:互联网 发布:淘宝买到假货怎么投诉 编辑:程序博客网 时间:2024/05/16 09:20

                        刚学的ListView组件,这个组件很有用。下面是一个模拟新浪微博随便看看栏目,分享给大家! 

                                                 程序效果如下:

                                            

   代码如下:

           MainActivity.java

package cn.bzu.listview03;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.ListView;import android.widget.SimpleAdapter;import android.widget.AdapterView.OnItemClickListener;import android.widget.Toast;//第一步:extends Activitypublic class MainActivity extends Activity {// 第二步:定义数据集合List<Map<String, ?>> data;    ListView listView;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);data = getData();// 第三步:创建SimpleAdapter绑定数据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=(ListView) this.findViewById(R.id.listView);listView.setAdapter(adapter);listView.setOnItemClickListener(new ListClickHandler());}//第四步:添加单击事件private class ListClickHandler implements OnItemClickListener{public void onItemClick(AdapterView<?> adapterView, View view, int position,long id) {Map<String, String> item=(Map<String, String>) data.get(position);}}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.p1);item.put("name", "住进苹果心脏");item.put("publish", "1分钟前");item.put("content", "1.如果这世界上真有奇迹,那就是努力的另一个名字。2.一心向着自己的目标前进的人,整个世界都会给他让路。3忍别人所不能忍的痛,吃别人所别人所不能吃的苦,是为了收获别人得不到的收获。");data.add(item);item = new HashMap<String, Object>();item.put("photo", R.drawable.p2);item.put("name", "我非你爱何必暧昧");item.put("publish", "1分钟前");item.put("content", "现在胖怎么了,想当年我最轻的时候才6斤多,跟我比。");data.add(item);item = new HashMap<String, Object>();item.put("photo", R.drawable.p3);item.put("name", "病亡挽歌");item.put("publish", "2分钟前");item.put("content", "很多年后,我们把这个夏天叫做那年夏天,但是那年夏天,我们曾笑得很美、很绚烂。");data.add(item);item = new HashMap<String, Object>();item.put("photo", R.drawable.p4);item.put("name", "时光凉春衫薄");item.put("publish", "2分钟前");item.put("content", "老班,你不要再费尽心思给我排位了,我到哪都特能聊。");data.add(item);item = new HashMap<String, Object>();item.put("photo", R.drawable.p5);item.put("name", "眼泪是海心里有鱼");item.put("publish", "3分钟前");item.put("content", "话说的再好听,也不如在我口渴的时候给我一杯水。");data.add(item);item = new HashMap<String, Object>();item.put("photo", R.drawable.p6);item.put("name", "不再联系");item.put("publish", "5分钟前");item.put("content", "我做过最勇敢的两件事,喜欢你和放弃你。");data.add(item);item = new HashMap<String, Object>();item.put("photo", R.drawable.p7);item.put("name", "灿灿°单纯的懵懂mmづ");item.put("publish", "10分钟前");item.put("content", "拿自己的热脸贴对方的冷屁股,还总认为自己还做得不够好,在自己眼里,这是爱;在对方眼里,这是烦;在别人眼里,这是贱。");data.add(item);item = new HashMap<String, Object>();item.put("photo", R.drawable.p8);item.put("name", "2b只是铅笔");item.put("publish", "12分钟前");item.put("content", "从我遇见你的那天起,我所做的每一件事都是只是为了更靠近你。");data.add(item);item = new HashMap<String, Object>();item.put("photo", R.drawable.p9);item.put("name", "ヴ菇凉我是纯爷们°");item.put("publish", "15分钟前");item.put("content", "永远不要去评价别人是否幸福,即使那个人看起来孤独无助,幸福如人饮水,冷暖自知,你不是我,怎知我走过的路,心中的乐与苦。");data.add(item);item = new HashMap<String, Object>();item.put("photo", R.drawable.p10);item.put("name", "LOL代替了曾经的CF..");item.put("publish", "30分钟前");item.put("content", "如果有一天你想哭,给我打电话,我无法许诺让你笑,但是我可以跟你一起哭;因为我们是朋友,所以我愿意分享你的悲伤与欢笑,不要觉得这是打扰,这是友谊。");data.add(item);return data;}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.activity_main, menu);return true;}}
  activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent" >    <ListView        android:id="@+id/listView"        android:layout_width="fill_parent"        android:layout_height="fill_parent" /></LinearLayout>
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:padding="10dp"        android:layout_width="48dp"        android:layout_height="48dp" />    <!-- 右边布局 -->    <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"                android:textColor="#000000" />            <!-- 发布时间 -->            <TextView                android:id="@+id/publish"                android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:gravity="right"                 android:textColor="#000000" />        </LinearLayout>        <!-- 下边发布内容 -->        <TextView            android:id="@+id/content"            android:layout_width="wrap_content"            android:layout_height="wrap_content"             android:textColor="#000000" />    </LinearLayout></LinearLayout>

dimens.xml

<resources>    <dimen name="padding_small">8dp</dimen>    <dimen name="padding_medium">8dp</dimen>    <dimen name="padding_large">16dp</dimen></resources>

mytitlebar.xml

<?xml version="1.0" encoding="utf-8"?><resources xmlns:android="http://schemas.android.com/apk/res/android">    <style name="myTitleBg">        <item name="android:background">#FF0000</item>    </style>     <style name="myTheme" parent="android:Theme">        <item name="android:windowNoTitle">false</item>        <item name="android:windowTitleSize">30dp</item>        <item name="android:background">#ffffff</item>        <item name="android:windowTitleBackgroundStyle">@style/myTitleBg</item>    </style></resources>

strings.xml

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

styles.xml

<resources>    <style name="AppTheme" parent="android:Theme.Light" /></resources>





0 0
原创粉丝点击