2012-01-30 46 views
30

Tôi đang sử dụng ActionBar. Tôi muốn có một spinner tiến bộ làm mới trên thanh tiêu đề, nếu tôi đặt nó quay - nếu không thì ẩn nó đi. Có thể không ?:Hiển thị spinner tiến trình (làm mới) trên ActionBar?

// My menu has a refresh item, but it shouldn't be visible on the 
// actionbar unless it's spinning. 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:id="@+id/menu_refresh" 
    android:title="@string/refresh" 
    android:icon="@drawable/ic_action_refresh" /> 
</menu> 

... 

// When I need to show some work being done on my activity, 
// can I somehow now make the spinner associated with the 
// refresh item become visible on the action bar? 
getActionBarHelper().setRefreshActionItemState(true); 

Tôi không muốn nó trên ActionBar trừ khi nó đang "tiến hành"/quay.

Cảm ơn

Trả lời

75

Xin lỗi vì không có thẻ mã, đăng từ điện thoại ...

Đây là từ ActionBarSherlock (Google rằng nếu bạn đã không đi qua nó, cho phép hỗ trợ actionbar trong tổ ong trước)

trong onCreate hoạt động chính

// This has to be called before setContentView and you must use the 
// class in android.support.v4.view and NOT android.view 

requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); 

Để hiển thị/ẩn tiến bộ trong thanh hành động. Chú ý với ActionBarSherlock bạn phải sử dụng boolean.TRUE/FALSE, không chỉ đúng/sai .........

if (getSupportLoaderManager().hasRunningLoaders()) { 
    setProgressBarIndeterminateVisibility(Boolean.TRUE); 
} else { 
    setProgressBarIndeterminateVisibility(Boolean.FALSE); 
} 
+0

Ok cảm ơn tôi sẽ đào sâu trong đó một chút. Mẫu được cung cấp với SDK không tốt như tôi nghĩ ban đầu. Họ đang sử dụng Window.FEATURE_CUSTOM_TITLE có tác dụng hiển thị cho người dùng thanh tiêu đề trước khi được vẽ hoàn toàn theo ý muốn của bạn, nó trông thực sự không chuyên nghiệp. Thở dài. – user291701

+0

@Jake Wharton Tôi đang cố gắng làm việc này. Khi nút 'refresh' được nhấn gọi lại, 'onOptionsItemSelected' được gọi. Chúng ta gọi 'setProgressBarIndeterminateVisibility' ở đâu theo gợi ý của MartinS. –

+19

Nếu bạn đang sử dụng ActionBarSherlock, hãy đảm bảo sử dụng setSupportProgressBarIndeterminateVisibility(). Nếu không, chỉ báo tiến trình của bạn sẽ quay mọi lúc và bạn sẽ không biết tại sao. –

3

Nếu bạn kéo dài từ một ActionBarActivity, hãy thử này:

public class MainActivity extends ActionBarActivity { 

    boolean showUp=true; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); 
     setContentView(R.layout.activity_main); 
     setSupportProgressBarIndeterminateVisibility(Boolean.TRUE); 

     Button b = (Button) findViewById(R.id.myButton); 
     b.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

       if(showUp){ 
        setSupportProgressBarIndeterminateVisibility(Boolean.FALSE); 
       }else { 
        setSupportProgressBarIndeterminateVisibility(Boolean.TRUE); 
       } 
       showUp=!showUp; 
      } 
     }); 
} 
+0

"Window" thuộc về gói nào? – Hesam

+3

Thư viện hỗ trợ v21 không hỗ trợ điều này, vì vậy hãy lưu ý. : //stackoverflow.com/questions/26443490/appcompat-show-progress-in-action-bar-causes-npe và https://chris.banes.me/2014/10/17/appcompat-v21/#comment- 1642002459 – indyfromoz

Các vấn đề liên quan