自己定制一个LinearLayout,可以让多个应用程序(当然,这几个应用具有相同的布局,但内容不一样)使用,好处是只需要用一个layout文件,里面的内容通过自己的定制的LinearLayout可以实现,具体说就是在构造LinearLayout时传入不同的参数。
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ListItemView extends LinearLayout {
private static final int VIEW_NAME_IN_ITEM = R.id.com_listitem_text_name_id;
private static final int VIEW_COMMENT_IN_ITEM = R.id.com_listitem_text_comment_id;
private static final int VIEW_IMAGE_IN_ITEM = R.id.com_listitem_image_id;
private TextView m_Name;
private TextView m_Comment;
private ImageView m_Image;
public ListItemView(Context context, int LayoutID, String name, String comment, int image_id) {
super(context);
if (initialize(context, LayoutID)) {
SetItem(name, comment, image_id);
}
}
public ListItemView(Context context, int LayoutID, String name, String comment, String imagePath) {
super(context);
if (initialize(context, LayoutID)) {
SetItem(name, comment, imagePath);
}
}
private boolean initialize(Context context, int layoutID) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
if (layoutInflater == null)
return false;
View ItemView = layoutInflater.inflate(layoutID, this);
if (ItemView == null)
return false;
m_Image = (ImageView) ItemView.findViewById(VIEW_IMAGE_IN_ITEM);
m_Name = (TextView) ItemView.findViewById(VIEW_NAME_IN_ITEM);
m_Comment = (TextView) ItemView.findViewById(VIEW_COMMENT_IN_ITEM);
if (m_Image == null || m_Name == null || m_Comment == null)
return false;
m_Name.setTextColor(Color.WHITE);
m_Comment.setTextColor(Color.GRAY);
return true;
}
public void SetItem(String str_name, String str_comment, int iconID) {
m_Name.setText(str_name);
m_Comment.setText(str_comment);
if (iconID > 0)
m_Image.setImageResource(iconID);
}
public void SetItem(String str_name, String str_comment, String iconPath) {
m_Name.setText(str_name);
m_Comment.setText(str_comment);
if (iconPath != null && iconPath.length() != 0){
m_Image.setImageBitmap(BitmapFactory.decodeFile(iconPath));
}else{
m_Image.setImageResource(R.drawable.globalsearchresultitem);
}
}
public void SetEnable(boolean enable) {
if (!enable) {
m_Name.setTextColor(Color.GRAY);
} else {
m_Name.setTextColor(Color.WHITE);
}
}
}
|
这样就有了一个自己定制的view,但是究竟如何使用它呢?我们通过一个自定义的adaptor来解释一下:
public class ListItemAdaptor extends ArrayAdapter<PoiContent > {
private int m_layout_res_id;
public ListItemAdaptor(Context context,
int layout_res_id, int textview_id, List<PoiContent > lstItems)
{
super(context, layout_res_id, textview_id,lstItems);
m_layout_res_id = layout_res_id;
}
public View getView(int position, View convertView, ViewGroup parent) {
ListItemView ItemView;
PoiContent item = getItem(position);
String name = String.format("(%s) ", GetDistanceText(item.GetDistance())) + item.toString();
String desc = ContentDisplay.GetContentDisplayDescription(item);
String icon = mPoiCategoryConfig.GetPoiCategory(item.GetCategoryID()).GetIcon();
if(item ==null) return null;
if (convertView == null) {
ItemView = new ListItemView(getContext(),m_layout_res_id, name, desc, icon);
}
else
{
ItemView = (ListItemView) convertView;
ItemView.SetItem(name, desc, icon);
}
ItemView.setTag(item );
return ItemView;
}
}
|
可以看到,在派生出来的ListItemAdaptor的ListItemAdaptor中,用到了我们前面自定义的ListItemView,并将其return给外界使用,它将成为后面要用到的listView的item的layout。
最后给我们的ListView set 我们定制的ListItemAdaptor:
lstView.setAdapter(new ListItemAdaptor(this, R.layout.searchresultlistviewitem, R.id.com_listitem_text_name_id, lstResultItems));
|
其中用到的list单行的layout如下:
layout.searchresultlistviewitem:<LinearLayout xmlns:android=""
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/com_listitem_group">
<ImageView android:id="@+id/com_listitem_image_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"/>
<LinearLayout
android:orientation="vertical"
android:paddingRight="5dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/com_listitem_text_name_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:editable="false"
android:enabled="true"
android:singleLine="false"
android:textColor="@color/listview_item_title_color"
android:lineSpacingExtra="5dip"
android:textSize="18dip"></TextView>
<TextView android:id="@+id/com_listitem_text_comment_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15dip"
android:editable="false"
android:enabled="true"
android:singleLine="false"
android:textColor="@color/listview_item_description_color">
</TextView>
</LinearLayout>
</LinearLayout>
|
最后再一起理一下整体思路,定制view,定制adaptor,ListView.setAdaptor(new OUR_ADAPTOR);
阅读(4094) | 评论(0) | 转发(0) |