以编程方式设置微调器文本颜色滞后,缓慢,瞬间颜色错误 [英] Setting spinner text color programmatically lags, is slow, is wrong color for split second

查看:52
本文介绍了以编程方式设置微调器文本颜色滞后,缓慢,瞬间颜色错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我的微调器有问题.每当我运行该应用程序时,如果活动未缓存在内存中,它有时会滞后.文本是默认颜色(如黑色),然后才能将其设置为正确的颜色.看起来真的很不专业.

视频:请观看此屏幕录像以查看实际效果:

滞后时间后的外观(以及从一开始的外观):

代码:

 公共类MyActivity扩展了AppCompatActivity{@Override受保护的void onCreate(Bundle savedInstanceState){Spinner spinner =(Spinner)findViewById(R.id.spinner);//摆脱普通工具栏的标题,因为微调器正在替换标题.getSupportActionBar().setDisplayShowTitleEnabled(false);//通过设置适配器在微调器上设置选择.spinner.setAdapter(新的SpinnerAdapter(toolbar.getContext(),新的String [] {概述",故事",规范",投票",视频"},accentColor,backgroundColor));//设置单击每个选项时的监听器.spinner.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){@Override公共无效onItemSelected(AdapterView<?>父级,视图视图,int位置,长id){//更改所选项目的文字颜色(((TextView)视图).setTextColor(backgroundColor);}@Override公共无效onNothingSelected(AdapterView<?>父对象){}});}} 

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">< Spinnerandroid:id ="@ + id/spinner"app:popupTheme ="@ style/AppTheme.PopupOverlay"android:layout_width ="wrap_content"android:layout_height ="wrap_content"/></android.support.v7.widget.Toolbar> 

解决方案

我做错了什么:

之前,我遵循此答案的建议,并在 onItemSelected 方法,但是该方法仅在UI完成后自动调用,并且您不能直接从代码中调用 onItemSelected .那导致了滞后.(但是,当您从下拉列表中选择一项时,仍然需要它-请参阅我对这个问题的解决方案.)

解决方案:

该策略是获取"Selected"视图并在onCreate完成之前设置其文本颜色.当我在调试器中对其进行测试时,在 onCreate 方法期间未显示任何用户界面,因此保证可以正常工作.

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

 //设置微调框的选定视图(不是下拉列表视图)的文本颜色spinner.setSelection(0,true);查看v = spinner.getSelectedView();((TextView)v).setTextColor(backgroundColor); 

关键是要使用 true 参数调用 spinner.setSelection(0,true) .否则,如果您仅调用 spinner.setSelection(0),视图 v 为空.感谢此答案,我对此有所了解.

完整方法:

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

  @Override受保护的void onCreate(Bundle savedInstanceState){Spinner spinner =(Spinner)findViewById(R.id.spinner);//摆脱普通工具栏的标题,因为微调器正在替换标题.getSupportActionBar().setDisplayShowTitleEnabled(false);//通过设置适配器在微调器上设置选择.spinner.setAdapter(新的SpinnerAdapter(toolbar.getContext(),新的String [] {概述",故事",规范",投票",视频"},accentColor,backgroundColor));//设置微调框的选定视图(不是下拉列表视图)的文本颜色spinner.setSelection(0,true);查看v = spinner.getSelectedView();((TextView)v).setTextColor(backgroundColor);//设置单击每个选项时的监听器.spinner.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){@Override公共无效onItemSelected(AdapterView<?>父级,视图视图,int位置,长id){//更改所选项目的文字颜色(((TextView)视图).setTextColor(backgroundColor);}@Override公共无效onNothingSelected(AdapterView<?>父对象){}});} 

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

TLDR: My spinner displays the wrong color for a split second.

I have a problem with my spinner. Whenever I run the app, if the activity is not cached in memory, it sometimes lags. The text is a default color (like black) before I can set it to the right color. It looks really unprofessional.

Video: Please watch this screen recording to see this in action: https://drive.google.com/file/d/0By2AG5yaBEhMRnRsbVBDU251STQ/view

How it looks for one split-second while loading the page:

How it looks after the lag time (and how it is supposed to look from the start):

Code:

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>

解决方案

What I was doing wrong:

Before, I was following the advice of this answer, and setting the text color in the onItemSelected method, but that method is only called automatically after the UI is done, and you cannot call onItemSelected directly from your code. That was causing a lag. (But it is still needed for when you choose an item from the drop down list - see my solution to this question.)

Solution:

The strategy is to obtain the "Selected" view and set its text color before onCreate finishes. When I tested it in the debugger, no UI is shown during the onCreate method, so this is guaranteed to work.

I just had to add this code after the call to 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);

The key point is to call spinner.setSelection(0, true) with the true parameter. Otherwise, if you just call spinner.setSelection(0), the View v would be null. I found out about this thanks to this answer.

Complete method:

Here is the complete method. NOTE: The code in onItemSelected still needs to be there! Because otherwise, every time you select an item from the drop down list, it will have the wrong color.

@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)
        { 
        } 
    }); 

} 

For more info on the source code of the setSelection methods, see the AbsSpinner.java code here: https://android.googlesource.com/platform/frameworks/base/+/jb-release/core/java/android/widget/AbsSpinner.java

And here is Spinner.java in case it helps: https://android.googlesource.com/platform/frameworks/base/+/jb-release/core/java/android/widget/Spinner.java

这篇关于以编程方式设置微调器文本颜色滞后,缓慢,瞬间颜色错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆