listview组件的应用—模拟微博

来源:互联网 发布:程序员个人工作体会 编辑:程序博客网 时间:2024/04/30 07:57


ListView组件的应用(模拟新浪微博界面)

阶段一:进行主界面的布局                           

Main.xml具体代码如下:

<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" >    <ListView        android:id="@+id/list"        android:layout_width="fill_parent"        android:layout_height="wrap_content" >    </ListView></LinearLayout>

在layout下新建item.xml,代码如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:orientation="horizontal" >    <ImageView        android:id="@+id/image"        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:paddingTop="10dp"                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:paddingTop="10dp"                android:gravity="right" />        </LinearLayout>        <TextView            android:id="@+id/content"            android:paddingTop="10dp"            android:paddingBottom="10dp"            android:layout_width="fill_parent"            android:layout_height="wrap_content" />    </LinearLayout></LinearLa
<p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Tahoma; font-size: 14px; line-height: 24px;">阶段二:编写MainActivity并进行相应的事件处理,具体代码如下:</p><pre class="java" name="code" style="margin-top: 0px; margin-bottom: 0px; padding: 0px; font-size: 14px; line-height: 24px;">package com.lks.sinablog;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 List<Map<String, ?>> data;private ListView listItem;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);listItem = (ListView) this.findViewById(R.id.list);data = getData();SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item,new String[] { "image", "name", "publish", "content" },new int[] { R.id.image, R.id.name, R.id.publish, R.id.content });listItem.setAdapter(adapter);listItem.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> adapter, View view, int position,long id) {Map<String, Object> item=(Map<String, Object>) data.get(position);Toast.makeText(getApplicationContext(), item.get("name")+"\n\n"+item.get("content"), 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("image", R.drawable.image1);item.put("name", "世界末日");item.put("publish", "1分钟前");item.put("content", "我过得还可以,不好不坏,不惊不喜,一切只是还可以。这样的生活我觉得也挺好。");data.add(item);item = new HashMap<String, Object>();item.put("image", R.drawable.image2);item.put("name", "吹乱了章节");item.put("publish", "9分钟前");item.put("content","我们都在等,等到最后或许是彼此转身都没有回首,此刻我们背对背,下一刻你是否会转身,我只能等待,或许你也在等待,那我们只能在等待中消散,最后谁都没有回头,就像车站离别,拥抱完就各自上车,又或许你忘了我叫什么……");data.add(item);item = new HashMap<String, Object>();item.put("image", R.drawable.image3);item.put("name", "回不去的过去");item.put("publish", "23分钟前");item.put("content", "开始听一些很旧的歌,才明白原来生活中的一切都会老去。");data.add(item);item = new HashMap<String, Object>();item.put("image", R.drawable.image4);item.put("name", "夜,未央");item.put("publish", "43分钟前");item.put("content", "我不贪心,只希望现在的朋友都不变,以后还是可以相聚,无关金钱名利,大碗喝酒大口吃肉,能潇洒开心的日子就忘记痛苦。老了,以后相拥回忆往事,在黄昏下相望。");data.add(item);item = new HashMap<String, Object>();item.put("image", R.drawable.image5);item.put("name", "浅光旧人不覆");item.put("publish", "1小时前");item.put("content","似乎所有的开始都有一个写好了的结局就算你自己再怎么努力也扭转不了注定,此刻我羡慕的是曾经我所不珍惜的……");data.add(item);return data;}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.main, menu);return true;}}

阶段三:自定义标题栏

首先,修改标题栏的高度和背景,在res\values下新建title.xml文件,在其中添加代码:

<resources>    <style name="TitleBackground">     <item name="android:background">#FF0000</item> </style><style name="selfdefine" parent="android:Theme">     <item name="android:windowTitleSize">30dp</item>     <item name="android:windowTitleBackgroundStyle">@style/TitleBackground</item> </style></resources>

然后修改AndroidManifest.xml文件:

<activity            android:name=".MainActivity"            android:theme="@style/selfdefine"            android:label="@string/title_activity_main" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>

 

运行结果显示:

                              

<p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; color: rgb(57, 57, 57); font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px; background-color: rgb(250, 247, 239);"><span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; font-family: 楷体; font-size: 18px;">  分析问题:</span></span></p><p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; color: rgb(57, 57, 57); font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px; background-color: rgb(250, 247, 239);"> </p><p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; color: rgb(57, 57, 57); font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px; background-color: rgb(250, 247, 239);"><span style="margin: 0px; padding: 0px; font-family: 楷体;"><span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; font-size: 18px;">1.ListView及ListView_Item的使用</span></span></span></p><p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; color: rgb(57, 57, 57); font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px; background-color: rgb(250, 247, 239);"> </p><p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; color: rgb(57, 57, 57); font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px; background-color: rgb(250, 247, 239);"><span style="margin: 0px; padding: 0px; font-family: 楷体;"><span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; font-size: 18px;">2.实体类</span></span></span></p><p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; color: rgb(57, 57, 57); font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px; background-color: rgb(250, 247, 239);"> </p><p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; color: rgb(57, 57, 57); font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px; background-color: rgb(250, 247, 239);"><span style="margin: 0px; padding: 0px; font-family: 楷体;"><span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; font-size: 18px;">3.ListView自定义适配器的书写</span></span></span></p><p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; color: rgb(57, 57, 57); font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px; background-color: rgb(250, 247, 239);"> </p><p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; color: rgb(57, 57, 57); font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px; background-color: rgb(250, 247, 239);"><span style="margin: 0px; padding: 0px; font-family: 楷体;"><span style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; font-size: 18px;">4.ListView的绑定数据源与控件</span></span></span></p><p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; color: rgb(57, 57, 57); font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px; background-color: rgb(250, 247, 239);"> </p><p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; color: rgb(57, 57, 57); font-family: verdana, 'ms song', Arial, Helvetica, sans-serif; font-size: 14px; line-height: 21px; background-color: rgb(250, 247, 239);"><span style="margin: 0px; padding: 0px; font-family: 楷体;"><span style="margin: 0px; padding: 0px; line-height: 1.5;"><span style="margin: 0px; padding: 0px; font-size: 18px;">5.</span><span style="margin: 0px; padding: 0px; font-size: 18px;">继承:BaseAdapter</span></span></span></p>

0 0
原创粉丝点击