0

我正在开发一个带有 android 2.1 包的应用程序。我正在使用 ActionBarSherlock 来获取操作栏。然后我尝试使用一个 Viewpager 和 multilpe Fragments Class 并拥有自己的布局。

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

我也使用了这个问题中给出的建议,但没有效果 请参阅此处的问题

这是我的带有 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 中的片段之一

<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" />

4

2 回答 2

1

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

于 2013-03-09T13:39:22.550 回答
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) {

    }

}
于 2014-04-28T07:39:04.003 回答