BaseAdapter 基本结构
------public int getCount():适配器中数据集中数据的个数
-----public Object getItem(int position):获取数据集中与指定索引对应的数据项
-----public long getItemId(int position):获取指定行对应的ID
-----public View getView(int position,View convertView,ViewGroup parent):获取每一个Item 的显示内容
下面通过Listview 和BaseAdpter 实现下面功能
首先制作布局文件item.xml
<LinearLayout 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:orientation="vertical"
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=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/iv_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_toEndOf="@+id/iv_image"
android:gravity="center"
android:text="title"
android:textSize="25sp" />
<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_image"
android:gravity="center_vertical"
android:text="content"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
效果图如下
下面用ItemBean.java 类来封装 布局文件
public class ItemBean {
int ItemImageResid;
String ItemTitle;
String ItemContent;
public ItemBean(int ItemImageResid, String ItemTitle, String ItemContent) {
this.ItemImageResid = ItemImageResid;
this.ItemTitle = ItemTitle;
this.ItemContent = ItemContent;
}
}
下面用myBaseAdapter.java类
import java.util.List;
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;
public class myBaseAdpter extends BaseAdapter {
private List<ItemBean> mList;
private LayoutInflater minflater;
public myBaseAdpter(Context context, List<ItemBean> list) {
mList = list;
this.minflater = LayoutInflater.from(context);
// context要使用当前的Adapter的界面对象mInflater布局装载器对象
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mList.size();// 返回ListView 所要显示的数据个数
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return mList.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0; // 返回指定索引对应的数据项
}
@Override
// 返回每一项的显示内容
public View getView(int arg0, View arg1, ViewGroup arg2) {
// 逗比式
//
// View view=minflater.inflate(R.layout.item,null);//R.layout.item
// 是需要装载到item中的布局文件
// ImageView imageView=(ImageView)view.findViewById(R.id.iv_image);
// TextView title=(TextView)view.findViewById(R.id.tv_title);
// TextView content=(TextView)view.findViewById(R.id.tv_content);
// ItemBean bean=mList.get(arg0);
// imageView.setImageResource(bean.ItemImageResid);
// title.setText(bean.ItemTitle);
// content.setText(bean.ItemContent);
// return view;
// 普通式 利用了listview 的缓存机制 如果没有缓存 才创建新的view
// 但是 大量findViewById 依然会浪费时间
// if(arg1==null){
// arg1=minflater.inflate(R.layout.item,null);
// }
// ImageView imageView=(ImageView)arg1.findViewById(R.id.iv_image);
// TextView title=(TextView)arg1.findViewById(R.id.tv_title);
// TextView content=(TextView)arg1.findViewById(R.id.tv_content);
// ItemBean bean=mList.get(arg0);
// imageView.setImageResource(bean.ItemImageResid);
// title.setText(bean.ItemTitle);
// content.setText(bean.ItemContent);
// return arg1;
// 文艺式 利用了listview 的缓存 更痛ViewHolder类实现显示数据的视图缓存 避免多次findViewById 寻找控件
ViewHolder viewHolder;
if (arg1 == null) {
viewHolder = new ViewHolder();
arg1 = minflater.inflate(R.layout.item, null);
viewHolder.imageView = (ImageView) arg1.findViewById(R.id.iv_image);
viewHolder.title = (TextView) arg1.findViewById(R.id.tv_title);
viewHolder.content = (TextView) arg1.findViewById(R.id.tv_content);
arg1.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) arg1.getTag();
}
ItemBean bean = mList.get(arg0);
viewHolder.imageView.setImageResource(bean.ItemImageResid);
viewHolder.title.setText(bean.ItemTitle);
viewHolder.content.setText(bean.ItemContent);
return arg1;
}
class ViewHolder {
public ImageView imageView;
public TextView title;
public TextView content;
}
}
最后 是MainActivity.java
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ListView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<ItemBean> itemBeanList=new ArrayList<ItemBean>();
for(int i=0;i<20;i++){
itemBeanList.add(new ItemBean(R.drawable.ic_launcher, "标题"+i, "内容"+i));
}
ListView listView=(ListView)findViewById(R.id.lv_main);
listView.setAdapter(new myBaseAdpter(this,itemBeanList));
}
@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;
}
}
main_activity.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"
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=".MainActivity" >
<ListView
android:id="@+id/lv_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>