如何从FragmentPagerAdapter中删除片段?

我知道这里有一些关于这方面的话题,但我找不到解决scheme,我可以为我的案件工作。

我有一个工作滑animation廊使用自定义FragmentActivityFragmentPagerAdapter ,其中包含碎片列表。

FragmentActivity是一个ImageView “删除”。 如果点击,函数deleteMedia()被调用,然后应该删除当前的Fragment并且应该显示下面的Fragment 。 在我的例子中,我将如何做到这一点?

FragmentActivity:

 public class GalleryPagerActivity extends FragmentActivity implements OnClickListener { private Intent intent; private SharedPreferences settings; private PagerAdapter mPagerAdapter; private ViewPager mPager; private List<Fragment> fragments; private List<WhiteboardMedia> wiList; private int selectedPosition; private LinearLayout llTop; private TextView tvTop; private ImageView delete; private ImageView share; private TextView tvCounter; private TextView tvFilename; private TextView tvFilesize; private TextView tvDate; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); try { super.setContentView(R.layout.gallery_pager); intent = getIntent(); Type collectionType = new TypeToken<List<WhiteboardMedia>>(){}.getType(); wiList = gson.fromJson(intent.getStringExtra("wiList"), collectionType); selectedPosition = intent.getIntExtra("position", 1); llTop = (LinearLayout) findViewById(R.id.llTop); llTop.setOnClickListener(this); tvTop = (TextView) findViewById(R.id.tvTop); tvTop.setOnClickListener(this); delete = (ImageView) findViewById(R.id.imgDelete); delete.setOnClickListener(this); share = (ImageView) findViewById(R.id.imgShare); share.setOnClickListener(this); tvCounter = (TextView) findViewById(R.id.tvCounter); tvFilename = (TextView) findViewById(R.id.tvFilename); tvFilesize = (TextView) findViewById(R.id.tvFilesize); tvDate = (TextView) findViewById(R.id.tvDate); createContextMenu(); initDropbox(); } catch (Exception e) { Log.e("GalleryPagerActivity", e.getLocalizedMessage()); } } /** * Initialise the pager */ private void initialisePager() { mPager = (ViewPager) super.findViewById(R.id.viewpager); mPager.setAdapter(this.mPagerAdapter); mPager.setOnPageChangeListener(new GalleryPageListener(tvCounter, tvFilename, tvFilesize, tvDate, wiList)); mPager.setCurrentItem(selectedPosition, true); updatePage(selectedPosition); } public void updatePage(int position) { int focusedPage = position + 1; Log.i("onPageSelected", "page selected " + position); WhiteboardMedia wiImage = wiList.get(position); String imageDate = "N/A"; try { Date dateTaken= new Date(); //wiImage.getDate(); SimpleDateFormat sdf = new SimpleDateFormat("yy/MM/dd"); imageDate = sdf.format(dateTaken); } catch (Exception e) { } try { tvCounter.setText(focusedPage + "/" + wiList.size()); tvFilename.setText(wiImage.getFilename()); tvFilesize.setText(wiImage.getSize() + "a"); tvDate.setText(imageDate); } catch (Exception e) { } } @Override protected void onResume() { super.onResume(); } @Override public void onDestroy() { super.onDestroy(); } private WhiteboardMedia getActiveWhiteboardImage() { return wiList.get(mPager.getCurrentItem()); } private final int DELETE = 1; @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(1, DELETE, 2, R.string.delete).setIcon(R.drawable.menu_btn_trash); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case DELETE: deleteMedia(); return true; } return super.onContextItemSelected(item); } @Override public void onClick(View v) { if (v == delete) { deleteMedia(); } } private void deleteMedia() { // TODO delete the active Fragment and display the next Fragment in the list } /****************************************************************************** * Context Menu *****************************************************************************/ private void createContextMenu() { // context menu stuff } @Override protected Dialog onCreateDialog(int id) { // stuff } } 

FragmentPagerAdapter:

 public class GalleryPagerAdapter extends FragmentPagerAdapter { private final List<Fragment> fragments; public GalleryPagerAdapter(FragmentManager fm, List<Fragment> fragments) { super(fm); this.fragments = fragments; } @Override public Fragment getItem(int position) { return this.fragments.get(position); } @Override public int getCount() { return this.fragments.size(); } } 

感谢帮助!

Solutions Collecting From Web of "如何从FragmentPagerAdapter中删除片段?"

首先,我build议你考虑改变你的FragmentPagerAdapter ,看起来更像样本 。 您通常不会持有片段列表,除了ArrayAdapter通常包含行列表Views列表。 通常情况下,您可以按需创build片段,而其他人则可以保存列表。

然后,要删除某些内容,请将其从模型数据中删除( FragmentPagerAdapter通常包装的内容)。 确保getCount()将返回正确数量的项目。 然后,调用FragmentPagerAdapter上的notifyDataSetChanged() ,这将触发ViewPager的重绘。

这是我使用的解决scheme:

mViewPager:是你用来设置片段的视图

 mViewPager = (YourViewPager) findViewById(R.id.myPager); 

TABLE:只是我所有碎片位置的整数列表

 public void destroyAllItem() { int mPosition = mViewPager.getCurrentItem(); int mPositionMax = mViewPager.getCurrentItem()+1; if (TABLE.size() > 0 && mPosition < TABLE.size()) { if (mPosition > 0) { mPosition--; } for (int i = mPosition; i < mPositionMax; i++) { try { Object objectobject = this.instantiateItem(mViewPager, TABLE.get(i).intValue()); if (objectobject != null) destroyItem(mViewPager, TABLE.get(i).intValue(), objectobject); } catch (Exception e) { Log.i(TAG, "no more Fragment in FragmentPagerAdapter"); } } } } @Override public void destroyItem(ViewGroup container, int position, Object object) { super.destroyItem(container, position, object); if (position <= getCount()) { FragmentManager manager = ((Fragment) object).getFragmentManager(); FragmentTransaction trans = manager.beginTransaction(); trans.remove((Fragment) object); trans.commit(); } } 

我发现一个解决schemeovverriding的方法“onPostResume()”的活动和调用notifyDataSetChanged里面。

 @Override protected void onPostResume() { super.onPostResume(); if(this.mCustomPagerAdapter!=null){ this.mCustomPagerAdapter.notifyDataSetChanged(); } }