我将使用android TabLayout设计支持库,但我不知道如何使用滑动视图.
这是我的代码
XML:
<androID.support.design.Widget.TabLayout androID:ID="@+ID/tabLayout" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content"/>
Java:
TabLayout tabLayout;tabLayout = (TabLayout) findVIEwByID(R.ID.tabLayout); tabLayout.addTab(tabLayout.newTab().setText("Tab 1")); tabLayout.addTab(tabLayout.newTab().setText("Tab 2")); tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));
解决方法:
聚会有点晚了,但是要做到这一点,您必须使用VIEwPager类并为每个视图(在选项卡下)使用片段.然后将VIEwPager附加到您的TabLayout实例和宾果游戏!您有滑动tabLayout.
这是我的一些使用两个标签的工作代码:
MyActivity.java:
super.onCreate(savedInstanceState);setContentVIEw(R.layout.main_layout);// Initializing tab and pager vIEwsTabLayout tabLayout = (TabLayout) findVIEwByID(R.ID.my_tab_layout);final VIEwPager vIEwPager = (VIEwPager) findVIEwByID(R.ID.my_vIEw_pager);// Making new tabs and adding to tabLayouttabLayout.addTab(tabLayout.newTab().setText("First Tab"));tabLayout.addTab(tabLayout.newTab().setText("Second Tab"));// Adding fragments to a ListList<Fragment> fragments = new Vector<Fragment>();fragments.add(Fragment.instantiate(this, MyFirstTabFragment.class.getname()));fragments.add(Fragment.instantiate(this, MySecondTabFragment.class.getname()));// Attaching fragments into tabLayout with VIEwPagervIEwPager.setAdapter(new SectionPagerAdapter(getSupportFragmentManager(), fragments));tabLayout.setupWithVIEwPager(vIEwPager);
SectionPagerAdapter.java
import androID.support.v4.app.Fragment;import androID.support.v4.app.FragmentManager;import androID.support.v4.app.FragmentPagerAdapter;import java.util.List;public class SectionPagerAdapter extends FragmentPagerAdapter { private List<Fragment> fragments; public SectionPagerAdapter(FragmentManager fm, List<Fragment> fragments) { super(fm); this.fragments = fragments; } @OverrIDe public Fragment getItem(int position) { return this.fragments.get(position); } @OverrIDe public int getCount() { return this.fragments.size(); } @OverrIDe public CharSequence getPageTitle(int position) { switch (position) { case 0: return "First Tab"; case 1: default: return "Second Tab"; } }}
MyFirstTabFragment.java:
public class MyFirstTabFragment extends Fragment { @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container, Bundle savedInstanceState) { // Declare your first fragment here return inflater.inflate(R.layout.my_first_fragment_layout, container, false); }}
MySecondTabFragment.java:
public class MySecondTabFragment extends Fragment { @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container, Bundle savedInstanceState) { // Declare your second fragment here return inflater.inflate(R.layout.my_second_fragment_layout, container, false); }}
main_layout.xml
<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:orIEntation="vertical" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent"> <!-- Declare androID.support.v7.Widget.Toolbar or... here --> <androID.support.design.Widget.TabLayout androID:ID="@+ID/my_tab_layout" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content"/> <androID.support.v4.vIEw.VIEwPager androID:ID="@+ID/my_vIEw_pager" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:layout_below="@ID/my_tab_layout"/></relativeLayout>
my_first_fragment_layout.xml
<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent"> <!-- Declare first tab layout here --></relativeLayout>
my_second_fragment_layout.xml
<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent"> <!-- Declare second tab layout here --></relativeLayout>
注意:在这里,我们使用支持库v4中的VIEwPager,Fragment,FragmentManager和FragmentPagerAdapter.
希望能帮助到你.
总结以上是内存溢出为你收集整理的java-如何使用Swipe视图实现android TabLayout设计支持库全部内容,希望文章能够帮你解决java-如何使用Swipe视图实现android TabLayout设计支持库所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)