2013-08-23 4 views
1

내 프로젝트에서 뷰 스위처를 사용하고 있습니다.두 레이아웃 모두에서 동일한 ID로 viewswitcher를 사용하는 방법

내 XML에 동일한 ID로 2 개의 레이아웃을 만들었습니다. 내보기를 전환 한 후에 두 레이아웃에서 동일한 ID를 사용하기 때문에 이전보기로 전환 할 수 없습니다.

이제 어떻게보기 전환기에서 두 레이아웃의 자바 코드에서 하나의 수신기를 사용할 수 있습니까? 다른 ID를 만들고 다른 리스너를 만들어 다시 전환하고 싶지 않습니다.

내 xml은 다음과 같습니다.

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; 
      } 
     } 
    }); 

도와주세요 .. XML에 부모 상대 레이아웃

답변

0

크레타 다른 ID를 다음과 같이

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

내 자바 코드입니다.

그리고 자바 파일에서 어떤 상대 레이아웃 버튼이 사용되었는지 확인하십시오. 다만 UR보기 스위칭하다 .may 버튼이나 텍스트 뷰 중 어느 UR 텍스트를 변경하려고 방법

단계 1)

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

단계 2)

if button.getParent equals to R1 then ... 
else ..... 
+0

어떻게 2 개의 다른 ID의 레이아웃으로 버튼을 구별 할 수 있습니까? 예를 들어주세요. – user2085965

+0

확인 내 대답 업데이트 –

0

를 사용 하지만 코드 때문에 두 레이아웃 모두 정확히 구별 할 수없는 동일한 URL입니다.