如何添加“最近搜索”的项目列表并编辑自定义AutoCompleteTextView的行项目?

时间:2019-12-02 08:10:50

标签: android android-custom-view autocompletetextview

我具有以下自定义AutoCompleteTextView-

public class ClearableAutoCompleteTextView extends android.support.v7.widget.AppCompatAutoCompleteTextView {

    @Override
    public void setDropDownBackgroundResource(int resId) {
        super.setDropDownBackgroundResource(resId);
    }

    // was the text just cleared?
    boolean justCleared = false;

    // if not set otherwise, the default clear listener clears the text in the
    // text view
    private OnClearListener defaultClearListener = () -> {
        ClearableAutoCompleteTextView et = ClearableAutoCompleteTextView.this;
        et.setText("");
    };

    private OnClearListener onClearListener = defaultClearListener;

    // The image we defined for the clear button
    public Drawable imgClearButton = getResources().getDrawable(R.drawable.bs_ic_clear_light);

    public interface OnClearListener {
        void onClear();
    }

    /* Required methods, not used in this implementation */
    public ClearableAutoCompleteTextView(Context context) {
        super(context);
        init();
    }

    /* Required methods, not used in this implementation */
    public ClearableAutoCompleteTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    /* Required methods, not used in this implementation */
    public ClearableAutoCompleteTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    void init() {

        // if the clear button is pressed, fire up the handler. Otherwise do nothing
        this.setOnTouchListener((v, event) -> {

            ClearableAutoCompleteTextView et = ClearableAutoCompleteTextView.this;

            if (et.getCompoundDrawables()[2] == null)
                return false;

            if (event.getAction() != MotionEvent.ACTION_UP)
                return false;

            if (event.getX() > et.getWidth() - et.getPaddingRight() - imgClearButton.getIntrinsicWidth()) {
                onClearListener.onClear();
                justCleared = true;
            }
            return false;
        });
    }

    public void setImgClearButton(Drawable imgClearButton) {
        this.imgClearButton = imgClearButton;
    }

    public void setOnClearListener(final OnClearListener clearListener) {
        this.onClearListener = clearListener;
    }

    public void hideClearButton() {
        this.setCompoundDrawables(null, null, null, null);
    }

    public void showClearButton() {
        this.setCompoundDrawablesWithIntrinsicBounds(null, null, imgClearButton, null);
    }
}

我正在用字符串适配器填充它。到目前为止一切顺利。

我要寻找的功能如下- *我想为“最近搜索”的项目添加另一个单独的适配器,这意味着当没有输入任何内容时将在列表中显示的项目。我该如何实施? *我想编辑自动完成的行项目,并在其中添加ImageView,并能够从外部更改图像。我该怎么做?

0 个答案:

没有答案