Fragment簡單複用(找錯中)

首先假設第一個Fragment是單獨的剩下的全是相同佈局的Fragment因此爲了高端大氣上檔次咱們複用它網絡

建立兩個Fragment第一無論它,主要是第二個須要複用的Fragmentide

public class BlankFragment extends Fragment {

    private ImageView iv;
    private String name;
    private TextView tv;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //獲取傳回來的值
        Bundle bundle = getArguments();
        if (bundle != null) {
            //拿到值能夠判斷在那個頁面,而後操做啦
            name = bundle.get("name").toString();
            Log.d("MyFragment", name);
        }
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_blank, container, false);
        initView(inflate);
        tv.setText(name);
        return inflate;
    }
    //設置方法給Activity傳值(重點要考)
    public static BlankFragment newInstance(String name) {
        Bundle args = new Bundle();
        args.putString("name", name);
        BlankFragment fragment = new BlankFragment();
        fragment.setArguments(args);
        return fragment;
    }
    private void initView(View inflate) {
        iv = (ImageView) inflate.findViewById(R.id.iv);
        tv = (TextView) inflate.findViewById(R.id.tv);
    }
}

而後是Activity中佈局

public class MainActivity extends AppCompatActivity {

    private ViewPager vp;
    private TabLayout tabLayout;
    private MainAdapter mainAdapter;
    private List<Fragment> fragments = new ArrayList<>();

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

        vp = (ViewPager) findViewById(R.id.vp);
        tabLayout = (TabLayout) findViewById(R.id.tab);

        mainAdapter = new MainAdapter(getSupportFragmentManager());
        fragments.add(new AFragment());
        fragments.add(BlankFragment.newInstance("第一個fragment"));
        fragments.add(BlankFragment.newInstance("第二個fragment"));
        fragments.add(BlankFragment.newInstance("第三個fragment"));
        mainAdapter.setFragments(fragments);
        vp.setAdapter(mainAdapter);
        //設置tabLayout
        tabLayout.setupWithViewPager(vp);
        //設置文字的顏色
        tabLayout.setTabTextColors(Color.GRAY, Color.BLUE);
        //設置下劃線的顏色
        tabLayout.setSelectedTabIndicatorColor(Color.BLUE);
    }
}

主要是適配器中this

public class MainAdapter extends FragmentPagerAdapter{
    private List<Fragment> fragments;
    //設置你TabLayout標籤名
    private String[] titles = {"一頁", "二頁", "三頁", "si頁"};

    public MainAdapter(FragmentManager fm) {
        super(fm);
    }
    //重點 要手寫
    public void setFragments(List<Fragment> fragments) {
        this.fragments = fragments;
    }

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

    @Override
    public int getCount() {
        return fragments != null ? fragments.size() : 0;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }

ok完工,須要傳不一樣的數據就能夠在Fragment中判斷標籤名字,而後網絡請求神馬的了spa

 

利用newInstance()方法實例化fragment

這種狀況下,newInstance()方法是一種「靜態工廠方法」,讓咱們在初始化和設置一個新的fragment的時候省去調用它的構造方法和額外的setter方法。爲你的Fragment提供靜態工廠方法是一種好的作法,由於它封裝和抽象了在客戶端構造對象所需的步驟,所以Google推薦使用,所咱們就使用吧!對象

相關文章
相關標籤/搜索