2016-02-09 12 views
6

Tôi sử dụng android.support.v4.widget.SwipeRefreshLayout trong ứng dụng Android của mình. Nó kết thúc tốt đẹp một ListView. Nội dung của chế độ xem danh sách được tải xuống từ máy chủ.Android ProgressBar được tạo kiểu như chế độ xem tiến trình trong SwipeRefreshLayout

Chế độ xem tiến trình được hiển thị khi người dùng vuốt xuống để tải lại dữ liệu từ máy chủ. Chế độ xem tiến trình trông giống như một phần của vòng tròn phát triển và co lại trong khi hoạt ảnh. Dường như phong cách của chế độ xem tiến trình này không thể được tùy chỉnh nhiều. Tuy nhiên, tôi ổn với phong cách dựng sẵn của nó.

Tôi cũng hiển thị cùng một hình động tiến trình trong khi tải dữ liệu ban đầu. Điều này có thể đạt được bằng cách gọi mySwipeRefreshLayout.setRefreshing(true). Đó là hoàn toàn OK quá.

Tuy nhiên, tôi muốn hiển thị cùng một chỉ báo tiến trình thông qua toàn bộ ứng dụng. Xem xét ví dụ: hoạt động khác giống như biểu mẫu có nút gửi. Không có ListView cũng không phải là SwipeRefreshLayout trong hoạt động biểu mẫu này. Một số dấu hiệu tiến trình sẽ được hiển thị trong khi dữ liệu được gửi được chuyển đến máy chủ. Tôi muốn hiển thị thanh tiến trình có cùng hoạt ảnh như trong SwipeRefreshLayout.

Có cách nào đơn giản và sạch sẽ để có cùng chỉ báo tiến trình cho cả SwipeRefreshLayout và hoạt động biểu mẫu không chứa bất kỳ chế độ xem danh sách và bố cục làm mới nào và không hỗ trợ bất kỳ cử chỉ vuốt nào?

Cảm ơn.

Trả lời

7

Tuy nhiên, tôi muốn hiển thị cùng một chỉ báo tiến trình thông qua ứng dụng toàn bộ . Xem xét ví dụ: hoạt động khác trông giống như một biểu mẫu với nút gửi. Không có ListView hoặc SwipeRefreshLayout trong hoạt động biểu mẫu này. Một số chỉ báo tiến trình sẽ được hiển thị trong khi dữ liệu đã gửi sẽ được chuyển đến máy chủ. Tôi muốn hiển thị thanh tiến trình với cùng một hoạt ảnh như trong SwipeRefreshLayout.

có bạn có thể sử dụng SwipeRefreshLayout để hiển thị như ProgressDialog khi nút bấm. kiểm tra bên dưới.

enter image description here

tôi đã thêm SwipeRefreshLayout bên trong file layout của tôi vẫn không có ListView hoặc ScrollView. giống như dưới đây

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="niu.com.djandroid.jdroid.androidstack.MainActivity" 
    tools:showIn="@layout/activity_main"> 

    <android.support.v4.widget.SwipeRefreshLayout 
     android:id="@+id/activity_main_swipe_refresh_layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 

      <TextView 
       android:id="@+id/textView" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Hello World!" /> 

      <SeekBar 
       android:id="@+id/seekBar" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="54dp" /> 

      <Button 
       android:id="@+id/button" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="New Button" /> 
     </LinearLayout> 
    </android.support.v4.widget.SwipeRefreshLayout> 


</LinearLayout> 

tại trong hoạt động của tôi sử dụng để hiển thị AsyncTaskSwipeRefreshLayout. cũng sử dụng mSwipeRefreshLayout.setOnRefreshListener(null) để dừng cử chỉ vuốt.

@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<String, Void, String> { 

     @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:

bạn có thể sử dụng thư viện SwipeRefreshLayoutBottom từ Github. giống như swiperefreshlayout.kiểm tra dưới đây

enter image description here

+0

Cảm ơn ví dụ phức tạp (+1). Tôi phải thừa nhận rằng tôi quá lười để tự mình thử nghiệm phương pháp này. Tôi sẽ phải quấn tất cả các hoạt động của mình bằng 'SwipeRefreshLayout' chỉ để" ăn cắp "hoạt ảnh tiến trình của nó. Tôi coi đây là một giải pháp "bẩn". Tôi hy vọng rằng có thể có thể là ví dụ một cách để đặt một số kiểu thành 'ProgressBar' sao cho nó trông giống như chế độ xem tiến trình của' SwipeRefreshLayout'. – Cimlman

+0

@Cimlman: kiểm tra câu trả lời đã chỉnh sửa! –

+0

Không chắc chắn tôi sẽ xem xét điều này một giải pháp bẩn, nó đã được dễ dàng để thực hiện cho một webview và trông cách tốt hơn so với các tiến trình chuẩn. Cảm ơn! –

0
mSwipeRefreshLayout.setOnRefreshListener(null) 

không làm việc cho tôi, vì vậy tôi đã thực hiện phương pháp này để kích hoạt và vô hiệu hóa sảng khoái.

private void setToRefresh(boolean refresh) { 
    if (refresh) { 
     mSwipeRefreshLayout.setEnabled(true); 
     mSwipeRefreshLayout.setRefreshing(true); 
    } else { 
     mSwipeRefreshLayout.setRefreshing(false); 
     mSwipeRefreshLayout.setEnabled(false); 
    } 
} 
Các vấn đề liên quan