ListView在平时开发中使用的最广泛,应用也是最多的,而使用系统的适配器并不能满足我们多样化的要求,因此本文就自定义适配器展开,下面直接附上我已经测试通过的代码:
MainActivity.java:
package com.example.listviewpushtorefresh;
import java.util.ArrayList;
import java.util.List;
import bean.Person;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(R.id.listview);
MyAdapter adapter = new MyAdapter(MainActivity.this,0,getData());
listview.setAdapter(adapter);
}
private List getData() {
// TODO Auto-generated method stub
List list= new ArrayList();
for(int i=0;i<20;i++)
{
Person p = new Person(R.drawable.ic_launcher,i+"","我是第"+i+"项");
list.add(p);
}
return list;
}
}
自定义数据类型:Person.java
package bean;
public class Person {
private int mPicture;
private String mTitle;
private String mContent;
public Person(int picture,String title,String content){
this.mPicture = picture;
this.mTitle = title;
this.mContent = content;
}
public int getPicture() {
return mPicture;
}
public void setmPicture(int mPicture) {
this.mPicture = mPicture;
}
public String getTitle() {
return mTitle;
}
public void setTitle(String mTitle) {
this.mTitle = mTitle;
}
public String getContent() {
return mContent;
}
public void setContent(String mContent) {
this.mContent = mContent;
}
}
自定义适配器:MyAdapter.java
package com.example.listviewpushtorefresh;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import bean.Person;
public class MyAdapter extends ArrayAdapter {
private int resoureId;
private List objects;
private Context context;
public MyAdapter(Context context, int resourceId, List objects) {
super(context, resourceId, objects);
// TODO Auto-generated constructor stub
this.objects=objects;
this.context=context;
}
private static class ViewHolder
{
ImageView imageView;
TextView title;
TextView content;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return objects.size();
}
@Override
public Person getItem(int position) {
// TODO Auto-generated method stub
return objects.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder viewHolder = null;
if(convertView==null)
{
viewHolder=new ViewHolder();
LayoutInflater mInflater=LayoutInflater.from(context);
convertView = mInflater.inflate(R.layout.item, null);
viewHolder.imageView = (ImageView) convertView.findViewById(R.id.imageview);
viewHolder.title = (TextView) convertView.findViewById(R.id.title);
viewHolder.content = (TextView) convertView.findViewById(R.id.content);
convertView.setTag(viewHolder);
}
else
{
viewHolder = (ViewHolder) convertView.getTag();
}
Person person = objects.get(position);
if(null!=person)
{
viewHolder.imageView.setBackgroundResource(person.getPicture());
viewHolder.title.setText(person.getTitle());
viewHolder.content.setText(person.getContent());
}
return convertView;
}
}
主布局文件: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" >
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listview"
android:layout_margin="2dp"
>
ListView>
RelativeLayout>
item.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#a4d3ee">
<ImageView
android:id="@+id/imageview"
android:layout_width="50dp"
android:layout_weight="0"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
<TextView
android:id="@+id/title"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="title"
android:textSize="20sp"/>
<TextView
android:id="@+id/content"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="content"/>
LinearLayout>
LinearLayout>