如何在FragmentPagerAdapter上以编程方式更改片段?

时间:2014-06-21 17:46:30

标签: android android-fragments android-listfragment android-tabs fragmentpageradapter

我使用Eclipse默认的ViewPager应用程序参数创建了应用程序。主要活动包含2个选项卡(tabA和tabB)。每个标签都指其片段(fragmentA和fragmentB)。

public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int index) {

        switch (index) {
        case 0:
            // Top Rated fragment activity
            return new Cars();
        case 1:
            // Games fragment activity
            return new News();
        }
        return null;
    }

    @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;
    }
}

在第二个标签(tabB)上,有一个列表视图。我正在尝试在选择一个列表项时将片段(不打开新活动)更改为第二个选项卡(tabB)上的新片段(fragmentC)。

public class News extends ListFragment {
...
            ListAdapter adapter = new LazyAdapter(getActivity(), carsList);
            setListAdapter(adapter);

            getListView().setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View view,
                        int position, long arg3) {
                    // TODO Auto-generated method stub

                    TextView tvId = (TextView) view.findViewById(R.id.tvId);


                    Toast.makeText(getActivity(), "Item was clicked. id:"+tvId.getText(),
                            Toast.LENGTH_SHORT).show();

                    //WHAT SHOULD I WRITE HERE?
                }
            });

如何在FragmentPagerAdapter内部更改片段,同时启用&#34; BACK&#34;按钮(如果在fragmentC上按下后退按钮,它应该返回到fragmentB)?

0 个答案:

没有答案