BaseAdapter的使用在Android开发中是十分频繁的,下面介绍Listview中如何使用BaseAdapter(下面是最终实现的界面)
1、首先附上布局文件notice_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"
android:gravity="center_vertical"
android:orientation="vertical" >
<View
android:id="@+id/re1"
android:layout_width="match_parent"
android:layout_height="4dp"
android:background="#ffffff" />
<RelativeLayout
android:id="@+id/relative1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/re1">
<ImageView
android:id="@+id/my_imageView"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/my_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/my_imageView"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/my_imageView"
android:text="title Text"
android:textColor="#000000"
android:textSize="25sp" />
<TextView
android:id="@+id/my_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/my_imageView"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/my_imageView"
android:text=" content Text"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="@+id/publish_man"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/line1"
android:layout_marginLeft="10dp"
android:singleLine="true"
android:text="发布人"
android:textColor="#000000"
android:textSize="15sp" />
<TextView
android:id="@+id/publish_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/line1"
android:layout_marginRight="10dp"
android:singleLine="true"
android:text="发布时间"
android:textColor="#000000"
android:textSize="15sp" />
<LinearLayout
android:id="@+id/line1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/my_content"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/content_img"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/ic_launcher"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
2、下面是BaseAdapterd的相关代码直接附上NoticeAdapter.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;
import com.smart.xyb.base.R;
import com.smart.xyb.base.data.NoticeIfo;
import com.smart.xyb.base.view.ImgDownLoad;
public class NoticeAdapter extends BaseAdapter {
private List<NoticeIfo>mList;
private LayoutInflater inflater;
Context context;
public NoticeAdapter(Context context,List<NoticeIfo> mlist) {
// TODO Auto-generated constructor stub
this.mList=mlist;
this.context=context;
inflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mList.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) {
Viewholder viewholder;
if (convertView==null) {
viewholder=new Viewholder();
convertView=inflater.inflate(R.layout.notice_item, null);
viewholder.publish_man_head=(ImageView) convertView.findViewById(R.id.my_imageView);
viewholder.publish_imageView=(ImageView) convertView.findViewById(R.id.content_img);
viewholder.title=(TextView) convertView.findViewById(R.id.my_title);
viewholder.content=(TextView) convertView.findViewById(R.id.my_content);
viewholder.publish_man=(TextView) convertView.findViewById(R.id.publish_man);
viewholder.publish_time=(TextView) convertView.findViewById(R.id.publish_time);
convertView.setTag(viewholder);
}else {
viewholder=(Viewholder) convertView.getTag();
}
String headurl=mList.get(position).publish_man_head;
String contenturl=mList.get(position).content_img;
viewholder.publish_man_head.setTag(headurl);
viewholder.publish_imageView.setTag(contenturl);
viewholder.title.setText(mList.get(position).title);
viewholder.content.setText(mList.get(position).content);
viewholder.publish_man.setText("发布人:"+mList.get(position).publish_man);
viewholder.publish_time.setText("发布时间:"+mList.get(position).publish_time);
return convertView;
}
class Viewholder{
public ImageView publish_man_head;
public ImageView publish_imageView;
public TextView title;
public TextView content;
public TextView publish_man;
public TextView publish_time;
}
}
3、实体类NoticeIfo.java
public class NoticeIfo {
public String publish_man_head;
public String content_img;
public String title;
public String content;
public String publish_man;
public String publish_time;
}
4、总结
注意:使用Viewholder可以极大的优化项目