我必须遗漏一些内容,但我无法从ViewPager
中删除片段。我将页面标题存储在ArrayList
:
ViewPagerActivity.java
的一部分(主要活动是FragmentActivity
):
public static ArrayList<String> mEntries = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.main );
context=this;
mEntries.add(" 111");
mEntries.add(" 222");
mEntries.add(" 333");
mEntries.add(" 444");
mEntries.add(" 555");
mEntries.add(" 666");
List<String> fragments = new Vector<String>();
fragments.add(111.class.getName());
fragments.add(222.class.getName());
fragments.add(333.class.getName());
fragments.add(444.class.getName());
fragments.add(555.class.getName());
fragments.add(666.class.getName());
this.mPagerAdapter = new PagerAdapter1(super.getSupportFragmentManager(), fragments);
pager = (ViewPager)super.findViewById(R.id.viewpager);
pager.setAdapter(this.mPagerAdapter);
// ...
我有一个片段(111
),我想通过点击555
中的按钮删除111
片段。 (因为它以0
开头,555
是第4个片段。
fragment111.java
的一部分(Fragment
}:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ViewPagerActivity.mEntries.remove(5);
ViewPagerActivity.mPagerAdapter.removeX(5);
ViewPagerActivity.mPagerAdapter.removeX2(5);
ViewPagerActivity.pager.setAdapter( ViewPagerActivity.mPagerAdapter);
ViewPagerActivity.pager.getAdapter().notifyDataSetChanged();
}
});
通过覆盖POSITION_NONE
方法,我有一些人错过了getItemPosition()
。
我的适配器类:
public class PagerAdapter1 extends FragmentStatePagerAdapter {
private List<String> fragments;
private static final String TAG = "FragmentPagerAdapter";
private static final boolean DEBUG = true;
private Fragment mCurrentPrimaryItem = null;
private final FragmentManager mFragmentManager;
private FragmentTransaction mCurTransaction = null;
/**
* @param fm
* @param fragments2
*/
public PagerAdapter1(FragmentManager fm, List<String> fragments2) {
super(fm);
mFragmentManager=fm;
this.fragments = fragments2;
}
/* (non-Javadoc)
* @see android.support.v4.app.FragmentPagerAdapter#getItem(int)
*/
@Override
public Fragment getItem(int position) {
//return this.fragments.get(position);
return Fragment.instantiate(ViewPagerActivity.context, fragments.get(position));
}
/* (non-Javadoc)
* @see android.support.v4.view.PagerAdapter#getCount()
*/
@Override
public int getCount() {
return this.fragments.size();
}
/**
* Return the Fragment associated with a specified position.
*/
@Override
public void startUpdate(ViewGroup container) {
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
if (mCurTransaction == null) {
mCurTransaction = mFragmentManager.beginTransaction();
}
final long itemId = getItemId(position);
// Do we already have this fragment?
String name = makeFragmentName(container.getId(), itemId);
Fragment fragment = mFragmentManager.findFragmentByTag(name);
if (fragment != null) {
if (DEBUG) Log.v(TAG, "Attaching item #" + itemId + ": f=" + fragment);
mCurTransaction.attach(fragment);
} else {
fragment = getItem(position);
if (DEBUG) Log.v(TAG, "Adding item #" + itemId + ": f=" + fragment);
mCurTransaction.add(container.getId(), fragment,
makeFragmentName(container.getId(), itemId));
}
if (fragment != mCurrentPrimaryItem) {
fragment.setMenuVisibility(false);
fragment.setUserVisibleHint(false);
}
return fragment;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
if (mCurTransaction == null) {
mCurTransaction = mFragmentManager.beginTransaction();
}
if (DEBUG) Log.v(TAG, "Detaching item #" + getItemId(position) + ": f=" + object
+ " v=" + ((Fragment)object).getView());
mCurTransaction.detach((Fragment)object);
}
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
Fragment fragment = (Fragment)object;
if (fragment != mCurrentPrimaryItem) {
if (mCurrentPrimaryItem != null) {
mCurrentPrimaryItem.setMenuVisibility(false);
mCurrentPrimaryItem.setUserVisibleHint(false);
}
if (fragment != null) {
fragment.setMenuVisibility(true);
fragment.setUserVisibleHint(true);
}
mCurrentPrimaryItem = fragment;
}
}
@Override
public void finishUpdate(ViewGroup container) {
if (mCurTransaction != null) {
mCurTransaction.commitAllowingStateLoss();
mCurTransaction = null;
mFragmentManager.executePendingTransactions();
}
}
@Override
public boolean isViewFromObject(View view, Object object) {
return ((Fragment)object).getView() == view;
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
}
/**
* Return a unique identifier for the item at the given position.
*
* <p>The default implementation returns the given position.
* Subclasses should override this method if the positions of items can change.</p>
*
* @param position Position within this adapter
* @return Unique identifier for the item at position
*/
public long getItemId(int position) {
return position;
}
private static String makeFragmentName(int viewId, long id) {
return "android:switcher:" + viewId + ":" + id;
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
public void removeX(int position){
fragments.remove(5);
}
public void removeX2(int position){
Fragment ff = getItem(position);
mFragmentManager.beginTransaction().remove(ff).commit();
notifyDataSetChanged();
}
}
我正在使用FragmentStatePagerAdapter
而notifyDataSetChanged()
效果很好(我更新CheckBox
的状态,我看到它有效)。如何从ViewPager
?
提前致谢!
答案 0 :(得分:1)
我有同样的问题,我找到的唯一解决方案是在删除这样的片段后再次设置适配器:
@Override
public void onClick(View v) {
ViewPagerActivity.mEntries.remove(4);
pager.setAdapter(mPagerAdapter);
}
更新:您不需要再次设置适配器。这里是我的 PagerAdapter 的一些方法,您可能需要做一些修改才能将其用作FragmentPagerAdapter
删除观看次数:
public static void deleteChildren(int pStart) {
List<ViewInfo> viewToRemove = new ArrayList<ViewInfo>();
for (int iView = pStart+1; iView < _views.size(); iView++) {
viewToRemove.add(_views.get(iView));
}
if (viewToRemove.size() != 0) {
_views.removeAll(viewToRemove);
}
}
@Override
public void destroyItem(View pView, int pIndex, Object pObject) {
((ViewPager) pView).removeView((View)pObject);
}
覆盖getItemPosition(Object pObject):
@Override
public int getItemPosition(Object pObject) {
int pos = 0;
for (ViewInfo iView : _views) {
if (iView.mView == (View)pObject) {
break;
}
pos++;
}
return pos;
}
刷新所有视图:
public static void refreshAdapterViewPager() {
_pagerAdapter.notifyDataSetChanged();
}
答案 1 :(得分:1)
问题是 的 FragmentStatePagerAdapter 下, 它回收了从视图中消失的碎片。 即使你创建了一个新的。 尝试使用 的 FragmentPagerAdapter 强> 相反,它开始实例化它。