1

我已经实现了一个自定义的自动完成文本视图来允许用户选择标签;当一个项目被选中时,它会正确地调用监听器来设置视图中选择的文本和与之对应的颜色。我遇到的问题是所选项目会立即作为选择之一弹出;我希望该项目消失(并且可能可以通过跟踪按下的项目来做到这一点,但是有没有更清洁的方法可以做到这一点或我错过了什么?

筛选:

    protected class TagFilter extends Filter{
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults results = new FilterResults();
        if(resultTags == null){
            resultTags = new LinkedList<Tag>();
        }
        else {
            resultTags.clear();
        }
        if (constraint != null) {
            String tagString = constraint.toString();
            for (Tag tag : globalTags) {
                if (tag.getText().startsWith(tagString)) {
                    resultTags.add(tag);
                }

            }
            if(resultTags.size() > 1) Collections.sort(resultTags);
        }
        synchronized (this) {
            results.values = resultTags;
            results.count = resultTags.size();
        }
        return results;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        tagAdapter.clear();
        if (results.count > 0) {
            for(Tag tagResult : (List<Tag>)results.values){
                tagAdapter.add(tagResult);
            }
            tagAdapter.notifyDataSetChanged();
        }
        else {
            tagAdapter.notifyDataSetInvalidated();
        }
    }
}

标记适配器代码:

    protected class TagAdapter extends ArrayAdapter<Tag> implements Filterable {
    @Override
    public Filter getFilter() {
        if(tf == null){
            tf = new TagFilter();
        }
        return tf;
    }
    public TagAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView text = new TextView(CreateActivity.this);
        text.setPadding(20, 20, 20, 20);
        text.setTextSize(20);
        text.setBackgroundColor(Color.BLACK);
        text.setTextColor(Color.WHITE);

        //TODO holder here
        Tag tag = getItem(position);
        text.setText("     " + tag.getText(), TextView.BufferType.SPANNABLE);
        ((Spannable)text.getText()).setSpan(
                new BackgroundColorSpan(tag.getColor()), 
                0, 
                4, 
                0);
        return text;
    }       
}

AutoCompleteTextView 监听器:

    tagText.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            Tag selectedTag = resultTags.get((int) arg3);

            selectedColor = selectedTag.getColor();
            tagText.setText(selectedTag.getText());
            tagText.dismissDropDown();
        }


    });

有人有想法吗?

4

0 回答 0