您的位置:首页 > 其它

ViewPager+Fragment+RadioGroup 老方法

2017-10-23 15:31 274 查看
activity_main.xml页面布局:
<RelativeLayout 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"
>

<RadioGroupandroid:id="@+id/radio"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>

<RadioButton
android:id="@+id/but1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="金岭"
android:button="@null"
android:checked="true"
android:padding="10dp"
android:gravity="center"
/>
<RadioButton
android:id="@+id/but2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="帅马"
android:button="@null"
android:padding="10dp"
android:gravity="center"
/>
<RadioButton
android:id="@+id/but3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
            [b]android:text="草斌"
android:button="@null"
android:padding="10dp"
android:gravity="center"
/>
</RadioGroup>

<android.support.v4.view.ViewPager
android:layout_above="@+id/radio"
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

</RelativeLayout>
fragment01页面布局:(另外两个页面也一样,颜色改一下就行,写别的布局也可以)
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#44ff44"android:orientation="vertical" ></LinearLayout>
Fragment01页面代码:(另外两个页面代码也一样,R.layout.fragment01布局改一下就行,主要写代码
package com.example.myapplication;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class Fragment01 extends Fragment{@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {//加载布局View view=inflater.inflate(R.layout.fragment01, container, false);return view;}}
MainActivity主页面代码:
package com.example.myapplication;import android.os.Bundle;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentPagerAdapter;import android.support.v4.view.ViewPager;import android.support.v4.view.ViewPager.OnPageChangeListener;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;public class MainActivity extends FragmentActivity {private ViewPager viewPager;private RadioGroup radioGroup;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//获取资源IdviewPager=(ViewPager) findViewById(R.id.viewpager);radioGroup=(RadioGroup) findViewById(R.id.radio);//1.通过FragmentPagerAdapter加载FragmentviewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {//条目的数量@Overridepublic int getCount() {return 3;}//arg0 位置@Overridepublic Fragment getItem(int arg0) {//定义一个空的FragmentFragment fragment=null;//根据滑动的位置去判断加载的那个Fragmentswitch (arg0) {case 0:fragment=new Fragment01();break;case 1:fragment=new Fragment02();break;case 2:fragment=new Fragment03();break;}return fragment;}});//2.对radioGroup监听 去设置点击button时可以切换ViewPagerd的页面//点击button切换页面radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {public void onCheckedChanged(RadioGroup group, int checkedId) {switch (checkedId) {case R.id.but1:viewPager.setCurrentItem(0);break;case R.id.but2:viewPager.setCurrentItem(1);break;case R.id.but3:viewPager.setCurrentItem(2);break;}}});//3.设置ViewPagerd的页选中的监听 去控制  页面滑动  button也跟着改变//滑动时  改变button的选中状态viewPager.setOnPageChangeListener(new OnPageChangeListener() {public void onPageSelected(int arg0) {switch (arg0) {case 0:radioGroup.check(R.id.but1);break;case 1:radioGroup.check(R.id.but2);break;case 2:radioGroup.check(R.id.but3);break;}}public void onPageScrolled(int arg0, float arg1, int [/b]arg2) {}public void onPageScrollStateChanged(int arg0) {}});}}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: