依次贴入代码 ItemBean
package pratice.ban.com.daily;
/**
* Created by apple on 16/9/29.
*/
public class ItemBean {
public int ItemImageResId;
public String title;
public String content;
public ItemBean(int itemImageResId, String title, String content) {
ItemImageResId = itemImageResId;
this.title = title;
this.content = content;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getItemImageResId() {
return ItemImageResId;
}
public void setItemImageResId(int itemImageResId) {
ItemImageResId = itemImageResId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
Main
package pratice.ban.com.daily;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by apple on 16/9/29.
*/
public class Main extends Activity {
ListView mListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
List<ItemBean> itemBeen = new ArrayList<>();
for (int i = 0; i < 20; i++) {
itemBeen.add(new ItemBean(
R.drawable.test_icon,
"title" + i,
"content" + i));
}
mListView = (ListView) findViewById(R.id.lv);
mListView.setAdapter(new MainAdapter(this,itemBeen));
}
}
MainAdapter 注释写的很详细。可以看注释。
package pratice.ban.com.daily;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
/**
* Created by apple on 16/9/29.
*/
public class MainAdapter extends BaseAdapter {
/**
* 通常在adapter中对数据进行初始化。
*
* @param list
*/
private List<ItemBean> mList;
private LayoutInflater mInflater;
/**
* 这里传入context来进行初始化LayoutInflater
*
* @param context
* @param list
*/
public MainAdapter(Context context, List<ItemBean> list) {
mList = list;
//初始化LayoutInflater
mInflater = LayoutInflater.from(context);
}
/**
* 返回listview需要显示的数据 的数量 有多少条数据,listview就显示多少条数据
*
* @return
*/
@Override
public int getCount() {
return mList.size();
}
/**
* 索引 所有的数据项都保存在mlist中,这里只要直接拿,就可以得到对应的数据项
*
* @param position
* @return
*/
@Override
public Object getItem(int position) {
return mList.get(position);
}
/**
* 对应的索引项
*
* @param position
* @return
*/
@Override
public long getItemId(int position) {
return position;
}
/**
* 需要返回数据项所显示的内容,首先需要创建一个view。
* layout文件转化为布局通常使用mInflater.inflate(R.layout.item,null);mInflater需要先初始化。
*/
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
//废弃写法
// 由于我们只需要将XML转化为View,并不涉及到具体的布局,所以第二个参数通常设置为null
// View inflate = mInflater.inflate(R.layout.item, null);
// //这里需要注意 我们是在转换完成的(view 这个view叫inflate)里寻找我们控件的id
// ImageView imageView = (ImageView) inflate.findViewById(R.id.iv);
// TextView title = (TextView) inflate.findViewById(R.id.tv_title);
// TextView content = (TextView) inflate.findViewById(R.id.tv_content);
//
// //下边需要把mList的数据取出来然后赋值给对应的控件
ViewHolder viewHolder;
if (convertView == null) { //说明为空,view为被实例化,缓存池中无缓存。
viewHolder = new ViewHolder();
// 由于我们只需要将XML转化为View,并不涉及到具体的布局,所以第二个参数通常设置为null
convertView = mInflater.inflate(R.layout.item, null);
// 这里需要注意 我们是在转换完成的(view 这个view叫convertView)里寻找我们控件的id
viewHolder.mImageView = (ImageView) convertView.findViewById(R.id.iv);
viewHolder.title = (TextView) convertView.findViewById(R.id.tv_title);
viewHolder.content = (TextView) convertView.findViewById(R.id.tv_content);
//通过setTag将viewHolder与convertView绑定
convertView.setTag(viewHolder);
} else {
//通过viewHolder对象找到对应的控件
viewHolder = (ViewHolder) convertView.getTag();
}
// 取出bean对象
ItemBean bean = mList.get(position);
// 设置控件的数据
viewHolder.mImageView.setImageResource(bean.getItemImageResId());
viewHolder.title.setText(bean.getTitle());
viewHolder.content.setText(bean.getContent());
return convertView;
}
/**
* 定义viewholder目的是为了避免重复的findviewById
*/
class ViewHolder {
public ImageView mImageView;
public TextView title;
public TextView content;
}
}
item.xml
<?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="wrap_content"
>
<ImageView
android:id="@+id/iv"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/test_icon"
/>
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_toEndOf="@+id/iv"
android:layout_toRightOf="@+id/iv"
android:gravity="center"
android:text="title"
android:textSize="20dp"
/>
<TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_below="@+id/tv_title"
android:layout_toEndOf="@+id/iv"
android:layout_toRightOf="@+id/iv"
android:gravity="center_vertical"
android:text="content"
android:textSize="20dp"
/>
</RelativeLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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="pratice.ban.com.daily.MainActivity">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</ListView>
</RelativeLayout>