【问题标题】:Setting spinner text color programmatically lags, is slow, is wrong color for split second以编程方式设置微调器文本颜色滞后,速度慢,瞬间颜色错误
【发布时间】:2016-02-27 18:34:44
【问题描述】:

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

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

视频: 请观看此屏幕录像以查看实际情况: 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>

【问题讨论】:

    标签: android spinner


    【解决方案1】:

    我做错了什么:

    之前,我是按照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);
    

    关键是调用spinner.setSelection(0, true) 带有true参数。否则,如果你只是调用spinner.setSelection(0),那么视图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

    【讨论】:

      【解决方案2】:

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

      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 不再需要

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-25
        • 2015-07-01
        • 2017-01-02
        相关资源
        最近更新 更多