问题描述
- 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(idtitlenull1""); 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
解决方案
我也遇到这个问题了,楼主解决了吗
解决方案二:
http://www.desteps.com/mobile/android/0101093.html
时间: 2024-10-22 02:33:11