动态加载fragment配合TabLayout

这种配合使用TabLayout来控制,获取position比较方便,fragment不需要一次性全部加载,而是在第一次使用的时候加载。也需要WindowManager和事务进行管理。因为没有用到ViewPager,所以没有滑动效果。比较适合做程序大分类主要框架。

一、MyFragment

/**
 * A simple {@link Fragment} subclass.
 */
public class MyFragment extends Fragment {
    private TextView textView;

    public MyFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_my, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        String title= (String) getArguments().get("title");
        textView= (TextView) view.findViewById(R.id.txt_title);
        textView.setText(title);
    }

}

二、TabFragmentActivity 

/**
 * 动态加载fragment,配合TabLayout
 */
public class TabFragmentActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener {
    private TabLayout tabLayout;
    private Fragment[] fragments;
    private String[] titles = {"天九", "地八", "人七", "和五"};
    private int position;
    FragmentManager fragmentManager;//获取fragment管理器
    FragmentTransaction fragmentTransaction;//获取事务

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_fragment);
        initView();
    }

    /**
     * 初始化
     */
    private void initView() {
        fragmentManager = getSupportFragmentManager();//获取fragment管理器
        fragments = new Fragment[titles.length];
        tabLayout = (TabLayout) findViewById(R.id.tab_nav);
        tabLayout.setOnTabSelectedListener(this);//这一句监听放在添加tab之前,才可以默认加载第一页
        //添加tab
        for (int i = 0; i < titles.length; i++) {
            TabLayout.Tab tab = tabLayout.newTab();
            tab.setText(titles[i]);
            tab.setIcon(R.mipmap.ic_launcher);
            tabLayout.addTab(tab, i, i == position);//这一句要先有监听才能选中fragment
        }
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        //刚进来没有执行
        Fragment fragmentFrom = fragments[position];//获取当前显示的fragment
        Fragment fragmentTo = (Fragment) tab.getTag();
        if (fragmentTo == null) {
            fragmentTo = getItem(tab.getPosition());
            tab.setTag(fragmentTo);
        }
        position = tab.getPosition();//给position赋新值
        switchFragment(fragmentFrom, fragmentTo);
    }

    /**
     * 切换显示fragment
     *
     * @param fragmentFrom
     * @param fragmentTo
     */
    private void switchFragment(Fragment fragmentFrom, Fragment fragmentTo) {
        fragmentTransaction = fragmentManager.beginTransaction();//获取事务
        //隐藏from
        if (fragmentFrom != null) {
            fragmentTransaction.hide(fragmentFrom);
        }
        //先查找fragmentTo是否已经被装载
        Fragment fragment = fragmentManager.findFragmentByTag(fragmentTo.getClass().getName());
        //如果fragmentTo不存在就装载,存在就显示
        if (fragment == null) {
            fragmentTransaction.add(R.id.frame_container, fragmentTo);
        } else {
            fragmentTransaction.show(fragmentTo);
        }
        fragmentTransaction.commit();
    }

    /**
     * 创建fragment
     *
     * @param position
     * @return
     */
    private Fragment getItem(int position) {
        Fragment fragment = new MyFragment();
        Bundle bundle = new Bundle();
        bundle.putString("title", titles[position]);
        fragment.setArguments(bundle);
        fragments[position] = fragment;
        return fragment;
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值