Java 如何使用滑动视图实现android TabLayout设计支持库
我打算使用android TabLayout设计支持库,但我不知道如何使用滑动视图 这是我的代码 XML:Java 如何使用滑动视图实现android TabLayout设计支持库,java,android,xml,android-tablayout,Java,Android,Xml,Android Tablayout,我打算使用android TabLayout设计支持库,但我不知道如何使用滑动视图 这是我的代码 XML: 将这一行添加到java代码:-tabLayout.setTabMode(tabLayout.MODE\u可滚动) 或者,您也可以在xml代码中指定:- app:tabMode=“scrollable”派对有点晚,但要做到这一点,你必须使用ViewPager类,并为每个视图使用片段(在选项卡下)。然后将ViewPager附加到TabLayout实例和宾果!你有你的刷卡表 下面是我使用两个选项
将这一行添加到java代码:-
tabLayout.setTabMode(tabLayout.MODE\u可滚动)代码>
或者,您也可以在xml代码中指定:-
app:tabMode=“scrollable”
派对有点晚,但要做到这一点,你必须使用ViewPager
类,并为每个视图使用片段(在选项卡下)。然后将ViewPager
附加到TabLayout
实例和宾果!你有你的刷卡表
下面是我使用两个选项卡的一些工作代码:
MyActivity.java:
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
// Initializing tab and pager views
TabLayout tabLayout = (TabLayout) findViewById(R.id.my_tab_layout);
final ViewPager viewPager = (ViewPager) findViewById(R.id.my_view_pager);
// Making new tabs and adding to tabLayout
tabLayout.addTab(tabLayout.newTab().setText("First Tab"));
tabLayout.addTab(tabLayout.newTab().setText("Second Tab"));
// Adding fragments to a list
List<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 ViewPager
viewPager.setAdapter(new SectionPagerAdapter(getSupportFragmentManager(), fragments));
tabLayout.setupWithViewPager(viewPager);
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);
}
}
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);
}
}
MySecondTabFragment.java:
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
// Initializing tab and pager views
TabLayout tabLayout = (TabLayout) findViewById(R.id.my_tab_layout);
final ViewPager viewPager = (ViewPager) findViewById(R.id.my_view_pager);
// Making new tabs and adding to tabLayout
tabLayout.addTab(tabLayout.newTab().setText("First Tab"));
tabLayout.addTab(tabLayout.newTab().setText("Second Tab"));
// Adding fragments to a list
List<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 ViewPager
viewPager.setAdapter(new SectionPagerAdapter(getSupportFragmentManager(), fragments));
tabLayout.setupWithViewPager(viewPager);
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);
}
}
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>
<?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>
<?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>
my\u first\u fragment\u 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>
<?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>
<?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>
my\u second\u fragment\u 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>
<?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>
<?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
希望有帮助。这将使选项卡本身可以滚动,而不会在选项卡下滑动视图。谢谢兄弟,您的解决方案帮助了我。@Sourabh无需担心,伙计:)非常感谢。你已经在整个互联网上做了关于这个的最简单的教程。再次感谢你。