模拟新浪微博随便看看栏目(listView自定义)
布局文件: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="#EE4000"
/>
<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>
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="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代码展示:
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();
}
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.a);
map.put("name", "八月爱丽丝");
map.put("publish", "1分钟前");
map.put("content", "从信息的传播上看,评论阻碍了信息的水平向流动。");
data.add(map);
map = new HashMap<String, Object>();
map.put("photo", R.drawable.b);
map.put("name", " 爱你的我");
map.put("publish", "10分钟前");
map.put("content", "对于普通用户来说,被人评论的可能性几乎为零。");
data.add(map);
map = new HashMap<String, Object>();
map.put("photo", R.drawable.c);
map.put("name", "神木。");
map.put("publish", "20分钟前");
map.put("content", "据国家互联网信息办公室发言人30日披露,一批传播谣言的互联网站被依法查处。");
data.add(map);
map = new HashMap<String, Object>();
map.put("photo", R.drawable.d);
map.put("name", "滨州");
map.put("publish", "20分钟前");
map.put("content", "针对新浪和腾讯博客网站集中出现的谣言的做了相应处罚");
data.add(map);
map = new HashMap<String, Object>();
map.put("photo", R.drawable.e);
map.put("name", "么么哒");
map.put("publish", "半小时前");
map.put("content", "希望广大网民自觉遵守法律法规,不信谣、不传谣。");
data.add(map);
map = new HashMap<String, Object>();
map.put("photo", R.drawable.f);
map.put("name", "了然");
map.put("publish", "45分钟前");
map.put("content", "疏于管理,致使网上谣言传播,造成恶劣社会影响。");
data.add(map);
map = new HashMap<String, Object>();
map.put("photo", R.drawable.g);
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;
}
}