ViewPaper+Fragment+RadioButton

最近自己写的Viewpaper +底部导航

废话少说直接看效果
ViewPaper+Fragment+RadioButton_第1张图片

思路:
viewpaper主要用来实现界面的左右滑动。
1.要实现底部导航能够控制,界面的滑动
2.要实现页面的滑动能够控制,底部导航的变化

接下来看一下xml布局

"http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    "@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    "@+id/rg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:background="@android:color/holo_purple"
        android:weightSum="4" >

        "@+id/rb1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/r4_selector"
            android:gravity="center_horizontal"
            android:text="首页"
            android:textColor="@drawable/selector_text_color" />

        "@+id/rb2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/r1_selector"
            android:gravity="center_horizontal"
            android:text="菜单"
            android:textColor="@drawable/selector_text_color" />

        "@+id/rb3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/r2_selector"
            android:gravity="center_horizontal"
            android:text="功能"
            android:textColor="@drawable/selector_text_color" />

        "@+id/rb4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/r3_selector"
            android:gravity="center_horizontal"
            android:text="退出"
            android:textColor="@drawable/selector_text_color" />
    </RadioGroup>

RelativeLayout>

java代码:

ViewPaper+Fragment+RadioButton_第2张图片

这个demo使用了4个Fragment ,只是充当布局,此处不再给出(自己可以随便写)

主要看一下MainActivity

  1. Viewpager 是通过适配器(FragmentPagerAdapter)的来提供数据的

此处需要重写一下适配器

package com.example.viewpager_3;

import java.util.List;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class FragmentPaperAdp extends FragmentPagerAdapter{


    List list;

    public FragmentPaperAdp(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }
    public FragmentPaperAdp(FragmentManager fm,List list) {
        super(fm);
        // TODO Auto-generated constructor stub
        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();
    }

}

2.接下来就是真正的硬菜了MainActivity

首先你要实例化所有的Fragment,并且把他们装到数组中

    //初始化数据
    private void initData() {
        // TODO Auto-generated method stub
        Fragment f1=new AFragment();
        Fragment f2=new BFragment();
        Fragment f3=new CFragment();
        Fragment f4=new DFragment();

    ArrayList<Fragment> list=new ArrayList<Fragment>();
        list.add(f1);
        list.add(f2);
        list.add(f3);
        list.add(f4);
    }```


填充适配器的数据
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

    //把碎片装进数组
    initData();
    initView();
    FragmentPaperAdp fadapter=new FragmentPaperAdp(getSupportFragmentManager(), list);
    viewPager.setAdapter(fadapter);
    rg.setOnCheckedChangeListener(oncheck);
    viewPager.setOnPageChangeListener(pagechaner);

}

//初始化视图组件
private void initView() {
    // TODO Auto-generated method stub
    viewPager=(ViewPager) findViewById(R.id.viewpager);
    rg=(RadioGroup) findViewById(R.id.rg);
}
RadioButton控制Viewpaper界面的切换
//选择按钮的监听
public RadioGroup.OnCheckedChangeListener oncheck=new OnCheckedChangeListener() {

    public void onCheckedChanged(RadioGroup group, int checkedId) {
        // TODO Auto-generated method stub

        if(R.id.rb1==checkedId)
        {
            viewPager.setCurrentItem(0);
        }else if(R.id.rb2==checkedId)
        {
            viewPager.setCurrentItem(1);

        }else if(R.id.rb3==checkedId)
        {
            viewPager.setCurrentItem(2);

        }else if(R.id.rb4==checkedId)
        {
            viewPager.setCurrentItem(3);

        }
    }
};

用界面的变化去控制单选按钮的变化

//是页面的变换控制单选按钮
ViewPager.OnPageChangeListener pagechaner=new OnPageChangeListener() {

    public void onPageSelected(int position) {
        // TODO Auto-generated method stub
    /**
     *从radiogroup中找到他的radiobutton,
     *然后设置他的选中状态 
     * 
     */
            ((RadioButton)rg.getChildAt(position)).setChecked(true);

    }

    public void onPageScrolled(int arg0, float arg1, int arg2) {
        // TODO Auto-generated method stub

    }

    public void onPageScrollStateChanged(int arg0) {
        // TODO Auto-generated method stub

    }
};

“`

资源下载地址

http://download.csdn.net/detail/nodonoidea/9031291

你可能感兴趣的:(viewpaper)