diff --git a/app/src/main/java/io/legado/app/lib/theme/view/ATEAutoCompleteTextView.kt b/app/src/main/java/io/legado/app/lib/theme/view/ATEAutoCompleteTextView.kt index b5d7337ff..b42587aae 100644 --- a/app/src/main/java/io/legado/app/lib/theme/view/ATEAutoCompleteTextView.kt +++ b/app/src/main/java/io/legado/app/lib/theme/view/ATEAutoCompleteTextView.kt @@ -5,14 +5,26 @@ import android.graphics.Rect import android.os.Build import android.util.AttributeSet import android.view.KeyEvent +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.ArrayAdapter import androidx.appcompat.widget.AppCompatAutoCompleteTextView +import io.legado.app.R import io.legado.app.lib.theme.Selector import io.legado.app.lib.theme.ThemeStore +import io.legado.app.utils.gone +import io.legado.app.utils.visible +import kotlinx.android.synthetic.main.item_1line_text_and_del.view.* +import org.jetbrains.anko.sdk27.listeners.onClick class ATEAutoCompleteTextView(context: Context, attrs: AttributeSet) : AppCompatAutoCompleteTextView(context, attrs) { + var callBack: CallBack? = null + var showDel: Boolean = false + init { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { backgroundTintList = Selector.colorBuild() @@ -29,6 +41,43 @@ class ATEAutoCompleteTextView(context: Context, attrs: AttributeSet) : override fun onFocusChanged(focused: Boolean, direction: Int, previouslyFocusedRect: Rect?) { super.onFocusChanged(focused, direction, previouslyFocusedRect) + performFiltering() + } + + fun performFiltering() { performFiltering(text, KeyEvent.KEYCODE_UNKNOWN) } + + fun setSelectValues(values: List, showDel: Boolean = false) { + this.showDel = showDel + setAdapter(MyAdapter(context, values)) + } + + fun setSelectValues(vararg value: String, showDel: Boolean = false) { + this.showDel = showDel + setAdapter(MyAdapter(context, value.toMutableList())) + } + + inner class MyAdapter(context: Context, values: List) : + ArrayAdapter(context, android.R.layout.simple_dropdown_item_1line, values) { + + override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { + val view = convertView ?: LayoutInflater.from(context) + .inflate(R.layout.item_1line_text_and_del, parent, false) + view.text_view.text = getItem(position) + if (showDel) view.iv_delete.visible() else view.iv_delete.gone() + view.iv_delete.onClick { + getItem(position)?.let { + remove(it) + callBack?.delete(it) + performFiltering() + } + } + return view + } + } + + interface CallBack { + fun delete(value: String) + } } diff --git a/app/src/main/res/layout/item_1line_text_and_del.xml b/app/src/main/res/layout/item_1line_text_and_del.xml new file mode 100644 index 000000000..cb08c99f8 --- /dev/null +++ b/app/src/main/res/layout/item_1line_text_and_del.xml @@ -0,0 +1,23 @@ + + + + + + + + \ No newline at end of file