我在操作栏中有一个刷新菜单项,我的主布局只包含一个按钮。所以点击刷新菜单项应该把它变成一个微调器,点击按钮应该停止它旋转。我的问题是,当我点击刷新按钮时,它开始旋转,这是正确的,但现在当我旋转它时,它会自动停止旋转。
这是我的活动。
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/layout/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/layout/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/menu/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>