将进度微调器放入SearchView中?
问题描述:
我在我的活动中使用了SearchView。当用户键入时,我正在向服务器执行搜索请求。我想指出一些活动正在发生。是否有可能在SearchView中显示进度微调器?将进度微调器放入SearchView中?
否则,人们如何处理这个问题 - 我们是否创建了一个自定义的操作栏父级布局,并在其中嵌入了SearchView?
<LinearLayout orientation="horizontal">
<SearchView />
<ProgressBar />
</LinearLayout>
谢谢
答
首先为进度条创建布局。像这样的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();
}
答
我知道这是不是搜索查看内,但是这是迄今为止最简单的方法来提供进度指示在动作条在这里找到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的顶部) 。
答
如果您使用的是扩展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();
}
好吧,我正确地认为这个解决方案依赖于谷歌没有发布更新“search_plate”更改的事实吗? – user3203425 2014-10-02 19:19:31
是的。您也可以通过应对Google sdk中的一个来创建自己的SearchView。 – 2014-10-02 19:21:07
不错的工作。谢谢 :) – 2015-06-12 11:49:44