我知道许多应用程序在 ViewPager 中使用片段.我的应用程序中也需要它.我没有找到任何指南,我该怎么做才能排除 这个.这是我的代码:
I know many applications using fragments in ViewPager. I need it in my application too. I have no found any guide how I can to do it exclude this. And here is my code:
public class MainActivity extends FragmentActivity {
Vector<Fragment> fragments;
ViewPager viewPager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
viewPager = (ViewPager) findViewById(R.id.pager);
fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, Fragment1.class.getName()));
fragments.add(Fragment.instantiate(this, Fragment2.class.getName()));
viewPager.setOffscreenPageLimit(fragments.size());
PagerAdapter pagerAdapter = new PagerAdapter(super.getSupportFragmentManager(), fragments);
viewPager.setAdapter(pagerAdapter);
viewPager.setCurrentItem(0);
}
class PagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public PagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
@Override
public int getCount() {
return this.fragments.size();
}
}
}
但是这段代码不能很好地工作.有时我在 Fragment1 中收到关于 getActivity() 返回 null 的错误.我从该站点和其他有关此错误的帖子中阅读了许多帖子,现在我知道这是不好的方法-使用 Vector 来存储片段.但我仍然不知道,如何正确使用片段进行 ViewPager.请帮忙.
But this code doesn't working well. Sometimes I get error in my Fragment1 about getActivity() return null. I read many posts from this site and other about this error and now I know that is bad way - use Vector for storing fragments. But I still don't know, how to do ViewPager with fragments properly. Please, help.
即使我一开始也很难理解这个概念,我不会说我完全理解它.但是,
Even I had trouble understanding the concept at first and I won't say that I fully understood it. But,
这是我在示例应用程序中使用片段的方式:
Here's how I am using fragment in my sample application:
第 1 步:创建我的适配器:
Step 1: Creating my Adapter:
public class AllPagesAdapter extends FragmentStatePagerAdapter {
public AllPagesAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new Fragment1();
case 1:
return new Fragment2();
case 2:
return new Fragment3();
case 3:
return new Fragment4();
case 4:
return new Fragment5();
}
return null;
}
@Override
public int getCount() {
return 5;
}
}
第 2 步:在主要活动中,我正在扩展片段活动并实现 ActionBar.TabListener.这是我的做法:
Step 2: In the main activity, I am extending fragment activity and implementing an ActionBar.TabListener. Here's how I am doing it:
public class SomeActivity extends FragmentActivity implements ActionBar.TabListener {
public ViewPager viewPager;
private AllPagesAdapter mAdapter;
private ActionBar actionBar;
private String [] tabs = {"Fragment1","Fragment2","Fragment3","Fragment4","Fragment5"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initializing all stuff
viewPager = (ViewPager)findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new AllPagesAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//Add the tabs here
for(String tab_name:tabs){
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
@Override
public void onPageSelected(int position){
//on Page change, that particular page should be selected
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0,float arg1,int arg2){
}
@Override
public void onPageScrollStateChanged(int position){
}
});
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction fragmentTransaction) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction fragmentTransaction) {
viewPager.setCurrentItem(tab.getPosition());
}
}
第 3 步:我只有一个片段用于此示例:
Step 3: I have just one fragment for this example:
代码如下:
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Can be layout you want it to be.
View databaseview = inflater.inflate(R.layout.database, container, false);
return databaseview;
}
}
第 4 步:最后一部分是布局:这将是起点.
Step 4: Last part is the layout: This will be starting point.
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v4.view.ViewPager>
最后,一件重要的事情是,片段的数量应该等于主活动中定义的字符串数组.如果没有,应用程序会崩溃.
Lastly, one important thing is that, number of fragments should be equal to the string array defined in the main activity. If not, the app crashes.
希望我解释清楚..:)
Hope I explained it well..:)
我知道这是一篇很老的帖子,但这个答案可供未来的观众使用.
I know this is a very old post but this answer can be used for future viewers.
这篇关于如何正确使用 ViewPager 的片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!