@zhutoulwz
2015-03-26T23:44:05.000000Z
字数 4962
阅读 2293
android
首先需要自定义一个adapter
类,继承BaseAdapter
,具体如下:
class ViewHolder
{
public ImageView imageView;
public TextView textView;
public Button button;
}
class MyAdapter extends BaseAdapter
{
private LayoutInflater inflater;
public MyAdapter(Context context)
{
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int location) {
return data.get(location);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null)
{
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.list_item, null);
holder.imageView = (ImageView) convertView.findViewById(R.id.img);
holder.textView = (TextView) convertView.findViewById(R.id.text);
holder.button = (Button) convertView.findViewById(R.id.button);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.imageView.setImageResource((Integer)data.get(position).get("img"));
holder.textView.setText(data.get(position).get("info").toString());
final int p = position;
holder.button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), String.valueOf(p), Toast.LENGTH_LONG).show();
}
});
return convertView;
}
}
然后,需要自定义一个xml文件,即list_item
的布局文件,如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
android:clickable="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dip"
android:orientation="horizontal"
android:layout_alignParentLeft="true"
android:gravity="center_vertical">
<ImageView
android:id="@+id/img"
android:layout_width="30dip"
android:layout_height="30dip"
android:scaleType="centerInside"
android:src="@drawable/ic_launcher"
/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="hello"
android:textSize="16sp"
android:textColor="#ccc"
android:gravity="center_vertical"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dip"/>
</LinearLayout>
<Button
android:id="@+id/button"
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_alignParentRight="true"/>
</RelativeLayout>
整个完整代码如下:
package com.lwz.activity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.testandroid.R;
public class MainActivity extends Activity {
private ListView list;
private List<Map<String, Object>> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
public void init()
{
list = (ListView) findViewById(R.id.list1);
// set data
data = new ArrayList<Map<String,Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("img", R.drawable.ic_launcher);
map.put("info", "hello");
data.add(map);
MyAdapter adapter = new MyAdapter(this);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(getApplicationContext(), "click" + arg2, Toast.LENGTH_LONG).show();
}
});
}
class ViewHolder
{
public ImageView imageView;
public TextView textView;
public Button button;
}
class MyAdapter extends BaseAdapter
{
private LayoutInflater inflater;
public MyAdapter(Context context)
{
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int location) {
return data.get(location);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null)
{
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.list_item, null);
holder.imageView = (ImageView) convertView.findViewById(R.id.img);
holder.textView = (TextView) convertView.findViewById(R.id.text);
holder.button = (Button) convertView.findViewById(R.id.button);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.imageView.setImageResource((Integer)data.get(position).get("img"));
holder.textView.setText(data.get(position).get("info").toString());
final int p = position;
holder.button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), String.valueOf(p), Toast.LENGTH_LONG).show();
}
});
return convertView;
}
}
}
注:这是自己的笔记,仅供参考