3

我之前已经将此代码用于具有两种布局的滑动选项卡视图,它工作正常。

public class MainActivity extends FragmentActivity {
/**
 * The {@link android.support.v4.view.PagerAdapter} that will provide
 * fragments for each of the sections. We use a
 * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
 * will keep every loaded fragment in memory. If this becomes too memory
 * intensive, it may be best to switch to a
 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
 */
SectionsPagerAdapter mSectionsPagerAdapter;

/**
 * The {@link ViewPager} that will host the section contents.
 */
ViewPager mViewPager;

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

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(
            getSupportFragmentManager());

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




}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a DummySectionFragment (defined as a static inner class
        // below) with the page number as its lone argument.
        Fragment fragment = new DummySectionFragment();
        Bundle args = new Bundle();
        args.putInt(DummySectionFragment.ARG_OBJECT, position);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getCount() {
        // Show 2 total pages.
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
        case 0:
            return getString(R.string.title_section1).toUpperCase(l);
        case 1:
            return getString(R.string.title_section2).toUpperCase(l);
        }
        return null;
    }
}

/**
 * A dummy fragment representing a section of the app, but that simply
 * displays dummy text.
 */
public static class DummySectionFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";
    public static final String ARG_OBJECT = "object";
    public DummySectionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        Bundle args = getArguments();
        int position = args.getInt(ARG_OBJECT);

        int tabLayout = 0;
        switch (position) {
        case 0:
        tabLayout = R.layout.tab_new;
        break;
        case 1:
        tabLayout = R.layout.tab_summary;
        break;

        }

        View rootView = inflater.inflate(tabLayout, container, false);

        return rootView;
    }
}
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

    case R.id.action_settings:
        Intent i = new Intent(this, SettingsActivity.class);
        startActivity(i);
    }

    return true;
}

}

为此,我只是创建了 2 个 xml 文件并在选项卡正确定位时对其进行了初始化;但是,现在我想要 2 个选项卡,但第二个必须是列表。我不知道该怎么做。我尝试扩展 listFragment,但它因找不到资源而崩溃。卡了好久了,求大神帮忙?我搜索了很多并查看了所有示例,但没有一个告诉我如何在选项卡中创建列表以及如何调用它。

这是我现在拥有的 MainActivity

public class MainActivity extends FragmentActivity {
SimpleListFragment x = new SimpleListFragment();
/**
 * The {@link android.support.v4.view.PagerAdapter} that will provide
 * fragments for each of the sections. We use a
 * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
 * will keep every loaded fragment in memory. If this becomes too memory
 * intensive, it may be best to switch to a
 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
 */
SectionsPagerAdapter mSectionsPagerAdapter;

/**
 * The {@link ViewPager} that will host the section contents.
 */
ViewPager mViewPager;

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

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(
            getSupportFragmentManager());

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

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a DummySectionFragment (defined as a static inner class
        // below) with the page number as its lone argument.
        Fragment fragment = new DummySectionFragment();
        Bundle args = new Bundle();
        args.putInt(DummySectionFragment.ARG_OBJECT, position + 1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
        case 0:
            return getString(R.string.title_section1).toUpperCase(l);
        case 1:
            return getString(R.string.title_section2).toUpperCase(l);
        }
        return null;
    }
}

/**
 * A dummy fragment representing a section of the app, but that simply
 * displays dummy text.
 */
public static class DummySectionFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";
    public static final String ARG_OBJECT = "object";
    public DummySectionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        Bundle args = getArguments();
        int position = args.getInt(ARG_OBJECT);

        int tabLayout = 0;
        switch (position) {
        case 0:
        tabLayout = R.layout.tab_home;
        break;
        case 1:
            SimpleListFragment simpleListFragment = new SimpleListFragment();

        break;

        }

        View rootView = inflater.inflate(tabLayout, container, false);

        return rootView;
    }
}

}

4

2 回答 2

5

ATM 您正在 DummySectionFragment 中创建 ListFragment。那是不对的。

这是一种方法。不是最好的,但很好理解。

在主活动中创建片段并将它们传递给适配器。DummyListFragment 是从 ListFragment 派生的新类。这里重要的是,它使用的 XML 布局包含一个 ID 为 @android:id/list 的 ListView。(因此片段能够理解它必须/可以使用的列表视图。

public class MainActivity extends FragmentActivity {

SectionsPagerAdapter mSectionsPagerAdapter;

ViewPager mViewPager;

List<Fragment> mFragments;


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

 mFragments = new ArrayList<Fragment>();
 mFragments.add(new DummySectionFragment();
 mFragments.add(new DummyListFragment();

  mSectionsPagerAdapter = new SectionsPagerAdapter(
        getSupportFragmentManager(), mFragments);

...

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    List<Fragment> fragmentList;

    public SectionsPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
        super(fm);
        fragmentList = fragments;
    }

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

    @Override
    public Fragment getItem(int position) {
        Fragment fragment = fragmentList.get(position);        
        return fragment;
    }

. .

public static class DummyListFragment extends ListFragment {

public DummyListFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    return inflater.inflate(R.layout.my_list_fragment, container, false);
}
于 2013-07-28T06:18:39.623 回答
0

好的,让我们看看。

  1. 完全删除DummySectionFragment

  2. 创建两个单独的片段。一个给你的ListView和另一个。确保你让他们工作。Eclipse 生成的代码使用支持库中的 Fragment 类,因此您的 Fragment 必须从Fragment(或ListFragment)类扩展,android.support.v4.app不是android.app。确保导入正确的。

  3. 更改您的实现FragmentPagerAdapter

    public class MyPagerAdapter extends FragmentPagerAdapter
    {
        public MyPagerAdapter(FragmentManager fm)
        {
            super(fm);
        }
    
        @Override
        public Fragment getItem(int pos)
        {
            // return instances of your custom fragments
            // tab 0: list
            // tab 1: other
            // (for more than 2 tabs use a switch statement instead)
            return (position == 0) ? new MyListFragment() : new MyOtherFragment();
        }
    
        @Override
        public int getCount()
        {
            // Defines the number of tabs shown and positions that getItem() should handel
            return 2;
        }
    
        @Override
        public CharSequence getPageTitle(int position)
        {
            // define the titles for your tabs in the strings.xml resource
            switch (position)
            {
                case 0: return getString(R.string.tab_fragment_title);
                case 1: return getString(R.string.other_fragment_title);
            }
    
            return null;
        }
    }
    
  4. 使用新适配器:

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        // Create your adapter and pass the support fragment manager
        MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager());
    
        // Set up the ViewPager with your pager adapter
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(adapter);
    }
    
于 2013-07-29T03:00:01.003 回答