我有一个班级:
public class MultiObjectSelectorView extends LinearLayout {
private List<String> items;
private List<String> default_items;
private List<String> display_items;
private EditText text_feild;
private ListView drop_down;
public MultiObjectSelectorView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.multiobjectselectorlayout, this, true);
drop_down = (ListView) findViewById(R.id.drop_down);
text_feild = (EditText) findViewById(R.id.MyTextView);
display_items = new ArrayList<String>();
final ListAdapter adapter = new ListAdapter(context,display_items);
drop_down.setAdapter(adapter);
text_feild.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before,int count) {
if(!s.toString().equals("")){
display_items = new ArrayList<String>();
String str = s.toString();
for(int i = 0;i<items.size();i++){
String[] splited = items.get(i).split("\\s+");
int isFound = 0;
for(int x = 0;x<splited.length;x++){
if(splited[x].startsWith(str)){
isFound = 1;
x = splited.length;
}
}
if(isFound == 1){
display_items.add(items.get(i));
}
}
}else{
display_items = new ArrayList<String>();
}
adapter.setListData(display_items);
adapter.notifyDataSetChanged();
}
public void afterTextChanged(Editable s) {
}
});
drop_down.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
}
});
}
public void SetList(String[] list){
items = new ArrayList<String>();
for(int i = 0;i<list.length;i++){
items.add(list[i]);
}
default_items = items;
}
public void SetList(String[] list,String[] list2){
items = new ArrayList<String>();
default_items = new ArrayList<String>();
for(int i = 0;i<list.length;i++){
items.add(list[i]);
}
for(int i = 0;i<list2.length;i++){
default_items.add(list2[i]);
}
}
public void SetList(String[] list,String[] list2,String[] list3){
items = new ArrayList<String>();
default_items = new ArrayList<String>();
for(int i = 0;i<list.length;i++){
items.add(list[i]);
}
for(int i = 0;i<list2.length;i++){
default_items.add(list2[i]);
}
for(int i = 0;i<list3.length;i++){
items.add(list3[i]);
}
}
private class ListAdapter extends ArrayAdapter<String> {
private final Context context;
private List<String> values;
public ListAdapter(Context context, List<String> values) {
super(context, R.layout.dropdownlayout, values);
this.context = context;
this.values = values;
}
public void setListData(List<String> values2){
values = values2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.dropdownlayout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.drop_down_text_view);
textView.setText(values.get(position));
return rowView;
}
@Override
public int getCount() {
return values.size();
}
@Override
public String getItem(int position) {
return values.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
}
}
我不使用MultiAutoCompleteTextView的原因是(1)。我希望下拉物品,(2)。下拉菜单不会被用作自动完成列表,它会在下拉视图中提供自定义建议(他们还没有做任何事情,但最终他们会将点击的内容添加到列表中)。 问题是列表视图在展开时会推送自定义视图组下方的所有内容。我希望它覆盖其他所有内容。
还有布局文件:
sion="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/MyTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/deit_restr_text"
android:ems="10" >
</EditText>
<ListView
android:id="@+id/drop_down"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>