Android ProgressBar styled like progress view in SwipeRefreshLayout

前端 未结 2 1097
南笙
南笙 2021-02-14 02:54

I use android.support.v4.widget.SwipeRefreshLayout in my Android app. It wraps a ListView. The content of the list view is downloaded from a server.

2条回答
  •  旧巷少年郎
    2021-02-14 03:28

    However, I would like to show the same progress indication through the whole app. Consider e.g. another activity that looks like a form with submit button. There is neither a ListView nor a SwipeRefreshLayout in this form activity. Some progress indication should be displayed while the submitted data are transferred to the server. I want to show a progress bar with the same animation like in SwipeRefreshLayout.

    yes you can use SwipeRefreshLayout to show like ProgressDialog when button click. check below.

    i have added SwipeRefreshLayout inside my layout file still there is not ListView or ScrollView. just like below

    
    
    
        
    
            
    
                
    
                
    
                

    now in my activity i used AsyncTask to show SwipeRefreshLayout. also use mSwipeRefreshLayout.setOnRefreshListener(null) to stop swipe gesture.

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.activity_main_swipe_refresh_layout);
    
    
            mSwipeRefreshLayout.setOnRefreshListener(null);
    
            ((Button) findViewById(R.id.button)).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // mSwipeRefreshLayout.setRefreshing(true);
                    // call AsyncTask
                    new LongOperation().execute("");
                }
            });
    
        }
    
        private class LongOperation extends AsyncTask {
    
            @Override
            protected String doInBackground(String... params) {
                for (int i = 0; i < 5; i++) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        Thread.interrupted();
                    }
                }
                return "Executed";
            }
    
            @Override
            protected void onPostExecute(String result) {
                // stop mSwipeRefreshLayout
                mSwipeRefreshLayout.setRefreshing(false);
            }
    
            @Override
            protected void onPreExecute() {
                // start mSwipeRefreshLayout
                mSwipeRefreshLayout.setRefreshing(true);
            }
    
            @Override
            protected void onProgressUpdate(Void... values) {
            }
        }
    

    EDITED:15/02/20 After 4 year, Here is great explanation on android developer site

提交回复
热议问题