ViewPager片段中小部件的交互

时间:2014-05-31 05:40:25

标签: android android-fragments android-viewpager adapter

我已成功设置与FragmentActivity关联的FragementPagerAdapter ViewPager,以实现两个标签式应用。

其中一个标签是" Wave"有一个文本视图和一个按钮。我想要的只是通过其xml属性描述的按钮的textview.setText方法调用onClick方法。

我不知道我应该在哪里初始化TextViewButton,如何获取Wave标签context以及我应该在哪里写onclick method -

public class InformationShow extends FragmentActivity {

XMLdata dataObject;

ViewPager viewPager;
    PagerAdapter adpt;


    Fragment temp;  
    TextView tv;
    Button bt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_main);
        viewPager=(ViewPager)findViewById(R.id.pager);
            adpt  =  new  PagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adpt);
      // temp=adpt.fg.findFragmentById((int)adpt.getItemId(1));

   tv=(TextView)findViewById(R.id.graphWaveTextView);
   bt = (Button)findViewById(R.id.button1);

     }


 public void changeText(View v){

  tv.setText("It worked ");
 }

适配器类

public class PagerAdapter extends FragmentPagerAdapter {
int count = 2;
CharSequence namme[] = {"Temperature","Wave"}; 
XMLdata data;
FragmentManager fg;
public  PagerAdapter(FragmentManager fragmentManager ){
    super(fragmentManager);
    this.fg = fragmentManager;
}
Context context;

@Override
public Fragment getItem(int arg0) {

    switch (arg0){

    case 0:{    

        TemperatureGraphFrag temp = new TemperatureGraphFrag();

        return temp;

    } 


    case 1:{WaveHeightGraphFrag wave = new WaveHeightGraphFrag();

    return wave;
    } 




    }

    return null;
}






@Override
public int getCount() {
    return 2;
}



@Override
public CharSequence getPageTitle(int position) {
return namme[position];
}




    }

碎片类

public class TemperatureGraphFrag extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    View    view = inflater.inflate(R.layout.graph_t, container, false);
        return view;
    }



}


public class WaveHeightGraphFrag extends Fragment  {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    View    view = inflater.inflate(R.layout.graph_sig_wave_height, container, false);


    return view;


    }




}
FragmentActicity

实施的

fragment_main XML

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.v4.view.PagerTabStrip
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:paddingBottom="10dp"
        android:paddingTop="10dp"
        android:textColor="#65C2C9"
        android:scrollbarSize="5dp"/>


</android.support.v4.view.ViewPager>

标签2片段XML graph_sig_wave_height

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"


     >

    <TextView
        android:id="@+id/graphWaveTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:layout_gravity="center"


         />

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

        android:layout_gravity="center" 
        android:onClick="changeText"/>

</LinearLayout>

标签1片段布局XML graph_t

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/linearTemp"
     >

    <TextView
        android:id="@+id/graphTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" 
        android:layout_gravity="center"
        />

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

将以下方法添加到WaveHeightGraphFrag班级:

@Override
public void onViewCreated(View view, Bundle savedInstanceState){

    final TextView t = (TextView) view.findViewById(R.id.graphWaveTextView);

    Button b = (Button) view.findViewById(R.id.button1);
    b.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v){
            t.setText("It worked ");
        }
   });
}

我希望这就是你想要的......:)