无法在片段之间交换,文本也未在 tablayout 中显示
Unable to swap between fragments and text is not shown either in tablayout
我使用了视图寻呼机和 Tablayout for tabbed activity.clicking 选项卡正在改变,但问题是我无法在片段之间交换,片段中的文本未显示。使用 tabLayout.setupWithViewPager(viewPager);
使我的选项卡不可见
这是我的 activity_main 代码:
<?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>
这是主要内容 Activity:
package com.example.sidrajawaid.demoviewpager;
import android.net.Uri;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
public class MainActivity extends AppCompatActivity implements tab1.OnFragmentInteractionListener,tab2.OnFragmentInteractionListener,tab3.OnFragmentInteractionListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ViewPager viewPager=findViewById(R.id.viepager);
TabLayout tabLayout=findViewById(R.id.tablayout);
PagerAdapter pagerAdapter=new PagerAdapter(getSupportFragmentManager(),tabLayout.getTabCount());
tabLayout.addTab(tabLayout.newTab().setText("Log In"));
tabLayout.addTab(tabLayout.newTab().setText("RecyclerView"));
tabLayout.addTab(tabLayout.newTab().setText("Coordinator"));
tabLayout.addTab(tabLayout.newTab().setText("ListView"));
tabLayout.addTab(tabLayout.newTab().setText("Log In"));
tabLayout.addTab(tabLayout.newTab().setText("RecyclerView"));
tabLayout.addTab(tabLayout.newTab().setText("Coordinator"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
viewPager.setAdapter(pagerAdapter);
//tabLayout.setupWithViewPager(viewPager);
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) {
}
});
}
@Override
public void onFragmentInteraction(Uri uri) {
}
}
这是我的适配器 class 代码:
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>
解决我的2个问题:
1- 在选项卡之间切换。
2- 片段中未显示的内容。
问题在这里
PagerAdapter pagerAdapter=new PagerAdapter(getSupportFragmentManager(),tabLayout.getTabCount());
第二个参数是 0,因为还没有添加标签。所以剪下线并将其粘贴到
上方
viewPager.setAdapter(pagerAdapter);
in onCreate()
片段中的方法,请同时检查适配器中的 getCount()
。确保它返回正确的值。
我使用了视图寻呼机和 Tablayout for tabbed activity.clicking 选项卡正在改变,但问题是我无法在片段之间交换,片段中的文本未显示。使用 tabLayout.setupWithViewPager(viewPager);
使我的选项卡不可见
这是我的 activity_main 代码:
<?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>
这是主要内容 Activity:
package com.example.sidrajawaid.demoviewpager;
import android.net.Uri;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
public class MainActivity extends AppCompatActivity implements tab1.OnFragmentInteractionListener,tab2.OnFragmentInteractionListener,tab3.OnFragmentInteractionListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ViewPager viewPager=findViewById(R.id.viepager);
TabLayout tabLayout=findViewById(R.id.tablayout);
PagerAdapter pagerAdapter=new PagerAdapter(getSupportFragmentManager(),tabLayout.getTabCount());
tabLayout.addTab(tabLayout.newTab().setText("Log In"));
tabLayout.addTab(tabLayout.newTab().setText("RecyclerView"));
tabLayout.addTab(tabLayout.newTab().setText("Coordinator"));
tabLayout.addTab(tabLayout.newTab().setText("ListView"));
tabLayout.addTab(tabLayout.newTab().setText("Log In"));
tabLayout.addTab(tabLayout.newTab().setText("RecyclerView"));
tabLayout.addTab(tabLayout.newTab().setText("Coordinator"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
viewPager.setAdapter(pagerAdapter);
//tabLayout.setupWithViewPager(viewPager);
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) {
}
});
}
@Override
public void onFragmentInteraction(Uri uri) {
}
}
这是我的适配器 class 代码:
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>
解决我的2个问题: 1- 在选项卡之间切换。 2- 片段中未显示的内容。
问题在这里
PagerAdapter pagerAdapter=new PagerAdapter(getSupportFragmentManager(),tabLayout.getTabCount());
第二个参数是 0,因为还没有添加标签。所以剪下线并将其粘贴到
上方 viewPager.setAdapter(pagerAdapter);
in onCreate()
片段中的方法,请同时检查适配器中的 getCount()
。确保它返回正确的值。