安卓:微调下拉与顶部位置选择的项目位置、项目

2023-09-07 23:40:16 作者:對不起"該用戶已成仙

我有微调Android中的一个问题。从下拉列表中选择一个项目将调整的偏移量下拉列表中打开它的下一次。因此,例如,如果我在500项下拉列表中选择项目100,下一次我打开下拉列表中,项目100将在列表的顶部。这就是我想要的行为。

I have an issue with the Spinner in Android. Selecting an item from the dropdown will adjust the offset of that dropdown the next time it is opened. So for example if I choose item 100 in a 500 item dropdown, the next time I open the dropdown, item 100 will be at the top of the list. This is the behaviour I want.

有似乎是一个问题,当我打电话结合选择功能 setSelection(INT)。下面的步骤我似乎已经打破上下拉纺纱偏移系统。

There seems to be an issue when I combine the selector functionality with calling setSelection(int). With the following steps I seem to have broken the offset system on dropdown spinners.

打开微调,选择第二项。 再次打开微调,这一次关闭它没有做出任何选择。 呼叫 setSelection(INT)上的微调与值大于2。打开微调第三次。注意,该偏移是相同的步骤1中后面 Open the Spinner and select the second item. Open the Spinner again and this time dismiss it without selecting anything. Call setSelection(int) on the Spinner with a value greater than 2. Open the Spinner a third time. Note that the offset is the same as back in Step 1.

我已经看到了在微调和适配器视图的code,但我看不到,我已经错过了什么公开呼吁。这是微调或错误在我的code错误?

I've taken a look at the code in Spinner and AdapterView, but I can't see anything public calls that I've missed. Is this a bug in Spinner or a bug in my code?

推荐答案

你有没有尝试公共无效setSelection(INT位置,布尔动画)?我还没有尝试过,但我想通过真正作为第二个参数应该在列表中滚动到该位置。另一种方法是计算滚动偏移量(项目高x选定的项目位置),并调用 setDropDownVerticalOffset

Did you try public void setSelection (int position, boolean animate)? I haven't tried it, but I think passing true as the second parameter should make the list scroll to the selected position. The other alternative is to calculate the scroll offset (item height x selected item position) and call setDropDownVerticalOffset.

更新:我试图改变在API演示微调的例子使用 setSelection(7,真)并按照当它似乎工作你在你的问题提供了4个步骤。我只是增加了一个处理程序,并修改 showToast 如下:

Update: I tried modifying the Spinner example in API demos to use setSelection(7, true) and it seems to work when following the 4 steps you provided in your question. I just added a Handler and modified showToast as follows:

private final Handler handler = new Handler();

void showToast(CharSequence msg) {
    Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    handler.postDelayed(new Runnable(){
        public void run() {
            Toast.makeText(Spinner1.this, "auto setting", Toast.LENGTH_SHORT).show();
            Spinner s2 = (Spinner) findViewById(R.id.spinner2);
            s2.setSelection(7, true);
        }
    }, 5000);
}

我测试如下:

打开第二微调采摘维纳斯(第二选择)。打开第二微调,那么preSS回解雇 5秒钟,postDelayed调用导致海王星(第七选择)后,选择打开微调和偏移正确