当前位置:  开发笔记 > Android > 正文

使用ViewPagerAdapter在TabLayout中未调用片段onCreateView方法

如何解决《使用ViewPagerAdapter在TabLayout中未调用片段onCreateView方法》经验,为你挑选了1个好方法。

我已按照此链接实施使用Tabs的 tablayout Android Material Design

现在我做了什么,我使用片段代替Activity for tablayout.

以下是代码: -

public class HopitalTabLayoutFragment extends Fragment {

private TabLayout tabLayout;
private ViewPager viewPager;

public HopitalTabLayoutFragment() {

}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_hopital_tab_layout, container, false);

    viewPager = (ViewPager)view. findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout)view. findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);

    return view;
}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());

    adapter.addFragment(new OneFragment(), "ONE");
    adapter.addFragment(new TwoFragment(), "TWO");
    adapter.addFragment(new ThreeFragment(), "THREE");

    viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter   {
    private final List mFragmentList = new ArrayList<>();
    private final List mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }



    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}
}

}

并从活动中添加此片段: -

public class HomeActivityDrawer extends AppCompatActivity {

private Toolbar toolbar;
private NavigationView navigation_view_left, navigation_view_right;
private DrawerLayout drawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_drawer);


drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
header = (RelativeLayout) findViewById(R.id.header);

toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

fragment = new HopitalTabLayoutFragment();
}
}

@Override protected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState); 的setContentView(R.layout.activity_home_drawer);

public class TwoFragment extends Fragment {



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_two, container, false);

    TextView tv = (TextView) view.findViewById(R.id.tv);
    tv.setText("DONE");
       return view;
}
}

fragment = new HopitalTabLayoutFragment(); }}

这些是我在标签布局中使用的三个片段: -

public class HopitalTabLayoutFragment extends Fragment {

private TabLayout tabLayout;
private ViewPager viewPager;

public HopitalTabLayoutFragment() {

}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_hopital_tab_layout, container, false);

    viewPager = (ViewPager)view. findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout)view. findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);

    return view;
}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());

    adapter.addFragment(new OneFragment(), "ONE");
    adapter.addFragment(new TwoFragment(), "TWO");
    adapter.addFragment(new ThreeFragment(), "THREE");

    viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter   {
    private final List mFragmentList = new ArrayList<>();
    private final List mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }



    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}
}

同样一和三.

现在的问题是,第一次加载所有三个片段并运行onCreateView方法,但是当我再次打开tablayout时,只有片段One启动而不是第二个片段.这段代码出了什么问题?请帮忙!!



1> 小智..:

导入FragmentStatePagerAdapter而不是FragmentPagerAdapter.

import android.support.v4.app.FragmentPagerAdapter;

import android.support.v4.app.FragmentStatePagerAdapter;

然后从FragmentStatePagerAdapter:扩展
class ViewPagerAdapter extends FragmentPagerAdapter

class ViewPagerAdapter extends FragmentStatePagerAdapter

推荐阅读
惬听风吟jyy_802
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有