pull/34/head
kunfei 5 years ago
parent 02462cce82
commit 06e7e5027b
  1. 49
      app/src/main/java/io/legado/app/lib/theme/view/ATEAutoCompleteTextView.kt
  2. 23
      app/src/main/res/layout/item_1line_text_and_del.xml

@ -5,14 +5,26 @@ import android.graphics.Rect
import android.os.Build import android.os.Build
import android.util.AttributeSet import android.util.AttributeSet
import android.view.KeyEvent 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 androidx.appcompat.widget.AppCompatAutoCompleteTextView
import io.legado.app.R
import io.legado.app.lib.theme.Selector import io.legado.app.lib.theme.Selector
import io.legado.app.lib.theme.ThemeStore 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) : class ATEAutoCompleteTextView(context: Context, attrs: AttributeSet) :
AppCompatAutoCompleteTextView(context, attrs) { AppCompatAutoCompleteTextView(context, attrs) {
var callBack: CallBack? = null
var showDel: Boolean = false
init { init {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
backgroundTintList = Selector.colorBuild() backgroundTintList = Selector.colorBuild()
@ -29,6 +41,43 @@ class ATEAutoCompleteTextView(context: Context, attrs: AttributeSet) :
override fun onFocusChanged(focused: Boolean, direction: Int, previouslyFocusedRect: Rect?) { override fun onFocusChanged(focused: Boolean, direction: Int, previouslyFocusedRect: Rect?) {
super.onFocusChanged(focused, direction, previouslyFocusedRect) super.onFocusChanged(focused, direction, previouslyFocusedRect)
performFiltering()
}
fun performFiltering() {
performFiltering(text, KeyEvent.KEYCODE_UNKNOWN) performFiltering(text, KeyEvent.KEYCODE_UNKNOWN)
} }
fun setSelectValues(values: List<String>, 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<String>) :
ArrayAdapter<String>(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)
}
} }

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:ignore="UseCompoundDrawables">
<TextView
android:id="@+id/text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<ImageView
android:id="@+id/iv_delete"
android:layout_width="24dp"
android:layout_height="24dp"
android:src="@drawable/ic_clear_all"
android:visibility="gone"
android:contentDescription="@string/delete" />
</LinearLayout>
Loading…
Cancel
Save