【问题标题】:Put a progress spinner in a SearchView?在 SearchView 中放置进度微调器?
【发布时间】:2014-10-01 14:16:25
【问题描述】:

我在我的 Activity 中使用 SearchView。当用户键入时,我正在向服务器执行搜索请求。我想表明一些活动正在发生。是否可以在 SearchView 中显示进度微调器?

否则,人们如何处理这个问题 - 我们是否创建自定义操作栏父布局,并在其中嵌入 SearchView?

<LinearLayout orientation="horizontal">
    <SearchView />
    <ProgressBar />
</LinearLayout>

谢谢

【问题讨论】:

    标签: android


    【解决方案1】:

    首先为进度条创建布局。像这样的 XML 应该可以完成这项工作:

    R.layout.loading_icon

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ProgressBar
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:id="@+id/search_progress_bar"
            android:layout_marginTop="5dp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true" />
    </RelativeLayout>
    

    接下来创建两个函数。一个用于在您的搜索视图上显示进度条,另一个用于隐藏它:

    public void showProgressBar(SearchView searchView, Context context)
    {
        int id = searchView.getContext().getResources().getIdentifier("android:id/search_plate", null, null);
        if (searchView.findViewById(id).findViewById(R.id.search_progress_bar) != null)
            searchView.findViewById(id).findViewById(R.id.search_progress_bar).animate().setDuration(200).alpha(1).start();
    
        else
        {
            View v = LayoutInflater.from(context).inflate(R.layout.loading_icon, null);
            ((ViewGroup) searchView.findViewById(id)).addView(v, 1);
        }
    }
    public void hideProgressBar(SearchView searchView)
    {
        int id = searchView.getContext().getResources().getIdentifier("android:id/search_plate", null, null);
        if (searchView.findViewById(id).findViewById(R.id.search_progress_bar) != null)
            searchView.findViewById(id).findViewById(R.id.search_progress_bar).animate().setDuration(200).alpha(0).start();
    }
    

    【讨论】:

    • 好吧,我认为这个解决方案依赖于谷歌没有发布“search_plate”更改的更新这一事实是对的吗?
    • 是的。您还可以通过应对来自 google sdk 的 SearchView 来创建自己的 SearchView。
    【解决方案2】:

    我知道它不在 SearchView 中,但这是迄今为止在 ActionBar 中提供进度指示的最简单方法,可在此处找到 http://blog.denevell.org/android-progress-spinner.html

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.activity_main);
    
        // Turn it on
        setProgressBarIndeterminateVisibility(true);
        // And when you want to turn it off
        setProgressBarIndeterminateVisibility(false);
    }
    

    如果您真的希望进度出现在 SearchView 中,我会假设您可以使用 FrameLayout 而不是 LinearLayout(或至少作为父级),将进度放在 XML 的底部(这会将其放在顶部SearchView)。

    【讨论】:

      【解决方案3】:

      如果您使用的是扩展 android.support.v7.widget.SearchView 的自定义 SearchView 类,则需要这样做:

      public void init(Context context){
      
              EditText searchPlate = (EditText) findViewById(android.support.v7.appcompat.R.id.search_src_text);
              //searchPlate.setHint("Search");
              mSearchPlateView = findViewById(android.support.v7.appcompat.R.id.search_plate);
              mSearchPlateView.setBackgroundColor(ContextCompat.getColor(context, android.R.color.transparent));
          }
      public void showProgressBar(Context context) {
      
              if (mProgressBar != null) {
                  mProgressBar.animate()
                          .setDuration(200)
                          .alpha(1)
                          .start();
              }
              else {
                  RelativeLayout relativeLayout = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.invest_progress_loading, null);
                  mProgressBar = (ProgressBar) relativeLayout.findViewById(R.id.search_progress_bar);
                  ((ViewGroup) mSearchPlateView).addView(relativeLayout, 1);
              }
          }
      
          public void hideProgressBar() {
      
              if(mProgressBar == null)
                  return;
      
              mProgressBar.animate()
                          .setDuration(200)
                          .alpha(0)
                          .start();
      
          }
      

      【讨论】:

        猜你喜欢
        • 2020-10-03
        • 2019-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多