我正在尝试通过setListAdapter和Efficient Adapter实现listView。我希望当列表显示时,背景不应该重复。我的代码重复了list.xml的整个布局,因为我的列表项显示了这么大的差距。
现在我的列表就是这样:
但我想要这种观点:
这是我的editText.xml,其中我输入了单词并打开了一个列表View。
<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 adapter中重复布局:
这是我的代码:
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);
} public static class viewHolder2 { TextView字; TextView含义; ImageView图像; ImageView image_color; RelativeLayout单元格;
}
// ////////////////////////////////////////////////////
/*********** 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();
}
} }
main.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" />
答案 0 :(得分:3)
我希望您在android:dividerHeight
下的布局文件中未提供<ListView/>
属性
如果是这样,请将其删除。
答案 1 :(得分:0)
当您设置了适配器类xml(父根)时,会出现此问题 match_content而不是wrap_content。 设置根内容的高度wrap_content
As i can see in adapter layout there is no parent root like relative, linearlayout, Framelayout and ConstraintLayout etc.