带有滑动视图的Android标签

时间:2017-09-29 16:01:50

标签: java android android-viewpager

我过去4天一直在使用许多标签式活动和滑动视图源代码,但没有在我的手机上启动...而且我没有找到它的根本原因 现在我正在使用这个Aid Code并按照所有步骤仍然我的应用程序没有启动.... 分享我的代码PLZ检查问题...或者引导我做一些事情

公共类Main2Activity扩展了AppCompatActivity {

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

    ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

    // Add Fragments to adapter one by one
    adapter.addFragment(new OneFragment(), "FRAG1");
    adapter.addFragment(new TwoFragment(), "FRAG2");
    adapter.addFragment(new FragmentThree(), "FRAG3");
    viewPager.setAdapter(adapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);


}



// Adapter for the viewpager using FragmentPagerAdapter
class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

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

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

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

}

片段代码

公共类OneFragment扩展了Fragment {

public OneFragment() {
    // 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
    return inflater.inflate(R.layout.fragment_one, container, false);
}

}

0 个答案:

没有答案