我正在尝试从 ViewPager 中动态添加和删除片段,添加作品没有任何问题,但删除无法按预期工作.
I'm trying to dynamically add and remove Fragments from a ViewPager, adding works without any problems, but removing doesn't work as expected.
每次我想删除当前项时,最后一项都会被删除.
Everytime I want to remove the current item, the last one gets removed.
我还尝试使用 FragmentStatePagerAdapter 或在适配器的 getItemPosition 方法中返回 POSITION_NONE.
I also tried to use an FragmentStatePagerAdapter or return POSITION_NONE in the adapter's getItemPosition method.
我做错了什么?
这是一个基本的例子:
MainActivity.java
public class MainActivity extends FragmentActivity implements TextProvider {
private Button mAdd;
private Button mRemove;
private ViewPager mPager;
private MyPagerAdapter mAdapter;
private ArrayList<String> mEntries = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mEntries.add("pos 1");
mEntries.add("pos 2");
mEntries.add("pos 3");
mEntries.add("pos 4");
mEntries.add("pos 5");
mAdd = (Button) findViewById(R.id.add);
mRemove = (Button) findViewById(R.id.remove);
mPager = (ViewPager) findViewById(R.id.pager);
mAdd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
addNewItem();
}
});
mRemove.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
removeCurrentItem();
}
});
mAdapter = new MyPagerAdapter(this.getSupportFragmentManager(), this);
mPager.setAdapter(mAdapter);
}
private void addNewItem() {
mEntries.add("new item");
mAdapter.notifyDataSetChanged();
}
private void removeCurrentItem() {
int position = mPager.getCurrentItem();
mEntries.remove(position);
mAdapter.notifyDataSetChanged();
}
@Override
public String getTextForPosition(int position) {
return mEntries.get(position);
}
@Override
public int getCount() {
return mEntries.size();
}
private class MyPagerAdapter extends FragmentPagerAdapter {
private TextProvider mProvider;
public MyPagerAdapter(FragmentManager fm, TextProvider provider) {
super(fm);
this.mProvider = provider;
}
@Override
public Fragment getItem(int position) {
return MyFragment.newInstance(mProvider.getTextForPosition(position));
}
@Override
public int getCount() {
return mProvider.getCount();
}
}
}
TextProvider.java
public interface TextProvider {
public String getTextForPosition(int position);
public int getCount();
}
MyFragment.java
public class MyFragment extends Fragment {
private String mText;
public static MyFragment newInstance(String text) {
MyFragment f = new MyFragment(text);
return f;
}
public MyFragment() {
}
public MyFragment(String text) {
this.mText = text;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment, container, false);
((TextView) root.findViewById(R.id.position)).setText(mText);
return root;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="add new item" />
<Button
android:id="@+id/remove"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="remove current item" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
</LinearLayout>
fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/position"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="35sp" />
</LinearLayout>
Louth 的解决方案不足以让我的工作正常,因为现有的片段没有被破坏.受 this answer 的启发,我发现解决方案是覆盖 getItemId(int position)
FragmentPagerAdapter
的方法,用于在 Fragment 的预期位置发生变化时提供新的唯一 ID.
The solution by Louth was not enough to get things working for me, as the existing fragments were not getting destroyed. Motivated by this answer, I found that the solution is to override the getItemId(int position)
method of FragmentPagerAdapter
to give a new unique ID whenever there has been a change in the expected position of a Fragment.
private class MyPagerAdapter extends FragmentPagerAdapter {
private TextProvider mProvider;
private long baseId = 0;
public MyPagerAdapter(FragmentManager fm, TextProvider provider) {
super(fm);
this.mProvider = provider;
}
@Override
public Fragment getItem(int position) {
return MyFragment.newInstance(mProvider.getTextForPosition(position));
}
@Override
public int getCount() {
return mProvider.getCount();
}
//this is called when notifyDataSetChanged() is called
@Override
public int getItemPosition(Object object) {
// refresh all fragments when data set changed
return PagerAdapter.POSITION_NONE;
}
@Override
public long getItemId(int position) {
// give an ID different from position when position has been changed
return baseId + position;
}
/**
* Notify that the position of a fragment has been changed.
* Create a new ID for each position to force recreation of the fragment
* @param n number of items which have been changed
*/
public void notifyChangeInPosition(int n) {
// shift the ID returned by getItemId outside the range of all previous fragments
baseId += getCount() + n;
}
}
现在,例如,如果您删除单个选项卡或对订单进行一些更改,您应该在调用 notifyDataSetChanged()
之前调用 notifyChangeInPosition(1)
,这将确保将重新创建所有片段.
Now, for example if you delete a single tab or make some change to the order, you should call notifyChangeInPosition(1)
before calling notifyDataSetChanged()
, which will ensure that all the Fragments will be recreated.
覆盖 getItemPosition():
当调用notifyDataSetChanged()
时,适配器调用它所附加的ViewPager
的notifyChanged()
方法.ViewPager
然后检查适配器的 getItemPosition()
为每个项目返回的值,删除那些返回 POSITION_NONE
的项目(参见 源代码) 然后重新填充.
When notifyDataSetChanged()
is called, the adapter calls the notifyChanged()
method of the ViewPager
which it is attached to. The ViewPager
then checks the value returned by the adapter's getItemPosition()
for each item, removing those items which return POSITION_NONE
(see the source code) and then repopulating.
覆盖 getItemId():
这是防止适配器在重新填充 ViewPager
时重新加载旧片段所必需的.您可以通过查看 FragmentPagerAdapter
中 instantiateItem() 的源代码.
This is necessary to prevent the adapter from reloading the old fragment when the ViewPager
is repopulating. You can easily understand why this works by looking at the source code for instantiateItem() in FragmentPagerAdapter
.
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));
}
如你所见,getItem()
方法只有在片段管理器没有找到具有相同 Id 的现有片段时才会被调用.对我来说,即使在调用 notifyDataSetChanged()
之后,旧片段仍然附加,这似乎是一个错误,但 ViewPager
的文档确实明确指出:
As you can see, the getItem()
method is only called if the fragment manager finds no existing fragments with the same Id. To me it seems like a bug that the old fragments are still attached even after notifyDataSetChanged()
is called, but the documentation for ViewPager
does clearly state that:
请注意,该课程目前处于早期设计和开发阶段.该 API 可能会在以后的兼容性库更新中发生变化,需要在针对较新版本编译应用程序时更改应用程序的源代码.
Note this class is currently under early design and development. The API will likely change in later updates of the compatibility library, requiring changes to the source code of apps when they are compiled against the newer version.
因此,希望此处给出的解决方法在未来版本的支持库中不再是必需的.
So hopefully the workaround given here will not be necessary in a future version of the support library.
这篇关于从 Android 中的 ViewPager 中删除片段页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!