所以我正在尝试在主导航栏下方的标签栏和布局处理我的主要活动。
我似乎无法获取ViewPager / TabLayout(不确定导致问题的原因,对此是新的)加载正确的片段。我已经阅读了大量的文档,并检查了超过1个教程,看看我是否正确地做了这个,看起来我做的是同样的事情......
我的OnTabSelectedListener
的{{1}}正在看到标签更改正确,并在TabLayout
我正在呼叫OnTabSelected
尝试设置我的观看者项目,但它仍然无法正常工作。首次加载时,它会加载第一个和第二个选项卡(我可以通过片段onCreate方法发送的API请求看到),当我点击其他选项卡时,它会一直加载错误的片段,有时甚至根本不加载一个!
不确定这是否相关但我在切换标签时收到此警告:
viewPager.setCurrentItem(tab.getPosition())
我正在使用构建工具版本03-03 16:24:22.940 16967-16967/com.sampleapp.app W/FragmentManager: moveToState: Fragment state for BlankFragment3{efddbec #2 id=0x7f0c007b android:switcher:2131492987:2} not updated inline; expected state 3 found 2
和以下依赖项:
23.0.2
这是我的主导航栏的布局代码:
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.android.support:support-v4:23.1.1'
<?xml version="1.0" encoding="utf-8"?>
根据多个教程,这里是我用于适配器的代码
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar1"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar2"
android:layout_width="match_parent"
android:layout_height="10dp"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_input_add" />
</android.support.design.widget.CoordinatorLayout>
以下是我如何设置我的viewpager和tablayout
MainActivity的onCreate:
public class TabsAdapter extends FragmentPagerAdapter{
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public TabsAdapter(FragmentManager fm) {
super(fm);
}
public void addFrag(Fragment fragment, String title){
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
最后,我的setupViewPager方法:
ViewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(mViewPager); // Method defined below
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(mViewPager);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
mViewPager.setCurrentItem(tab.getPosition());
Log.w("Tab Selection", String.valueOf(tab.getPosition()));
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
如果有人发现任何问题,请告诉我,我不能为我的生活获取这些标签加载正确的片段。
提前致谢!
答案 0 :(得分:6)
尝试使用此代码配对:D
ViewPager适配器
public class PageAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PageAdapter(FragmentManager fm,int numTabs) {
super(fm);
this.mNumOfTabs = numTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
RemindsTab remindsTab = new RemindsTab();
return remindsTab;
case 1:
TasksTab tasksTab = new TasksTab();
return tasksTab;
case 2:
QuicklyNotesTab quicklyNotesTab = new QuicklyNotesTab();
return quicklyNotesTab;
default:
return null;
}
}
@Override
public int getCount() {
return mNumOfTabs;
}
}
以及我在 MainActivity 中的代码,用于这些标签:
final TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Recordatorios"));
tabLayout.addTab(tabLayout.newTab().setText("Tareas"));
tabLayout.addTab(tabLayout.newTab().setText("Notas Rapidas"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PageAdapter(getSupportFragmentManager(),tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(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) {
}
});
希望你能解决问题:)
答案 1 :(得分:1)
添加课程
class FragmentHolder{
Fragment fragment;
String title;
public FragmentHolder(Fragment fragmentm String title){
this.fragment = fragment;
this.title = title;
}
}
并在适配器
中public class TabsAdapter extends FragmentPagerAdapter{
private final List<FragmentHolder> mFragmentList = new ArrayList<>();
public TabsAdapter(FragmentManager fm) {
super(fm);
}
public void addFrag(FragmentHolder fragmentHolder){
mFragmentList.add(fragmentHolder);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position).fragment;
}
@Override
public int getCount() {
return mFragmentList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position).title;
}
}
答案 2 :(得分:1)
首先,您必须知道FragmentPageAdaper的工作原理。 FragmentPagerAdapter在开始时实例化两(2)个片段,第一个索引(0)片段和第二个下一个索引(1)片段。 因此,不要尝试使用代码进行操作,只需根据默认的FragmentPageAdapter工作更改您的登录信息。
1)在活动
中添加此行viewPager.setOffscreenPageLimit("your number of fragment");
2)将其添加到您的片段中,这样您就可以在用户在页面之间切换时控制片段操作
// pause video and other list when user switch fragment pager
public void setUserVisibleHint(boolean isVisibleToUser)
{
super.setUserVisibleHint(isVisibleToUser);
if (this.isVisible())
{
if (!isVisibleToUser) // If we are becoming invisible, then...
{
mPlayer.pause();
final SharedPreferences.Editor editor= pref.edit();
editor.putBoolean("PlayerPause", true);
editor.putInt("videotime",mPlayer.getCurrentTimeMillis());
editor.commit();
Log.e("Video "," serUserVisibleHint not visible to User");//pause or stop video
}
if (isVisibleToUser) // If we are becoming visible, then...
{
Log.e("Video "," serUserVisibleHint Visible to User"); //play your video
}
}
}
答案 3 :(得分:0)
@ Riptyde4,我所做的只是将TabsAdapter扩展的类从 FragmentPagerAdapter 更改为 FragmentStatePagerAdapter ,这允许viewPager中的不同状态更改为检测。我这样做了,效果很好。