fragment快捷键
在res下面新建drawable文件夹》新建radio_select.xml文件》
-<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@color/red"/>
<item android:state_checked="false" android:drawable="@color/white"/>
</selector>
colors.xml下面的:
<resources>
<color name="red">#f00</color>
<color name="white">#ffffff</color>
</resources>
布局
<LinearLayout 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"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/radio_group"
android:layout_weight="1"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/rb_01"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/radio_select"
android:button="@null"
android:checked="true"
android:gravity="center"
android:text="xml"
android:textSize="18sp" />
<RadioButton
android:id="@+id/rb_02"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/radio_select"
android:button="@null"
android:gravity="center"
android:text="json"
android:textSize="18sp" />
</RadioGroup>
</LinearLayout>
==========================================================
主要方法
//FragmentActivity 找控件
view_pager = (ViewPager) findViewById(R.id.view_pager);
radio_group = (RadioGroup) findViewById(R.id.radio_group);
// 通过viewPager展示Fragment
list = new ArrayList<Fragment>();
list.add(new Fragment_xml());
list.add(new Fragment_json());
// 适配到ViewPager上
view_pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager(),list));
// 滑动页面,,,,radiobutton切换
view_pager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
radio_group.check(radio_group.getChildAt(arg0).getId());
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
// 切换radiobutton,,,页面切换
radio_group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.rb_01:
view_pager.setCurrentItem(0, false);
break;
case R.id.rb_02:
view_pager.setCurrentItem(1, false);
break;
default:
break;
}
}
});
============================================
FragmentPagerAdapter类
import java.util.List;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class MyPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> list;
public MyPagerAdapter(FragmentManager fm, List<Fragment> list) {
super(fm);
this.list = list;
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
}