在SearchView中添加进度旋转器?

4
我正在我的Activity中使用一个SearchView。随着用户的输入,我会向服务器执行搜索请求。我想表明某些活动正在进行中。是否可以在SearchView内显示进度旋转器?
否则,人们是如何处理这个问题的 - 我们创建一个自定义的actionbar父布局,然后在其中嵌入SearchView吗?
<LinearLayout orientation="horizontal">
    <SearchView />
    <ProgressBar />
</LinearLayout>

谢谢你

3个回答

7

首先创建进度条的布局。一个类似这样的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”名称的更新,对吗? - user3203425
是的,可以。您还可以通过复制Google SDK中的SearchView来创建自己的SearchView。 - Pedro Oliveira

1

如果您正在使用一个扩展了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();

    }

1

我知道这不是在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顶部)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接