2012-07-11 68 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