以编程方式设置微调器文本颜色滞后,速度慢,瞬间颜色错误

Setting spinner text color programmatically lags, is slow, is wrong color for split second

TLDR: 我的微调器瞬间显示了错误的颜色。

我的微调器有问题。每当我 运行 应用程序时,如果 activity 没有缓存在内存中,它有时会滞后。在我可以将其设置为正确的颜色之前,文本是默认颜色(如黑色)。看起来真的很不专业。

视频: 请观看此屏幕录像以了解实际效果: https://drive.google.com/file/d/0By2AG5yaBEhMRnRsbVBDU251STQ/view

它如何在加载页面时寻找一瞬间:

滞后时间后的样子(以及从一开始应该看起来的样子):

代码:

public class MyActivity extends AppCompatActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        Spinner spinner = (Spinner) findViewById(R.id.spinner);

        //Get rid of the normal toolbar's title, because the spinner is replacing the title.
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        //Set the choices on the spinner by setting the adapter.
        spinner.setAdapter(new SpinnerAdapter(toolbar.getContext(), new String[]{"Overview", "Story", "Specifications", "Poll", "Video"}, accentColor, backgroundColor));

        //Set the listener for when each option is clicked.
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            {
                //Change the selected item's text color
                ((TextView) view).setTextColor(backgroundColor);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent)
            {
            }
        });
    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
                                   xmlns:app="http://schemas.android.com/apk/res-auto"
                                   android:layout_width="match_parent"
                                   android:layout_height="wrap_content"
                                   android:background="@color/ColorPrimary"
                                   android:elevation="4dp">
    <Spinner
        android:id="@+id/spinner"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</android.support.v7.widget.Toolbar>

我做错了什么:

之前,我听从了this answer的建议,在onItemSelected方法中设置了文字颜色,但是那个方法只有在UI完成后才会自动调用,并且您不能直接从您的代码中调用 onItemSelected。那导致了滞后。 (但是当你从下拉列表中选择一个项目时仍然需要它 - 请参阅我对这个问题的解决方案。)

解法:

策略是在onCreate完成之前获取"Selected"视图并设置其文本颜色。当我在调试器中测试它时,在 onCreate 方法期间没有显示 UI,所以这保证可以工作。

我只需要在调用 setAdapter(...) 之后添加这段代码:

//Set the text color of the Spinner's selected view (not a drop down list view)
spinner.setSelection(0, true);
View v = spinner.getSelectedView();
((TextView)v).setTextColor(backgroundColor);

关键点是用true参数调用spinner.setSelection(0, true)否则,如果你只调用spinner.setSelection(0),视图v 将为空。感谢 this answer.

我发现了这个

完整方法:

这是完整的方法。 注意: onItemSelected 中的代码仍然需要存在!因为否则,每次您 select 从下拉列表中选择一个项目时,它的颜色都会有误。

@Override 
protected void onCreate(Bundle savedInstanceState)
{ 
    Spinner spinner = (Spinner) findViewById(R.id.spinner);

    //Get rid of the normal toolbar's title, because the spinner is replacing the title. 
    getSupportActionBar().setDisplayShowTitleEnabled(false); 

    //Set the choices on the spinner by setting the adapter. 
    spinner.setAdapter(new SpinnerAdapter(toolbar.getContext(), new String[]{"Overview", "Story", "Specifications", "Poll", "Video"}, accentColor, backgroundColor));

    //Set the text color of the Spinner's selected view (not a drop down list view)
    spinner.setSelection(0, true);
    View v = spinner.getSelectedView();
    ((TextView)v).setTextColor(backgroundColor);

    //Set the listener for when each option is clicked. 
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    { 

        @Override 
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
        { 
           //Change the selected item's text color 
           ((TextView) view).setTextColor(backgroundColor);
        } 

        @Override 
        public void onNothingSelected(AdapterView<?> parent)
        { 
        } 
    }); 

} 

有关 setSelection 方法源代码的更多信息,请参阅此处的 AbsSpinner.java 代码:https://android.googlesource.com/platform/frameworks/base/+/jb-release/core/java/android/widget/AbsSpinner.java

这里是 Spinner.java 以防有帮助:https://android.googlesource.com/platform/frameworks/base/+/jb-release/core/java/android/widget/Spinner.java

如果其他人需要为微调器的选定项目和下拉项目获得单独的文本颜色、设计等,您可以使用下一个代码完成它

val adapter = ArrayAdapter(
        context,
        R.layout.custom_spinner_title_item,
        tabsArray.map { it.name }.toTypedArray(),
    )
    spinner.adapter = adapter

    adapter.setDropDownViewResource(android.R.layout.simple_list_item_1)

哪里

R.layout.custom_spinner_title_item - 所选项目的设计文件

android.R.layout.simple_list_item_1 - 下拉项设计文件

因此,AdapterView.OnItemSelectedListener hack

不再需要