安卓開發:viewpager + fragment 實現滑動切換

農夫YH發表於2018-07-31

時間緊迫 長話短說 以後再補上
佈局檔案、

<?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;
            }
        }
    }


}

相關文章