RadioGroup+FragmentManager实现外部页面切换;ViewPager+FragmentAdapter实现内部页面切换

tabbar页面切换(RadioButton+FragmentManager+FrameLayout的基础上,进行Fragment实现外部页面切换,ViewPager+Fragment实现内部界面切换。

底部tabbar实现效果是一摸一样的,内部页面切换是在一个fragment里再进行页面切换,使用ViewPager+FragmentAdapter。

内部嵌套的管理fragment布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/layout_views"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/text_view1"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:textColor="@color/colorPrimary"
                android:textSize="19sp"
                android:text="热点"/>
            <TextView
                android:id="@+id/text_view2"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:textColor="@color/colorPrimary"
                android:textSize="19sp"
                android:text="成都"/>
            <TextView
                android:id="@+id/text_view3"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:textColor="@color/colorPrimary"
                android:textSize="19sp"
                android:text="科技"/>
        </LinearLayout>
        <android.support.v4.view.ViewPager
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </android.support.v4.view.ViewPager>
    </LinearLayout>
</LinearLayout>

管理的fragment代码:


import android.os.Bundle;
import android.support.annotation.Nullable;
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.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.uoso.myapplicationdemo.R;
import com.example.uoso.myapplicationdemo.viewpagerdemo.Fragment1;
import com.example.uoso.myapplicationdemo.viewpagerdemo.Fragment2;
import com.example.uoso.myapplicationdemo.viewpagerdemo.Fragment3;

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

/**
 * Fragment页面中使用ViewPager+Fragment
 */

public class FragmentFind extends Fragment implements View.OnClickListener{

    private TextView textView1;
    private TextView textView2;
    private TextView textView3;
    private List<TextView> textViewList;
    private ViewPager viewPager;

    private List<Fragment> mFragmentList = new ArrayList<Fragment>();
    private FragmentAdapter mFragmentAdapter;
    private Fragment1 fragment1;
    private Fragment2 fragment2;
    private Fragment3 fragment3;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_found, container, false);
        textView1 = (TextView) view.findViewById(R.id.text_view1);
        textView2 = (TextView) view.findViewById(R.id.text_view2);
        textView3 = (TextView) view.findViewById(R.id.text_view3);
        viewPager = (ViewPager) view.findViewById(R.id.view_pager);
        initView();
        return view;
    }

    private void initView(){

        textView1.setOnClickListener(this);
        textView2.setOnClickListener(this);
        textView3.setOnClickListener(this);

        textViewList = new ArrayList<>();
        textViewList.add(textView1);
        textViewList.add(textView2);
        textViewList.add(textView3);

        textViewList.get(0).setTextColor(getResources().getColor(R.color.colorAccent));

        fragment1 = new Fragment1();
        fragment2 = new Fragment2();
        fragment3 = new Fragment3();
        mFragmentList.add(fragment1);
        mFragmentList.add(fragment2);
        mFragmentList.add(fragment3);

        // 1.android的v4扩展包中的FragmentActivity中获取FragmentManager使用的就是getSupportFragmentManager(),
        // android.app中获取管理类的方法就是getFragmentManager()。
        // 2.需要管理相互独立的并且隶属于Activity的Fragment使用FragmentManager(),
        // 而在Fragment中动态的添加Fragment要使用getChildFragmetManager()来管理。
        mFragmentAdapter = new FragmentAdapter(getChildFragmentManager(), mFragmentList);
        viewPager.setAdapter(mFragmentAdapter);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for(int i = 0; i < textViewList.size();i++){
                    if(i == position){
                        textViewList.get(i).setTextColor(getResources().getColor(R.color.colorAccent));
                    }else {
                        textViewList.get(i).setTextColor(getResources().getColor(R.color.colorPrimary));
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.text_view1:
                viewPager.setCurrentItem(0);
                break;
            case R.id.text_view2:
                viewPager.setCurrentItem(1);
                break;
            case R.id.text_view3:
                viewPager.setCurrentItem(2);
                break;
        }
    }

    public class FragmentAdapter extends FragmentPagerAdapter {

        List<Fragment> fragmentList = new ArrayList<Fragment>();

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

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

        @Override
        public int getCount() {
            if(fragmentList != null && fragmentList.size() > 0){
                return fragmentList.size();
            }else {
                return 0;
            }
        }
    }
}

嵌套的小fragment布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ccebe8">

</LinearLayout>

潜逃的小fragment代码:


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.uoso.myapplicationdemo.R;

/**
 * Created by UOSO on 2018/3/14.
 */

public class Fragment1 extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment1, null);
        initView();
        return view;
    }

    public void initView(){

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值