如何在Fragment中重写OnItemSelected方法

时间:2018-10-09 17:19:16

标签: android android-fragments kotlin

我试图以相同方式覆盖片段 Spinner OnItemSelected 方法我在按钮上使用了 OnClick ,但是它不再响应

在OnCreate内,注释的块可以完美地工作,但是我想对其进行清理,因为在此布局中我有多个旋转器,因此它变得非常污染。

另一个问题:如果可能,此 OnNothingSelected 方法如何工作以及如何更有效地使用它?

在表达式“ if(itemSelected!=” ...“)”中,是因为微调器的第一个元素是带有“ ....”的字符串,以便不要在开头选择任何项,我正在使用这种出色的技术...该如何改善呢?

我的代码如下:

Vetarano2.kt

com.mtsa.escudeiro_rpghelper.fragments

import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.Button
import android.widget.Spinner
import android.widget.Toast
import com.mtsa.escudeiro_rpghelper.R

class Veterano2 : Fragment(), View.OnClickListener, AdapterView.OnItemSelectedListener {

    private lateinit var spinner: Spinner
    private lateinit var button: Button

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {

        // Inflate the layout for this fragment
        val fragView = inflater.inflate(R.layout.fragment_veterano2, container, false)


        initViews(fragView)
        initListeners()

        // SPINNER RAÇA
        /*
        spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
                val selecionado = parent.getItemAtPosition(position) as String
                Toast.makeText(context, "Opção escolhida: $selecionado", Toast.LENGTH_SHORT).show()
            }
            override fun onNothingSelected(parent: AdapterView<*>) {}
        }
        */

        return fragView
    }

    private fun initViews(v: View) {
        spinner = v.findViewById(R.id.spinner2)
        button = v.findViewById(R.id.button2)
    }

    private fun initListeners() {
        spinner.onItemSelectedListener = this
        button.setOnClickListener(this)
    }

    override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
        when (view?.id) {
            R.id.spinner2 -> {
                val selecionado = parent?.getItemAtPosition(position) as String
                Toast.makeText(context, "Opção escolhida: $selecionado", Toast.LENGTH_SHORT).show()
            }
        }
    }

    override fun onNothingSelected(parent: AdapterView<*>?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.button2 -> {
                Toast.makeText(context, "SALVAR", Toast.LENGTH_SHORT).show()
            }
        }
    }
}

fragment_veterano2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="64dp">

    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/array_example"
        android:spinnerMode="dropdown" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="64dp"
        android:text="Button" />
</LinearLayout>

arrays_diversos.xml

<resources>
    <string-array name="array_example">
        <item>AAAAA</item>
        <item>BBBBB</item>
        <item>CCCCC</item>
        <item>DDDDD</item>
        <item>EEEEE</item>
    </string-array>
</resources>

非常感谢!

编辑:重新格式化代码以实现更好的可视化,但问题仍然存在

1 个答案:

答案 0 :(得分:0)

正如doc所说,如果您有多个微调框,则应像下面那样判断parent的ID,对不起,我只知道Java。

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        int iid = parent.getId();
        switch (iid) {
            case R.id.spinner1:
                Toast.makeText(getActivity(), "hello,spinner1", Toast.LENGTH_SHORT).show();
                break;
            case R.id.spinner2:
                Toast.makeText(getActivity(), "hello,spinner2", Toast.LENGTH_SHORT).show();
                break;
            break; 
            default:
                break;
        }

    }