package com.example.mylistview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
private int cnt = 0;
private MyAdspter adapter;
private Handler handler;
private List<Map<String, Object>> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.list);
list = new ArrayList<Map<String, Object>>();
handler = new Handler();
adapter = new MyAdspter(this, list);
listView.setAdapter(adapter);
test();
}
private void test(){
new Timer().schedule(new TimerTask() {
public void run() {
cnt++;
if(cnt <10){
getData(cnt);
Runnable updater = new Runnable() {
public void run() {
((BaseAdapter) adapter).notifyDataSetChanged();
}
};
handler.post(updater);
}
};
}, 0, 3000);
}
public void getData(int i) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("image", R.drawable.ic_launcher);
map.put("title", "这是标题" + i);
map.put("info", "这是一个详细详细信息" + i);
list.add(map);
}
}
class MyAdspter extends BaseAdapter {
private List<Map<String, Object>> data;
private LayoutInflater layoutInflater;
public MyAdspter(Context context, List<Map<String, Object>> data) {
this.data = data;
this.layoutInflater = LayoutInflater.from(context);
}
public final class ComponentView {
public ImageView image;
public TextView title;
public Button view;
public TextView info;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ComponentView cv = null;
if (convertView == null) {
cv = new ComponentView();
convertView = layoutInflater.inflate(R.layout.list, null);
cv.image = (ImageView) convertView.findViewById(R.id.image);
cv.title = (TextView) convertView.findViewById(R.id.title);
cv.view = (Button) convertView.findViewById(R.id.view);
cv.info = (TextView) convertView.findViewById(R.id.info);
convertView.setTag(cv);
} else {
cv = (ComponentView) convertView.getTag();
}
cv.image.setBackgroundResource((Integer) data.get(position).get("image"));
cv.title.setText((String) data.get(position).get("title"));
cv.info.setText((String) data.get(position).get("info"));
return convertView;
}
}
布局:
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.mylistview.MainActivity" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
</RelativeLayout>
列表项布局:
<?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:background="#ffffff"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/image"
android:layout_margin="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="5dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#666872" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#666872"
android:textSize="10dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right" >
<Button
android:id="@+id/view"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_centerVertical="true"
android:layout_marginTop="7dp"
android:layout_marginRight="8dp"
android:text="详细" />
</LinearLayout>
</LinearLayout>
</LinearLayout>效果:
转载请注明原文地址: https://ju.6miu.com/read-114303.html