1

プロジェクトでビュースイッチャーを使用しています。

私の xml では、すべて同じ ID を持つ 2 つのレイアウトを作成しました。ビューを切り替えた後、両方のレイアウトで同じ ID を使用しているため、前のビューに切り替えることができません。

ビュースイッチャーの両方のレイアウトに対してJavaコードで1つのリスナーを使用するにはどうすればよいですか。別のIDを作成して、別のリスナーを作成して再度切り替えることはしたくありません。

私のxmlは以下の通りです。

<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/profileSwitcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/switchBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="Switch" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/switchBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="Switch" />
</RelativeLayout>

</ViewSwitcher>

私のJavaコードは次のとおりです

final ViewSwitcher switcher = (ViewSwitcher) findViewById(R.id.profileSwitcher);
    Button btn = (Button) findViewById(R.id.switchBtn);
    btn.setOnClickListener(new OnClickListener() {

        private boolean switchCheck;

        public void onClick(View v) {
            new AnimationUtils();
            switcher.setAnimation(AnimationUtils.makeInAnimation(getApplicationContext(), true));
            if (!switchCheck) {
                switcher.showNext();
                switchCheck = true;
            } else {
                switcher.showPrevious();
                switchCheck = false;
            }
        }
    });

助けてください..

4

2 に答える 2

0

xml で親の相対レイアウトに異なる ID を作成します。

次に、Java ファイルで、どの相対レイアウト ボタンが使用されているかを確認します。メソッドを使用して

ステップ1)

RelativeLayout r1 =  RelativeLayout findviewbyId(R.id.rl1)
RelativeLayout r2 =  RelativeLayout findviewbyId(R.id.rl2)

ステップ2)

if button.getParent equals to R1 then ... 
else .....
于 2013-08-23T09:54:33.600 に答える
0

ボタンまたは textView のいずれかでテキストを変更してみてください。ビューが切り替わっている可能性がありますが、両方のレイアウトのコードがまったく同じであるため、区別できません。

于 2013-08-23T10:14:13.020 に答える