Tablayout+Fragment

  • Post author:
  • Post category:其他


package bwei.com.myjd.view.fragment;


import android.support.design.widget.TabLayout;
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 java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import bwei.com.myjd.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class FaXianFragment extends BaseFragment {


    @BindView(R.id.tab)
    TabLayout tab;
    @BindView(R.id.vp)
    ViewPager vp;
    private List<Fragment> fragments = new ArrayList<>();
    private List<String> tabs = new ArrayList<>();


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

    @Override
    protected void setClick() {

    }

    @Override
    int getContentview() {
        return R.layout.fragment_fa_xian;
    }

    @Override
    protected void initData() {
        FXFragment_01 fxFragment_01 = new FXFragment_01();
        FXFragment_02 fxFragment_02 = new FXFragment_02();
        FXFragment_03 fxFragment_03 = new FXFragment_03();
        tabs.add("发现1");
        tabs.add("发现2");
        tabs.add("发现3");
        fragments.add(fxFragment_01);
        fragments.add(fxFragment_02);
        fragments.add(fxFragment_03);
        //设置TabLayout的模式
        tab.setTabMode(TabLayout.MODE_FIXED);

        vp.setAdapter(new FxMadapter(getActivity().getSupportFragmentManager()));
        //关联ViewPager和TabLayout
        tab.setupWithViewPager(vp);
    }


    private class FxMadapter extends FragmentPagerAdapter {

        public FxMadapter(FragmentManager fm) {
            super(fm);
        }

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

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

        //这个方法一定要自己手动添上
        @Override
        public CharSequence getPageTitle(int position) {
            return tabs.get(position);

        }
    }
}

网址:https://segmentfault.com/a/1190000009548637



版权声明:本文为Gybk123原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。