我尝试在我的活动中添加一个带有片段的标签,但它没有响应或事件显示。
以下是我的代码。
这是我的主要活动:
private void setupTabLayout() {
TabLayout tabLayout = findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("COMMENTS"));
tabLayout.addTab(tabLayout.newTab().setText("ATTACHMENTS"));
tabLayout.addTab(tabLayout.newTab().setText("AUDIT TRAIL"));
tabLayout.addTab(tabLayout.newTab().setText("APPROVERS"));
final ViewPager viewPager = findViewById(R.id.pager);
final PagerAdapter adapter = new com.example.android.ontrack.adapters.PagerAdapter(getSupportFragmentManager(),tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
Here is my XML code:
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="@dimen/margin"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/margin"
android:paddingLeft="@dimen/padding"
android:paddingRight="@dimen/padding"
android:background="@color/blue"
android:fillViewport="true"
app:tabMode="scrollable"
app:tabIndicatorColor="@android:color/white"
app:tabSelectedTextColor="@android:color/white"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
这是我的PagerAdapter:
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int mNumOfTabs) {
super(fm);
this.mNumOfTabs = mNumOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
CommentsFragment tab1 = new CommentsFragment();
return tab1;
case 1:
AttachmentsFragment tab2 = new AttachmentsFragment();
return tab2;
case 2:
AuditTrailFragment tab3 = new AuditTrailFragment();
return tab3;
case 3:
ApproversFragment tab4 = new ApproversFragment();
return tab4;
default:
return null;
}
}
@Override
public int getCount() {
return mNumOfTabs;
}
}
答案 0 :(得分:3)
您只需致电tabLayout.setupWithViewPager(viewPager);
似乎XML中存在问题,TabLayout
应位于ViewPager
因为
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
表示您想要更改选项卡,每个选项卡都托管一个片段。
更新:您已删除了添加标签的代码,如评论中发布的github链接所示。
// add these lines again
TabLayout tabLayout = findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("COMMENTS"));
tabLayout.addTab(tabLayout.newTab().setText("ATTACHMENTS"));
tabLayout.addTab(tabLayout.newTab().setText("AUDIT TRAIL"));
tabLayout.addTab(tabLayout.newTab().setText("APPROVERS"));
ViewPager viewPager = (ViewPager)findViewById(R.id.viewPager);
viewPager.setAdapter(new CustomFragmentPagerAdapter(getSupportFragmentManager()));
viewPager.setCurrentItem(2);
tabLayout.setupWithViewPager(viewPager);
//... code of listener etc
答案 1 :(得分:1)
这是我的样本。它应该工作。 https://github.com/ElifBon/FragmentAdapterProject
的xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"></ViewPager>
</LinearLayout>
您的活动:
TabLayout tabLayout = (TabLayout)findViewById(R.id.tabLayout);
ViewPager viewPager = (ViewPager)findViewById(R.id.viewPager);
viewPager.setAdapter(new CustomFragmentPagerAdapter(getSupportFragmentManager()));
viewPager.setCurrentItem(2);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
// TODO : Something
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
// TODO : Something
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
适配器:
private class CustomFragmentPagerAdapter extends FragmentPagerAdapter{
Fragment1 fragment1;
Fragment2 fragment2;
Fragment3 fragment3;
public CustomFragmentPagerAdapter(android.support.v4.app.FragmentManager fm){
super(fm);
}
@Override
public Fragment getItem(int position) {
if(position == 0){
if(fragment1 == null){
fragment1 = Fragment1.getInstance();
}
return fragment1;
}else if(position == 1){
if(fragment2 == null){
fragment2 = Fragment2.getInstance();
}
return fragment2;
}else if(position == 2){
if(fragment3 == null){
fragment3 = Fragment3.getInstance();
}
return fragment3;
}else{
return null;
}
}
@Override
public CharSequence getPageTitle(int position) {
return "Title - " + String.valueOf(position);
}
@Override
public int getCount() {
return 3;
}
}