PagerSlidingTabStrip - 替换viewpager中的片段

时间:2015-02-18 15:53:50

标签: android android-fragments android-viewpager android-listfragment android-tabstrip

SCOPE

我正在使用[PagerSlidingTabStrip] [1]并且我在每个选项卡中有两个选项卡(A和B)和一个ListFragment,其中包含6个列表项(1-6),单击它们时将转换到另一个片段(C) )。

Sample of Scope Sample of Detail View

问题

在搜索StackOverflow / Google并尝试各种方法4天后,我始终以一两个结果(不包括各种错误)结束。

1。)在片段中使用TextView,TextView字符串将填充在ActionBar(测试)和标签A之间。

2。)我会点击一个列表项,它会用一个空白的白色片段替换ListFragment(比如C)。

来源

MainActivity

public class MainActivity extends ActionBarActivity {

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

    // Initialize the ViewPager and set an adapter
    ViewPager pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(new MainPagerAdapter(getSupportFragmentManager()));

    // Bind the tabs to the ViewPager
    PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
    tabs.setShouldExpand(true); // expand tabs to fill parent
    tabs.setViewPager(pager);

}

public class MainPagerAdapter extends FragmentPagerAdapter {

    private final String[] TITLES = {"A", "B"};

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

    @Override
    public CharSequence getPageTitle(int position) {
        return TITLES[position];
    }

    @Override
    public int getCount() {
        return TITLES.length;
    }

    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                return new A();
            case 1:
                return new B();
        }
        return null;
    }
}

A / B类

public class A extends ListFragment {

    ListView list;

    final String[] items = {"1", "2", "3", "4", "5", "6"};

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.layout_a, container, false);
        list = (ListView) rootView.findViewById(android.R.id.list);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, items);
        list.setAdapter(adapter);

        return rootView;
    }

    @Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    // this is where I get stuck...using switch/case has resulted in the 2 results above
    }
}

任何帮助都会非常感激,因为我已经坚持了将近一个星期而且这令人发狂!

0 个答案:

没有答案