Put a progress spinner in a SearchView?

前端 未结 3 1307
梦如初夏
梦如初夏 2021-01-01 06:03

I\'m using a SearchView in my Activity. As the user types, I am performing search requests to a server. I want to indicate that some activity is happening. Is it possible to

相关标签:
3条回答
  • 2021-01-01 06:21

    First create the layout for the progressbar. A XML like this should do the job:

    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>
    

    Next create two functions. One to show the progress bar on your searchview and other to hide it:

    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();
    }
    
    0 讨论(0)
  • 2021-01-01 06:23

    I know that it is not within the SearchView, but this is by far the simplest method to provide progress indication in the ActionBar found here 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);
    }
    

    If you really want the progress to appear inside the SearchView, I would assume that you could use a FrameLayout instead of LinearLayout (or atleast as a parent), putting the progress in bottom of the XML (which will place it on top of the SearchView).

    0 讨论(0)
  • 2021-01-01 06:32

    If you are using a custom SearchView class that extends android.support.v7.widget.SearchView, you'll need to do this:

    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();
    
        }
    
    0 讨论(0)
提交回复
热议问题