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

标签 android spinner

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

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

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

它在加载页面时如何寻找一瞬间: enter image description here

延迟时间过后它的样子(以及它从一开始应该看起来的样子): enter image description here

代码:

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” View 并设置其文本颜色。当我在调试器中对其进行测试时,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);

关键点是调用 spinner.setSelection(0, true) 使用 true 参数。否则,如果您只调用 spinner.setSelection(0), View v 将为空。感谢this answer,我发现了这一点.

完整方法:

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

@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

关于android - 以编程方式设置微调器文本颜色滞后,速度慢,瞬间颜色错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33908564/

相关文章:

java - 如何将字符串从 2 个 Activity 传递到 1 个 Activity ?

java - 崩溃时如何处理调用?

android - 在 Spinner 上选择/滚动项目时应用程序崩溃。 (LG 移动) (java.lang.IllegalStateException : Iteration already started)

Android微调器没有触发onItemSelected

android - scp/secure 从远程 android 设备/SSH 服务器复制最新文件

Android:如何设置保存文件等的路径,就像在我们写的 windows 中,c:/someting/...我将在 android 中做什么

android - "Node.js is not installed"。在尝试将 android studio 集成到 AWS 时

Android:无法加载 Activity

java - 从 sql 填充微调器并使结果唯一

java - 索引越界异常