织梦CMS - 轻松建站从此开始!

沙龙国际开户_沙龙国际www.salon365.com

在Fragment中使用TabLayout和ViewPager时,发现TabLayou

时间:2017-07-18 03:03来源:未知 作者:admin 点击:
package com.masterzhang.www.fragment;import android.app.Activity;import android.net.Uri;import android.os.Bundle;import android.support.design.widget.TabLayout;import android.support.v4.app.Fragment;import android.support.v4.view.ViewPager;
package com.masterzhang.www.fragment;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.masterzhang.www.ecourier.R;
import com.masterzhang.www.fragmentPagerAdapter.TrackGetFragmentPagerAdapter;

/**
 * 领取追踪主页面
 */
public class TrackGetMain extends Fragment {
    private static TrackGetMain tractGetMain = null;
    private TrackGetFragmentPagerAdapter mAdapter;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private View mainView;
    public static TrackGetMain newInstance() {
        if( tractGetMain == null )
        {
            synchronized (TrackGetMain.class)
            {
                if( tractGetMain == null)
                {
                    tractGetMain = new TrackGetMain();
                }
            }
        }
        return tractGetMain;
    }

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

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

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

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {

    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onStart() {
        super.onStart();
        mAdapter = new TrackGetFragmentPagerAdapter(getActivity().getSupportFragmentManager());
        tabLayout = (TabLayout) mainView.findViewById(R.id.tabs);
        viewPager = (ViewPager) mainView.findViewById(R.id.vp_view);
        viewPager.setAdapter(mAdapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
    }
}
package com.masterzhang.www.fragmentPagerAdapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;

import com.masterzhang.www.fragment.PersonMessage;
import com.masterzhang.www.fragment.TalkList;
import com.masterzhang.www.fragment.TrackGet;

public class TrackGetFragmentPagerAdapter extends FragmentPagerAdapter {
    final int PAGE_COUNT = 3;
    private String titles[] = new String[]{"未配送","未送达","未评价"};

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


    @Override
    public int getCount() {
        return PAGE_COUNT;
    }

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

    @Override
    public Fragment getItem(int position) {
        if( position == 0)
        {
            return TrackGet.newInstance();
        }
        else if( position == 1 )
        {
            return PersonMessage.newInstance();
        }
        else
        {
            return TalkList.newInstance();
        }
    }
}

效果如图所示

织梦二维码生成器
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
推荐内容