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个回答

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

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐