我正在尝试通过 setListAdapter 和 Efficient Adapter 来实现 listView。我希望当列表显示时背景不应该重复。我的代码重复了 list.xml 的整个布局,因此我的列表项显示的差距很大。
现在我的清单是这样工作的:
但我想要这种类型的视图:
这是我的editText.xml,我在其中输入单词并打开列表视图。
<EditText
android:id="@+id/start_edit"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="top|left"
android:ems="10"
android:hint="Type to search"
android:paddingLeft="50dp" >
<requestFocus />
</EditText>
此布局适用于 list.xml :
<RelativeLayout
android:id="@+id/RelativeLayout_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/blue_cellbar" >
list.xml 文件在 Effecient 适配器中重复布局:
这是我的代码:
listAdapter = new EfficientAdapter2(this);
setListAdapter(listAdapter);
public static class viewHolder2 {
TextView word;
TextView meaning;
ImageView image;
ImageView image_color;
RelativeLayout cell;
}
private class EfficientAdapter2 extends BaseAdapter implements Filterable,OnItemClickListener {
private Context context;
LayoutInflater inflater;
public EfficientAdapter2(Context context) {
this.context = context;
inflater = LayoutInflater.from(context);
}
public int getCount() {
// if(SearchWordString.isEmpty()==false)
// {
return SearchWordString.size();
/// }
//return 0;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
viewHolder2 holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list, null);
ViewToUse=parent;
holder = new viewHolder2();
// Log.i("View","is Null");
convertView.setTag(holder);
} else {
//Log.i("View","is not Null");
holder = (viewHolder2) convertView.getTag();
}
holder.cell = (RelativeLayout) convertView
.findViewById(R.id.RelativeLayout_list);
return convertView;
}
更新
public class Start extends ListActivity implements OnTouchListener,
android.view.GestureDetector.OnGestureListener {
// /////////////////////////////////////// //// /************* 内部类视图 ****************/ // ////////// ////////////////////////////////// onCreate() { ListView list_to_use = getListView (); listAdapter = new EfficientAdapter2(this); list_to_use.setAdapter(listAdapter); list_to_use.setBackgroundColor(2); viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper1);
search = (EditText) findViewById(R.id.start_edit);
search.addTextChangedListener(myTextWatcher);
} 公共静态类 viewHolder2 { TextView 字;TextView 含义;ImageView 图像;ImageView 图像颜色;相对布局单元格;
}
// ////////////////////////////////////////////////////
/*********** INNER CLASS EfficientAdapter ************/
// ////////////////////////////////////////////////////
private class EfficientAdapter2 extends BaseAdapter implements Filterable,OnItemClickListener {
private Context context;
LayoutInflater inflater;
public EfficientAdapter2(Context context) {
this.context = context;
inflater = LayoutInflater.from(context);
}
public int getCount() {
// if(SearchWordString.isEmpty()==false)
// {
return SearchWordString.size();
/// }
//return 0;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
viewHolder2 holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_search_item, null);
ViewToUse=parent;
holder = new viewHolder2();
// Log.i("View","is Null");
convertView.setTag(holder);
holder.word = (TextView) convertView.findViewById(R.id.title_list);
holder.meaning = (TextView) convertView
.findViewById(R.id.textView_meaning_list);
holder.image = (ImageView) convertView
.findViewById(R.id.image_list);
holder.image_color = (ImageView) convertView
.findViewById(R.id.imageView_color_list);
holder.cell = (RelativeLayout) convertView
.findViewById(R.id.RelativeLayout_list);
} else {
//Log.i("View","is not Null");
holder = (viewHolder2) convertView.getTag();
}
} }
主.xml:
<?xml version="1.0" encoding="utf-8"?>
<EditText
android:id="@+id/start_edit"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="top|left"
android:ems="10"
android:hint="Type to search"
android:paddingLeft="50dp" >
<requestFocus />
</EditText>
<ViewFlipper
android:id="@+id/viewFlipper1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top|left"
android:layout_marginTop="50dp" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/blue_home"
android:fastScrollEnabled="true"
android:smoothScrollbar="true"
android:divider="@drawable/blue_dic"
android:dividerHeight="250sp" >
</ListView>
</FrameLayout>
</ViewFlipper>
listView 的行:
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:id="@+id/image_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@drawable/abacus_thumbnail"
android:scaleType="centerCrop" />
<ImageView
android:id="@+id/imageView_color_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@drawable/blue_thumbnail" />
<TextView
android:id="@+id/title_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/image_list"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@+id/image_list"
android:gravity="center"
android:text="Abacus"
android:textColor="#000000"
android:textSize="30sp"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="@+id/textView_meaning_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/title_list"
android:layout_below="@+id/title_list"
android:layout_marginTop="10dp"
android:text="TextView"
android:textColor="#000000"
android:textSize="25sp" />