2012-11-20 3 views
0

FragmentPagerAdapter를 확장하는 사용자 정의 클래스에 의해 관리되는 3 개의 조각이 내 활동에 있습니다.Android : 버튼이있는 ViewPager에서 조각을 처리하십시오.

내 홈페이지 XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
     > 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      /> 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      /> 

     <Button 
      android:id="@+id/button3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      /> 


    <android.support.v4.view.ViewPager 
       android:id="@+id/tabviewpager" 
       android:layout_width="fill_parent" 
       android:layout_height="0dp" 
       android:layout_weight="1" /> 

</LinearLayout> 

내 활동

public class RankingFragmentActivity extends FragmentActivity{ 
.... 

    List fragments = new Vector(); 


fragments.add(Fragment.instantiate(this,Tab1Fragment.class.getName())); 
fragments.add(Fragment.instantiate(this,Tab2Fragment.class.getName())); 
fragments.add(Fragment.instantiate(this,Tab3Fragment.class.getName())); 

//adapter 
MyPagerAdapter mPagerAdapter = new MyPagerAdapter(super.getSupportFragmentManager(), fragments); 

ViewPager pager = (ViewPager) super.findViewById(R.id.tabviewpager); 

pager.setAdapter(this.mPagerAdapter); 

} 

내가 letf에서 오른쪽으로 (오른쪽 왼쪽) 조각에서 완벽하게 전환 할 수 있습니다.

표시되는 프래그먼트를 변경하기 위해 버튼을 리스너로 설정하고 싶지만 어떻게해야할지 모르겠다.

예 : 조각 2 단추 3에

클릭 표시됩니다 : 단추 1에 클릭 : 조각 1이 단추 2에

클릭을 표시 조각 3

표시됩니다

감사합니다!

답변

1

대답은 바로입니다 the documentation for FragmentPagerAdapter

// Watch for button clicks. 
    Button button = (Button)findViewById(R.id.goto_first); 
    button.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      mPager.setCurrentItem(0); 
     } 
    }); 
+0

완벽한 ~ 당신의 도움과 링크 주셔서 감사합니다! – johann