2012-07-11 45 views
0

我在操作欄中有一個刷新菜單項,我的主佈局只包含一個按鈕。 ,所以點擊刷新菜單項應該把它變成一個微調,點擊按鈕應該停止旋轉。 我的問題是,當我點擊刷新按鈕時,它開始旋轉,這是正確的,但現在當我旋轉它時,它會自動停止旋轉。Actionbarsherlock方向改變不確定進度條停止旋轉

這是我的活動。

public class MyActivity extends SherlockActivity implements OnClickListener{ 

View refreshView = null; 
com.actionbarsherlock.view.MenuItem refreshItem = null; 
Animation rotateClockwise = null; 
Button btn = null; 
boolean downloadComplete = false; 
boolean selected = false; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_my); 
    btn = (Button)findViewById(R.id.button1); 
    btn.setOnClickListener(this); 
} 

@Override 
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) { 
    getSupportMenuInflater().inflate(R.menu.my_menu, menu); 
    // Inflate our custom layout. 

    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); 
    refreshView = (View) inflater.inflate(
      R.layout.actionbar_indeterminate_progress, null); 
    refreshItem = menu.findItem(R.id.menu_refresh); 
    if (selected){ 
     refreshItem.setActionView(refreshView); 
    } 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(
     final com.actionbarsherlock.view.MenuItem item) { 
    switch (item.getItemId()) { 
    case R.id.menu_refresh: 
     runOnUiThread(new Runnable() { 

      @Override 
      public void run() { 
       // TODO Auto-generated method stub 
       // Apply the View to our MenuItem 
       item.setActionView(refreshView); 
      } 
     }); 
     // Refresh the data 
     //refresh(); 
     selected =true; 
     break; 
    } 
    return true; 
} 

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    downloadComplete = !downloadComplete; 
    selected = false; 
    runOnUiThread(new Runnable() { 
      @Override 
     public void run() { 
      // TODO Auto-generated method stub 
      refreshItem.setActionView(null); 
     } 
    }); 
} 
} 

這是我RES /佈局/ actionbar_indeterminate_progress.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="center"> 
    <ProgressBar android:layout_width="32dp" 
     android:layout_height="32dp" 
     android:layout_marginLeft="12dp" 
     android:layout_marginRight="12dp" 
     android:layout_gravity="center" 
     style="?android:attr/indeterminateProgressStyle" /> 
</FrameLayout> 

我RES /佈局/ activity_my.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:padding="@dimen/padding_medium" 
     android:text="@string/hello_world" 
     tools:context=".MyActivity" /> 
</RelativeLayout> 

和我RES /菜單/ my_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item 
     android:id="@+id/menu_refresh" 
     android:icon="@drawable/ic_menu_refresh" 
     android:showAsAction="always" 
     android:title="Refresh"/> 
    <item 
     android:id="@+id/settings" 
     android:icon="@drawable/ic_menu_refresh" 
     android:title="Settings"> 
    </item> 
</menu> 

回答

1

問題是一個活動是摧毀然後再根據方向變化創建。你需要做兩件事情:

  1. 保存選擇場的方向變化前值。有關如何處理方向更改的詳細信息,請參閱Handling configuration changes部分以及更多文章Handling Runtime Changes
  2. 當在方向更改後重新創建活動時,請重新存儲選定的字段,如果該值爲true,則使用其值來啓動動畫。
+1

我試圖避免這種情況,但我想這是唯一的出路。 thanxs – Mrinal 2012-07-19 12:40:07