使用Viewpager和ABS的Android多片段

时间:2013-03-09 13:18:51

标签: android android-viewpager

我正在使用android 2.1软件包开发一个应用程序。 我正在使用ActionBarSherlock获取操作栏。 然后我尝试使用一个Viewpager和multilpe Fragments Class以及它自己的布局。

但我得到的只是第一个片段。

我也使用了这个问题中给出的建议,但没有效果 See the Question Here

以下是我使用ActionBar和Viewpager的MainActivity。

public class MainActivity extends SherlockFragmentActivity {

 private static final int NUMBER_OF_PAGES = 2;
 private MyFragmentPagerAdapter mMyFragmentPagerAdapter;

ActionBar actionBar;
ViewPager viewPager;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    actionBar = getSupportActionBar();
    actionBar.setHomeButtonEnabled(true);
    //actionBar.setNavigationMode(actionBar.NAVIGATION_MODE_TABS);

    viewPager =  (ViewPager) findViewById(R.id.pager);
    //viewPager.setOffscreenPageLimit(4);

     mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
     viewPager.setAdapter(mMyFragmentPagerAdapter);

}



@Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("tab", getSupportActionBar().getSelectedNavigationIndex());
    }


  private static class MyFragmentPagerAdapter extends FragmentPagerAdapter {  

      public MyFragmentPagerAdapter(FragmentManager fm) {  
           super(fm);  
      }  

      @Override  
      public Fragment getItem(int index) { 

          Fragment f = new Fragment();
          Log.d("index  ", ""+index);

          switch (index) {
            case 0:
                f = PageFragment.newInstance(index);
            break;
            case 1:
                f = FragmentTwo.newInstance(index);
            break;


            default:
            break;
            }

          return f;


      }  

      @Override  
      public int getCount() {  

           return NUMBER_OF_PAGES;  
      }  
 }  

}

这是我的一个片段类。

public class PageFragment extends SherlockFragment {  

 public static PageFragment newInstance(int title) {

     PageFragment pageFragment = new PageFragment();
     Bundle bundle = new Bundle();
     bundle.putInt("title", title);
     pageFragment.setArguments(bundle);
     return pageFragment;
 }

 @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, container, false);  
     TextView textView = (TextView) view.findViewById(R.id.textView1);  
     textView.setText("View # 01");

     return view;  
 }  

}

以下是activity_main.xml XML

中的My片段之一
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >


 <android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

  <fragment android:name="com.mzubair.test.PageFragment"
          android:id="@+id/headlines_fragment"
          android:layout_weight="1"
          android:layout_width="0dp"
          android:layout_height="match_parent" />
  <fragment android:name="com.mzubair.test.FragmentTwo"
          android:id="@+id/headlines_fragment2"
          android:layout_weight="1"
          android:layout_width="0dp"
          android:layout_height="match_parent" />

2 个答案:

答案 0 :(得分:1)

从XML中删除两个片段标记。

答案 1 :(得分:0)

尝试使用此PagerAdapter(根据您的代码进行调整),然后从您的布局中删除那些您不希望将您的片段添加到viewpager下方或旁边的片段:/

public class AdapterTabs extends FragmentPagerAdapter implements
        ActionBar.TabListener, ViewPager.OnPageChangeListener {
    private final Context mContext;
    private final ActionBar mActionBar;
    private final ViewPager mViewPager;
    protected final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

    static final class TabInfo {
        protected final Class<?> clss;
        protected final Bundle args;

        TabInfo(Class<?> _class, Bundle _args) {
            clss = _class;
            args = _args;
        }
    }

    public AdapterTabs(SherlockFragmentActivity activity, ViewPager pager) {
        super(activity.getSupportFragmentManager());
        mContext = activity;
        mActionBar = activity.getSupportActionBar();
        mViewPager = pager;
        mViewPager.setAdapter(this);
        mViewPager.setOnPageChangeListener(this);
    }

    public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
        TabInfo info = new TabInfo(clss, args);
        tab.setTag(info);
        tab.setTabListener(this);
        mTabs.add(info);
        mActionBar.addTab(tab);
        notifyDataSetChanged();
    }

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

    @Override
    public Fragment getItem(int position) {
        TabInfo info = mTabs.get(position);
        return Fragment.instantiate(mContext, info.clss.getName(), info.args);
    }

    public void onPageScrolled(int position, float positionOffset,
            int positionOffsetPixels) {

    }

    public void onPageSelected(int position) {
        mActionBar.setSelectedNavigationItem(position);
        ((SherlockFragmentActivity) mContext).supportInvalidateOptionsMenu();
    }

    public void onPageScrollStateChanged(int state) {

    }

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // force l'appel de onOptionMenuPrepared
        ((SherlockFragmentActivity) mContext).supportInvalidateOptionsMenu();
        Object tag = tab.getTag();
        for (int i = 0; i < mTabs.size(); i++) {
            if (mTabs.get(i) == tag) {
                mViewPager.setCurrentItem(i);
            }
        }
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {

    }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {

    }

}