0

アクションバーに更新メニュー項目があり、メインレイアウトはボタンだけで構成されています。したがって、更新メニュー項目をクリックするとスピナーに変わり、ボタンをクリックすると回転が停止します。私の問題は、更新ボタンを押すと回転が始まりますが、回転すると自動的に回転が止まるということです。

これが私の活動です。

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>
4

1 に答える 1

1

問題は、アクティビティが破棄され、向きの変更時に再度作成されることです。あなたは2つのことをする必要があります:

  1. 向きを変更する前に、選択したフィールドの値を保存します。向きの変更を処理する方法の詳細については、「構成の変更の処理」セクションおよび「ランタイム変更の処理」を参照してください。
  2. 向きを変更した後にアクティビティを再作成する場合は、選択したフィールドを再保存し、値がtrueの場合は、その値を使用してアニメーションを開始します。
于 2012-07-14T07:18:38.950 に答える