“Spinner already Selected Item Selection event” can be translated as: “已选择的项选取事件”

huangapple go评论80阅读模式
英文:

Spinner already Selected Item Selection event

问题

场景:
我正在使用 Spinner 来选择日期范围,这个功能运行得很好。问题是,有一个选项叫做“自定义范围”,我需要在这个选项上打开一个自定义日期范围选择器。虽然第一次使用时它运行得很好。

问题是:
当用户再次点击它时,不会调用 onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) 方法(这是 Spinner 的功能)。但从用户的角度来看,这似乎是个错误。

有没有办法在已选中的 Spinner 项上获得任何选择事件?

英文:

Scenario:
I am using Spinner for date range selection, which is working perfectly. The issue is, there is an option "Custom Range" on which I have to open a custom date range picker. Although it is working fine for the first time.

“Spinner already Selected Item Selection event” can be translated as:
“已选择的项选取事件”

“Spinner already Selected Item Selection event” can be translated as:
“已选择的项选取事件”

The issue is:
When users click it again, it doesn't call onItemSelected(parent: AdapterView&lt;*&gt;?, view: View?, position: Int, id: Long) (which is a functionality of Spinner). But from the user point of view, it's a bug.

Is there any way, we can get any selection event on an already selected item of Spinner?

答案1

得分: 4

要在已选择的Spinner项上调用事件,您可以按照以下方式进行操作:

  1. 通过扩展AppCompatSpinner创建您自己的Spinner类:
import android.content.Context
import android.util.AttributeSet
import android.util.Log

class MySpinner(context: Context, attrs: AttributeSet?) : androidx.appcompat.widget.AppCompatSpinner(
    context,
    attrs
) {
    var listener: OnItemSelectedListener? = null

    override fun setSelection(position: Int) {
        super.setSelection(position)
        if (position == selectedItemPosition) {
            listener!!.onItemSelected(this, selectedView, position, selectedItemId)
        }
    }

    override fun setOnItemSelectedListener(listener: OnItemSelectedListener?) {
        this.listener = listener
    }
}
  1. 在您的布局中使用它:
<com.yourcompany.kotlintest.MySpinner
    android:id="@+id/spinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
  1. 在创建布局时执行以下操作:
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val spinner = findViewById<MySpinner>(R.id.spinner)

        spinner.adapter = ArrayAdapter(
            this,
            android.R.layout.simple_spinner_dropdown_item,
            arrayListOf("One", "Two", "Three")
        )

        spinner.onItemSelectedListener = object :
            AdapterView.OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>,
                view: View, position: Int, id: Long
            ) {
                Log.d("MyTag", "Click item at pos $position")
            }

            override fun onNothingSelected(parent: AdapterView<*>) {
                Log.d("MyTag", "Nothing selected")
            }
        }
    }
}

现在,当您重新选择相同的项时,函数将被执行。

英文:

To call an event on the already selected item in Spinner You can do it in this way:

  1. Create Your own Spinner class by extending AppCompatSpinner:
import android.content.Context
import android.util.AttributeSet
import android.util.Log

class MySpinner(context: Context, attrs: AttributeSet?) : androidx.appcompat.widget.AppCompatSpinner(
    context,
    attrs
)
{
    var listener: OnItemSelectedListener? = null

    override fun setSelection(position: Int)
    {
        super.setSelection(position)
        if (position == selectedItemPosition)
        {
            listener!!.onItemSelected(this, selectedView, position, selectedItemId)
        }
    }

    override fun setOnItemSelectedListener(listener: OnItemSelectedListener?)
    {
        this.listener = listener
    }
}
  1. Use it in Your layout:
&lt;com.yourcompany.kotlintest.MySpinner
    android:id=&quot;@+id/spinner&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    /&gt;
  1. When creating a layout do this:
class MainActivity : AppCompatActivity()
{
    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val spinner = findViewById&lt;MySpinner&gt;(R.id.spinner)

        spinner.adapter = ArrayAdapter(
            this,
            android.R.layout.simple_spinner_dropdown_item,
            arrayListOf(&quot;One&quot;, &quot;Two&quot;, &quot;Three&quot;)
        )

        spinner.onItemSelectedListener = object :
                AdapterView.OnItemSelectedListener
        {
            override fun onItemSelected(
                parent: AdapterView&lt;*&gt;,
                view: View, position: Int, id: Long
            )
            {
                Log.d(&quot;MyTag&quot;, &quot;Click item at pos $position&quot;)
            }

            override fun onNothingSelected(parent: AdapterView&lt;*&gt;)
            {
                Log.d(&quot;MyTag&quot;, &quot;Nothing selected&quot;)
            }
        }
    }
}

Now when You reselect the same item function will be executed

答案2

得分: -1

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView parentView, View selectedItemView, int position, long id) {
// 在这里编写处理选择的代码,使用选定的项目位置
}

@Override
public void onNothingSelected(AdapterView<?> parentView) {
    // 在这里编写处理未选择项的代码
}

});

英文:

did you use

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView&lt;?&gt; parentView, View selectedItemView, int position, long id) {
        // your code here with selected **item position**
    }

    @Override
    public void onNothingSelected(AdapterView&lt;?&gt; parentView) {
        // your code here
    }

});

huangapple
  • 本文由 发表于 2020年10月27日 16:29:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/64550613.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定