安卓开发:viewpager + fragment 实现滑动切换

本文详细介绍了一种基于Android的主界面布局设计,利用RelativeLayout和RadioGroup实现底部导航栏的功能,通过ViewPager进行页面间的平滑切换,同时展示了如何在MainActivity中设置ViewPager和RadioGroup的联动效果,以及Fragment的管理和切换。
摘要由CSDN通过智能技术生成

时间紧迫 长话短说 以后再补上
布局文件、

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#FFFFFFFF"
        android:orientation="vertical" >
       <!-- <FrameLayout
            android:id="@+id/frm_all"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1" >
        </FrameLayout>-->

        <android.support.v4.view.ViewPager
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:id="@+id/mainpager"
            >


        </android.support.v4.view.ViewPager>
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.0"
            android:visibility="gone" />

        <RadioGroup
            android:id="@+id/radiogroup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:gravity="center_vertical"
            android:orientation="horizontal" >
            <RadioButton
                android:id="@+id/home"
                style="@style/main_tab_bottom"
                android:layout_weight="1"
                android:drawableTop="@drawable/homebottom"
                android:text="" />
            <RadioButton
                android:id="@+id/freends"
                style="@style/main_tab_bottom"
                android:layout_weight="1"
                android:drawableTop="@drawable/friendsbottom"
                android:text="" />
            <RadioButton
                android:id="@+id/find"
                style="@style/main_tab_bottom"
                android:layout_weight="1"
                android:drawableTop="@drawable/findbottom"
                android:text="" />
            <RadioButton
                android:id="@+id/shop"
                style="@style/main_tab_bottom"
                android:layout_weight="1"
                android:drawableTop="@drawable/shopbottom"
                android:text="" />
            <RadioButton
                android:id="@+id/mycenter"
                style="@style/main_tab_bottom"
                android:layout_weight="1"
                android:drawableTop="@drawable/mycenterbottom"
                android:text="" />

        </RadioGroup>
    </LinearLayout>

</RelativeLayout>

activity 文件

package com.example.jzd.surname;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.RadioGroup;

import com.example.jzd.surname.activity.HomeFragment;
import com.example.jzd.surname.activity.MycenterFragment;
import com.example.jzd.surname.activity.TestFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private List<Fragment> fragmentList = new ArrayList<>();
    private TestFragment fragmentM;
    private TestFragment fragmentN;
    private MyPagerAdapter adapter;

    private RadioGroup radiogroup;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_main);
        viewPager = findViewById(R.id.mainpager);
        viewPager.setOffscreenPageLimit(5);

        fragmentM = new TestFragment();
        fragmentN = new TestFragment();
        //填充数据
        fragmentList.add(new HomeFragment());
        fragmentList.add(fragmentN);
        fragmentList.add(new TestFragment());
        fragmentList.add(new TestFragment());
        fragmentList.add(new MycenterFragment());

        adapter = new MyPagerAdapter(getSupportFragmentManager(), fragmentList);
        viewPager.setAdapter(adapter);

        radiogroup = (RadioGroup) findViewById(R.id.radiogroup);

        radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.home:
                        viewPager.setCurrentItem(0);
                        break;
                    case R.id.freends:
                        viewPager.setCurrentItem(1);

                        break;
                    case R.id.find:
                        viewPager.setCurrentItem(2);

                        break;
                    case R.id.shop:
                        viewPager.setCurrentItem(3);

                        break;
                    case R.id.mycenter:
                        viewPager.setCurrentItem(4);

                }
            }
        });
        radiogroup.check(R.id.home);
        viewPager.addOnPageChangeListener(new MyPagerChangeListener());
    }


    class MyPagerAdapter extends FragmentPagerAdapter {

        private List<Fragment> mfragmentList;

        TestFragment fragment111;


        public MyPagerAdapter(FragmentManager fm, List<Fragment> fragmentList) {
            super(fm);
            this.mfragmentList = fragmentList;
        }

        @Override
        public Fragment getItem(int position) {
            return mfragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mfragmentList.size();
        }
    }

    public class MyPagerChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageSelected(int arg0) {
            switch (arg0) {
                case 0:
                    radiogroup.check(R.id.home);
                    break;
                case 1:
                    radiogroup.check(R.id.freends);
                    break;
                case 2:
                    radiogroup.check(R.id.find);
                    break;
                case 3:
                    radiogroup.check(R.id.shop);
                    break;
                case 4:
                    radiogroup.check(R.id.mycenter);
                    break;
            }
        }
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值