Android 无法在片段之间交换,并且文本也未显示在tablayout中
我已将view pager和TableLayout用于选项卡式活动。单击选项卡正在更改,但问题是我无法在片段之间交换,并且片段中的文本未显示。使用Android 无法在片段之间交换,并且文本也未显示在tablayout中,android,android-fragments,android-viewpager,android-tablayout,Android,Android Fragments,Android Viewpager,Android Tablayout,我已将view pager和TableLayout用于选项卡式活动。单击选项卡正在更改,但问题是我无法在片段之间交换,并且片段中的文本未显示。使用tabLayout.setupWithViewPager(viewPager)正在使我的选项卡不可见 以下是我的活动\u主代码: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/
tabLayout.setupWithViewPager(viewPager)代码>正在使我的选项卡不可见
以下是我的活动\u主代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:minHeight="?attr/actionBarSize"
android:theme="@style/Theme.AppCompat.DayNight.DarkActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<TextView
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:paddingTop="10dp"
android:gravity="center_horizontal"
android:text="Sample ViewPager"
android:textColor="@color/colorPrimary"
android:textSize="20sp"
android:textStyle="bold"
android:theme="@style/Theme.AppCompat.DayNight.DarkActionBar"
/>
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tablayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@android:color/transparent"
android:minHeight="?attr/actionBarSize"
android:theme="@style/Theme.AppCompat.DayNight.DarkActionBar"
app:tabMode="scrollable"
app:tabTextColor="@color/colorPrimary">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viepager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</LinearLayout>
以下是我的适配器类代码:
package com.example.sidrajawaid.demoviewpager;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
public class PagerAdapter extends FragmentStatePagerAdapter {
int numberOfTabs;
public PagerAdapter(FragmentManager fm,int tabs) {
super(fm);
this.numberOfTabs=tabs;
}
@Override
public Fragment getItem(int position) {
{
switch (position){
case 0:
tab1 tab_1=new tab1();
return tab_1;
case 1:
tab2 tab_2=new tab2();
return tab_2;
case 2:
tab3 tab_3=new tab3();
return tab_3;
}
return null;
}
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return super.getPageTitle(position);
}
@Override
public int getCount() {
return 0;
}
}
下面是tab1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".tab1">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/hello_blank_fragment" />
</RelativeLayout>
解决我的两个问题:
1-在选项卡之间交换。
2-内容未显示在片段中 问题就在这里
PagerAdapter pagerAdapter=new PagerAdapter(getSupportFragmentManager(),tabLayout.getTabCount());
第二个参数为0,因为尚未添加任何选项卡。因此,在线条上方剪切,并将其粘贴到线条上方
viewPager.setAdapter(pagerAdapter);
在片段中的onCreate()
方法中,也请检查适配器中的getCount()
。确保返回的值正确。。成功了。现在它正在切换,文本也可见。但当我导航到其他选项卡(如tab#3或tab#4)时,应用程序正在崩溃并出现以下错误。@SidraJawaid,欢迎!!快乐编码!!
viewPager.setAdapter(pagerAdapter);