2 jonahzheng jonahzheng 于 2013.04.23 17:00 提问

viewpager如何动态删除fragment
        private ViewPager viewPager;
    /** 页面list **/
    ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();
    @Override
    public void onCreate(Bundle savedInstanceState) {


        viewPager = (ViewPager)findViewById(R.id.viewPager);

        TextView titleTextView;
        titleTextView = (TextView) this.findViewById(R.id.title_text);
        Intent intent = getIntent();
        String id = intent.getStringExtra("id");
        String title = intent.getStringExtra("title");
        titleTextView.setText(title);
        addFragmentToViewPager(id,title,null,"1");
        viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager(), fragmentList));
        viewPager.setOnPageChangeListener(new MyOnPageChangeListener());
        viewPager.setOnTouchListener(new MyOnTouchListener());
        Button titleleftButton=(Button)findViewById(R.id.title_bt_left);
        titleleftButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub              
//处理删除
                int w=fragmentList.size();
                if(w==1){
                    finish();   
                }else{
                    fragmentList.remove(w-1).onDestroy();
                    viewPager.setCurrentItem(w-2);
                    Log.i("bctt",fragmentList.size()+"");
                }

            }
        });
//添加一个fragment到viewpager
    private void addFragmentToViewPager(String id, String title, String sql, String FragmentName) {
            Fragment CardListFragment;
            CardListFragment = new CardListFragment();
            CardListFragment.setArguments(args);
            fragmentList.add(CardListFragment);
            viewPager.setCurrentItem(fragmentList.indexOf(CardListFragment));

想动态添加删除fragment到viewpager,不知道如何实现,很急,请知道的同学帮忙看一下
还有一个问题,如何去除viewpager左右滑动,这样我可以通过一个按钮点击,一级一级的删除fragment

3个回答

iloveyoueveryday
iloveyoueveryday   2014.07.14 16:54

我也遇到这个问题了,楼主解决了吗

xunfan
xunfan   2014.12.25 18:16
xuan2418421171
xuan2418421171   2016.12.07 15:44

怎么可以去指定删除一个Fragment

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!