用户在片段上滑动时删除片段

时间:2014-02-03 15:49:35

标签: android android-fragments

我正在尝试在我的片段适配器上实现FINISH()。我只是想知道我应该使用哪些可能的代码作为用户滑动片段,SWIPED片段被移除....

我的适配器代码:

public class FragmentAdapter extends FragmentPagerAdapter implements IconPagerAdapter
{    
    public FragmentAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }

    @Override
    public int getIconResId(int index) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public Fragment getItem(int position) {
        // TODO Auto-generated method stub
        Fragment fragment = new Question1();
        switch(position){
        case 0:
            fragment = new Question1();
            break;
        case 1:
            fragment = new Question2();
            break;
        case 2:
            fragment = new Question3();
            break;
        case 3:
            fragment = new Question4();
            break;
        case 4:
            fragment = new Question5();
            break;
        case 5:
            fragment = new FinalPage();
            break;
        }
        return fragment;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 6;
    }

    @Override
    public CharSequence getPageTitle(int position){
        String title = "";
        switch(position){
        case 0:
            title = "Question 1";
            break;
        case 1:
            title = "Question 2";
            break;
        case 2:
            title = "Question 3";
            break;
        case 3:
            title = "Question 4";
            break;
        case 4:
            title = "Question 5";
            break;
        case 5:
            title = "Submit Quiz";
            break;
        }

            return title;
    }
}

我的活动代码:

public class QuestionActivity extends FragmentActivity
{   
    FragmentAdapter mAdapter;
    ViewPager mPager;
    PageIndicator mPageIndicator;
    String share_name = "Q1_Answer";

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

        mAdapter = new FragmentAdapter(getSupportFragmentManager());

        mPager = (ViewPager)findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);

        mPageIndicator = (PageIndicator)findViewById(R.id.indicator);
        mPageIndicator.setViewPager(mPager);    
    }
}

1 个答案:

答案 0 :(得分:0)

在ViewPager中处理片段时,无论何时想要操作片段,都应该操作传递给PagerAdapter的List对象,然后在PagerAdapter对象上调用notifyDataSetChanged。

例如:

List<Fragment> mFragments = new List<Fragment>();
mFragments.Add(frag1);
mFragments.Add(frag2);

FragmentStatePagerAdapter pagerAdapter;
pagerAdapter = new FragmentStatePagerAdapter(FragmentManager, mFragments, this);

ViewPager viewPager = FindViewById<ViewPager>(Resource.Id.view_pager);
viewPager.Adapter = pagerAdapter;

...

mFragments.Remove(0);

pagerAdapter.notifyDataSetChanged();
pagerAdapter.setCurrentItem(0, false);