将进度微调器放在SearchView中?

时间:2014-10-01 14:16:25

标签: android

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

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

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

谢谢

3 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分:1)

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

@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)。

答案 2 :(得分: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();

    }