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) {}});}}
相关文章推荐
- ViewPager,RadioGroup,FragmentManager 详解 <二> 主菜单 RadioGroup 和ViewPager 实现联动
- Fragment+ViewPager+RadioGroup实现的页面简单切换
- ViewPager+RadioGroup实现标题栏切换,Fragment切换
- ViewPager+RadioGroup实现标题栏切换,Fragment切换
- Android——ViewPager+RadioGroup+Fragment
- ViewPager+RadioGroup实现标题栏切换,Fragment切换
- ViewPager + Fragment+radiogroup实现滑动标签页
- 使用RadioGroup+ViewPager+Fragment实现带滑动的页卡效果TabHost时遇到的问题
- Android ViewPager+RadioGroup+Fragment超高仿微信主界面
- radiogroup+viewpager+fragment联动
- ViewPager+RadioGroup实现标题栏切换,Fragment切换
- 利用RadioGroup + ViewPager + Fragment实现书签导航
- 滑动或点击按钮切换页面 ViewPager+RadioGroup+Fragment
- 使用ViewPager+Fragment+RadioGroup实现类似微信主界面
- Android ViewPager+RadioGroup+Fragment超高仿微信主界面
- ViewPager+RadioGroup实现标题栏切换,Fragment切换
- ViewPager+RadioGroup实现标题栏切换,Fragment切换
- radiogroup+viewpager+fragment联动,
- RadiogroupFragmentViewpager