模拟新浪微博随便看看
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="#A020F0"
/>
<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>
新建的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文件代码显示:
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" };
int[] show ={ R.id.photo, R.id.name, R.id.publish, R.id.content };
@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);
}
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();
}
});
}
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.z);
map.put("name", "全宝蓝");
map.put("publish", "1分钟前");
map.put("content", "接地气才会有生命力,根基牢才能有竞争力。");
data.add(map);
map = new HashMap<String, Object>();
map.put("photo", R.drawable.d);
map.put("name", " 朴素妍");
map.put("publish", "10分钟前");
map.put("content", "能够战胜自己的人才会去战胜对手,只想着战胜对手的人更多的是以失败而告终。");
data.add(map);
map = new HashMap<String, Object>();
map.put("photo", R.drawable.f);
map.put("name", "朴智妍");
map.put("publish", "20分钟前");
map.put("content", "总想占别人点小便宜,斤斤计较个人得失,那他失去的将是更多的财富。");
data.add(map);
map = new HashMap<String, Object>();
map.put("photo", R.drawable.bl);
map.put("name", "李居丽");
map.put("publish", "25分钟前");
map.put("content", "心胸狭隘不会有更广阔的天地,冷漠无情不能有更亲密的朋友。");
data.add(map);
map = new HashMap<String, Object>();
map.put("photo", R.drawable.f);
map.put("name", "朴孝敏");
map.put("publish", "半小时前");
map.put("content", "能够想到才有可能做到,连想都不敢想的事情是无法做到的。");
data.add(map);
map = new HashMap<String, Object>();
map.put("photo", R.drawable.g);
map.put("name", "咸恩静");
map.put("publish", "45分钟前");
map.put("content", "只要胸中装着广阔的天空,阳光总会眷顾那些渴望和追求光明的人。");
data.add(map);
map = new HashMap<String, Object>();
map.put("photo", R.drawable.q);
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;
}
}