Android Fragment未在Viewpager中显示

时间:2014-05-15 18:21:56

标签: android android-fragments

我有一个Android应用程序,使用滑动标签在片段之间导航。我在xml文件中定义了2个片段和1个主要活动。每当我运行我的应用程序时,我都会使用正确的名称“管理”,“扫描”,“关于”来正确显示选项卡......但页面为空白。当我单步调试我的调试器以检查onCreateView的返回值(在我下面的FragmentPagerAdapter代码中)时,它似乎返回正确的值...我无法弄清楚为什么我一直得到一个空白屏幕。有人可以帮忙吗?

这是我的 activity_main.xml

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.atd.giftmonster.MainActivity" />

这是我的 MainActivity.java

public class MainActivity extends ActionBarActivity implements ActionBar.TabListener {

ViewPager mViewPager;
DFragmentManager mFragmentManager;

private static EditText textEmail;
private static EditText textPw;

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

    List<String> titles = new ArrayList<String>();
    titles.add("administrator");
    titles.add("scan");
    titles.add("about");

    List<Integer> layoutids = new ArrayList<Integer>();
    layoutids.add(R.layout.fragment_admin);
    layoutids.add(R.layout.fragment_scan);
    layoutids.add(R.layout.fragment_scan);

    // Set up the action bar.
    final ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    mFragmentManager = new DFragmentManager(getSupportFragmentManager(), layoutids, titles);

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mFragmentManager);

    // When swiping between different sections, select the corresponding
    // tab. We can also use ActionBar.Tab#select() to do this if we have
    // a reference to the Tab.
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }
    });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mFragmentManager.getCount(); i++) {
        actionBar.addTab(
                actionBar.newTab()
                        .setText(mFragmentManager.getPageTitle(i))
                        .setTabListener(this));
    }
}

这是我的 FragmentPagerAdapter w / 静态片段类

public class DFragmentManager extends FragmentPagerAdapter {

List<String> mTitle;
List<Integer> mLayoutID;
List<Fragment> mFragments;
FragmentManager mFragmentManager;

public DFragmentManager(FragmentManager fm, List<Integer> layoutIDs, List<String> layoutTitles)
{
    super(fm);
    mFragments = new ArrayList<Fragment>();

    mLayoutID = layoutIDs;
    mFragmentManager = fm;
    mTitle = layoutTitles;

    int size = mLayoutID.size();

    for (int i = 0; i < size; i++)
    {

        mFragments.add(DFragment.newInstance(mLayoutID.get(i)));
    }
}

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

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

@Override
public CharSequence getPageTitle(int position) {
    //return (CharSequence)new String("ss");
    //return (CharSequence) mFragments.get(position).getTitle();
    return (CharSequence) mTitle.get(position);
}

public Fragment getFragment(int position)
{
    return mFragments.get(position);
}

public static class DFragment extends Fragment
{
    private View mView;
    private static final String ARG_LAYOUT_ID = "layout_id";

    public DFragment()
    {
        mView = null;
    }

    public static final DFragment newInstance(int layoutID)
    {
        DFragment fragment = new DFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(ARG_LAYOUT_ID, layoutID);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle bundle)
    {
        int resourceID = getArguments().getInt(ARG_LAYOUT_ID);
        mView = inflater.inflate(resourceID, group, false);
        return mView;
    }

    public View getView()
    {
        return mView;
    }
}

编辑:

我修改了这个问题已经有一段时间了,但是我没有将R.id.fragmetnid传递给args中的片段来修复它。 (List layoutIds变量)。相反,我会传递我自己的指定ID,然后在onCreateView中膨胀片段。似乎现在正在运作。

2 个答案:

答案 0 :(得分:0)

在您的活动中尝试此版本的代码:

public class SampleSwipeActivity extends FragmentActivity implements ActionBar.TabListener {

   private ViewPager viewPager;
   private ActionBar actionBar;
   private TabsPagerAdapter mAdapter;


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

      requestWindowFeature(Window.FEATURE_ACTION_BAR);
      setContentView(R.layout.main);

      viewPager = (ViewPager) findViewById(R.id.pager);
      actionBar = getActionBar();

      actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

      String [] tabs = new String[]{"Tab1", "Tab2", "Tab3", "Tab4"};


      for(String tab : tabs)
      {
         actionBar.addTab(actionBar.newTab()
                .setText(tab)
                .setTabListener(this));
      }

      mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
      viewPager.setAdapter(mAdapter);

   }



   @Override
   public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft)
   {
       viewPager.setCurrentItem(tab.getPosition());

       viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener()
       {

          @Override
          public void onPageSelected(int position)
          {
             actionBar.setSelectedNavigationItem(position);
          }

          @Override
          public void onPageScrolled(int arg0, float arg1, int arg2)
          {
          }

          @Override
          public void onPageScrollStateChanged(int arg0)
          {
          }
       });
   }

   @Override
   public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

   }

   @Override
   public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

   }
}

由于您正在使用Fragments,我将使用FragmentActivity而不是ActionBarActivity。

我希望这会有所帮助。

答案 1 :(得分:-2)

在您的代码中,您可以更改

// Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);

用这个

// Set up the ViewPager with the sections adapter.
    mViewPager = (android.support.v4.view.ViewPager) findViewById(R.id.pager);