【问题标题】:Is it possible to add a serchable option to an existing custom spinner?是否可以向现有的自定义微调器添加可搜索选项?
【发布时间】:2021-02-25 23:41:08
【问题描述】:

所以基本上标题就说明了。我将尝试将代码放入,如果我能得到答案,那就太好了。

第二个片段。

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_second, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    view.findViewById<Button>(R.id.button_second).setOnClickListener {
        findNavController().navigate(R.id.action_SecondFragment_to_FirstFragment)




            }



    val spinner04 = view.findViewById<AppCompatSpinner>(R.id.spinner04)
    val modelList: List<Model> = readFromAsset()
    val customDropDownAdapter = CustomDropDownAdapter(requireContext(), modelList)
    spinner04.adapter = customDropDownAdapter

    }

private fun readFromAsset(): List<Model> {
    val file_name = "android_version.json"

    val bufferReader = requireActivity().application.assets.open(file_name).bufferedReader()

    val json_string = bufferReader.use {
        it.readText()
    }
    val gson = Gson()
    val modelList: List<Model> = gson.fromJson(json_string, Array<Model>::class.java).toList()
    return modelList
}
}

fragmentsecond.xml 中的微调器

<androidx.appcompat.widget.AppCompatSpinner
    android:id="@+id/spinner04"
    android:layout_width="365dp"
    android:layout_height="69dp"
    android:layout_marginBottom="292dp"
    app:layout_constraintBottom_toTopOf="@+id/button_second"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.531"
    app:layout_constraintStart_toStartOf="parent" />

<TextView
    android:id="@+id/textView"
    android:layout_width="245dp"
    android:layout_height="31dp"
    android:layout_marginTop="172dp"
    android:text="Izaberite zemlju"
    android:textSize="24sp"
    app:layout_constraintBottom_toTopOf="@+id/spinner04"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.614"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.034" />

custom_spinner_item.xml

 <ImageView android:layout_width="40dp"
    android:layout_height="40dp"
    android:id="@+id/img"
    android:contentDescription="@string/app_name"/>

<TextView android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:paddingStart="10dp"
    android:layout_gravity="center_vertical"
    android:id="@+id/text"
    android:paddingLeft="10dp" />

如果需要,我也可以显示 CustomAddapterCode。问题是我正在尝试在微调器中实现搜索选项,但我希望我不需要制作新的微调器。那么有什么建议吗?

You can take a look what i would like is to place that option either at Izaberite zemlju or at the actual Spinner that`s how it looks.

CustomDropDownAdapter

private val inflater: LayoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater

override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {

    val view: View
    val vh: ItemHolder
    if (convertView == null) {
        view = inflater.inflate(R.layout.custom_spinner_item, parent, false)
        vh = ItemHolder(view)
        view?.tag = vh
    } else {
        view = convertView
        vh = view.tag as ItemHolder
    }
    vh.label.text = dataSource.get(position).name

    val id = context.resources.getIdentifier(dataSource.get(position).url, "drawable", context.packageName)
    vh.img.setBackgroundResource(id)

    return view
}

override fun getItem(position: Int): Any? {
    return dataSource[position];
}

override fun getCount(): Int {
    return dataSource.size;
}

override fun getItemId(position: Int): Long {
    return position.toLong();
}

private class ItemHolder(row: View?) {
    val label: TextView
    val img: ImageView

    init {
        label = row?.findViewById(R.id.text) as TextView
        img = row?.findViewById(R.id.img) as ImageView
    }
}

}

【问题讨论】:

    标签: android search spinner


    【解决方案1】:

    将微调器转换为带有列表的对话框,并在其中实现带有适配器的搜索

    【讨论】:

    • 如果方便的话,有什么方法可以给我发个代码吗?如果没有,我会尝试用谷歌搜索它。
    • 我将微调器设置为对话框,告诉我是否应该在现有的 CustomDropDownAdapter 中添加搜索选项?不知道我是否可以从这里发送代码。
    • 将数组设为公开的,即您在适配器中添加的数组并添加到对话框编辑文本中 searchEditText.addTextChangedListener(new TextWatcher() @Override public void afterTextChanged(final Editable editable) { 在此处添加方法现在根据可编辑的 if (locationModelArrayListMainItems.get(x).getName().toLowerCase().startsWith(editable.toString().toLowerCase())) { 过滤值然后添加到新列表并更新您的适配器跨度>
    • 看看一些例子,不确定我明白了。
    猜你喜欢
    • 2018-12-14
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    • 2015-12-30
    • 2012-05-05
    • 2010-12-07
    • 1970-01-01
    相关资源
    最近更新 更多