删除页面后 ViewPager 显示错误内容
ViewPager Shows Wrong Content after Removing a Page
我使用 FragmentStatePagerAdapter
创建了一个 ViewPager
实现,让我的应用程序的用户浏览和编辑可能存在的大量记录。父 activity 的菜单包含一个删除按钮,允许用户删除当前显示的片段。片段包括准静态(例如 TextView
)以及直接可编辑的数据(例如 EditText
)。数据在编辑时保存(为简洁起见,此处未显示)。
现在当我删除一个页面时会显示下一条记录,这是有意的。 TextView
显示正确,但是当前页的EditText
显示的是删除页对应的值,而且下一页的值取自错误的记录。
如何让 ViewPager
也正确显示 EditText
视图?
例如,删除内容为 3 的第 3 页后,我得到以下内容:
下面是 Activity
、FragmentStatePagerAdapter
和 Fragment
的代码。
Activity:
public class ViewPagerActivity extends AppCompatActivity {
private ViewPagerAdapter mViewPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager);
mViewPager = (ViewPager) findViewById(R.id.viewPager);
mViewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mViewPagerAdapter);
mViewPager.setCurrentItem(0);
}
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_view_pager, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.action_delete:
deleteItem();
return true;
}
return super.onOptionsItemSelected(item);
}
private void deleteItem() {
mViewPagerAdapter.removeItem(this, mViewPager.getCurrentItem());
if (mViewPagerAdapter.getCount() == 0) {
finish();
}
}
}
适配器:
class ViewPagerAdapter extends FragmentStatePagerAdapter {
private List<PageContent> mPageContents;
ViewPagerAdapter(FragmentManager fm) {
super(fm);
mPageContents = new ArrayList<>();
mPageContents.add(new PageContent("Page 1", "Detail 1"));
mPageContents.add(new PageContent("Page 2", "Detail 2"));
mPageContents.add(new PageContent("Page 3", "Detail 3"));
mPageContents.add(new PageContent("Page 4", "Detail 4"));
mPageContents.add(new PageContent("Page 5", "Detail 5"));
mPageContents.add(new PageContent("Page 6", "Detail 6"));
}
@Override
public int getCount() {
return mPageContents.size();
}
@Override
public int getItemPosition(Object object) {
return PagerAdapter.POSITION_NONE;
}
@Override
public Fragment getItem(int position) {
PageContent pageContent = mPageContents.get(position);
return PagerFragment.newInstance(pageContent.getTitle(), pageContent.getDescription());
}
void removeItem(AppCompatActivity activity, int position) {
mPageContents.remove(position);
notifyDataSetChanged();
if (mPageContents.size() == 0) {
activity.finish();
}
}
}
class PageContent {
private String mTitle;
String getTitle() { return mTitle; }
private String mDescription;
String getDescription() { return mDescription; }
PageContent(String title, String content) {
mTitle = title;
mDescription = content;
}
}
片段:
public class PagerFragment extends Fragment {
private String mTitle;
private String mDetail;
private static final String ARG_TITLE = "title";
private static final String ARG_DETAIL = "detail";
public PagerFragment() { }
public static PagerFragment newInstance(String title, String detail) {
PagerFragment fragment = new PagerFragment();
Bundle args = new Bundle();
args.putString(ARG_TITLE, title);
args.putString(ARG_DETAIL, detail);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mTitle = getArguments().getString(ARG_TITLE);
mDetail = getArguments().getString(ARG_DETAIL);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_pager, container, false);
TextView titleView = (TextView) view.findViewById(R.id.title);
EditText detailView = (EditText) view.findViewById(R.id.detail);
titleView.setText(mTitle);
detailView.setText(mDetail);
return view;
}
}
尝试在 PagerFragment
的 onResume
方法中调用 setText
方法。在这里找到答案:。不知道为什么或如何,但当我在你的代码上尝试时它起作用了。
public class PagerFragment extends Fragment {
private String mTitle;
private String mDetail;
private static final String ARG_TITLE = "title";
private static final String ARG_DETAIL = "detail";
TextView titleView;
EditText detailView;
public PagerFragment() {
}
public static PagerFragment newInstance(String title, String detail) {
PagerFragment fragment = new PagerFragment();
Bundle args = new Bundle();
args.putString(ARG_TITLE, title);
args.putString(ARG_DETAIL, detail);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mTitle = getArguments().getString(ARG_TITLE);
mDetail = getArguments().getString(ARG_DETAIL);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.item_pager, container, false);
titleView = (TextView) view.findViewById(R.id.text);
detailView = (EditText) view.findViewById(R.id.edit);
return view;
}
@Override
public void onResume() {
super.onResume();
titleView.setText(mTitle);
detailView.setText(mDetail);
}
}
我使用 FragmentStatePagerAdapter
创建了一个 ViewPager
实现,让我的应用程序的用户浏览和编辑可能存在的大量记录。父 activity 的菜单包含一个删除按钮,允许用户删除当前显示的片段。片段包括准静态(例如 TextView
)以及直接可编辑的数据(例如 EditText
)。数据在编辑时保存(为简洁起见,此处未显示)。
现在当我删除一个页面时会显示下一条记录,这是有意的。 TextView
显示正确,但是当前页的EditText
显示的是删除页对应的值,而且下一页的值取自错误的记录。
如何让 ViewPager
也正确显示 EditText
视图?
例如,删除内容为 3 的第 3 页后,我得到以下内容:
下面是 Activity
、FragmentStatePagerAdapter
和 Fragment
的代码。
Activity:
public class ViewPagerActivity extends AppCompatActivity {
private ViewPagerAdapter mViewPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager);
mViewPager = (ViewPager) findViewById(R.id.viewPager);
mViewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mViewPagerAdapter);
mViewPager.setCurrentItem(0);
}
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_view_pager, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.action_delete:
deleteItem();
return true;
}
return super.onOptionsItemSelected(item);
}
private void deleteItem() {
mViewPagerAdapter.removeItem(this, mViewPager.getCurrentItem());
if (mViewPagerAdapter.getCount() == 0) {
finish();
}
}
}
适配器:
class ViewPagerAdapter extends FragmentStatePagerAdapter {
private List<PageContent> mPageContents;
ViewPagerAdapter(FragmentManager fm) {
super(fm);
mPageContents = new ArrayList<>();
mPageContents.add(new PageContent("Page 1", "Detail 1"));
mPageContents.add(new PageContent("Page 2", "Detail 2"));
mPageContents.add(new PageContent("Page 3", "Detail 3"));
mPageContents.add(new PageContent("Page 4", "Detail 4"));
mPageContents.add(new PageContent("Page 5", "Detail 5"));
mPageContents.add(new PageContent("Page 6", "Detail 6"));
}
@Override
public int getCount() {
return mPageContents.size();
}
@Override
public int getItemPosition(Object object) {
return PagerAdapter.POSITION_NONE;
}
@Override
public Fragment getItem(int position) {
PageContent pageContent = mPageContents.get(position);
return PagerFragment.newInstance(pageContent.getTitle(), pageContent.getDescription());
}
void removeItem(AppCompatActivity activity, int position) {
mPageContents.remove(position);
notifyDataSetChanged();
if (mPageContents.size() == 0) {
activity.finish();
}
}
}
class PageContent {
private String mTitle;
String getTitle() { return mTitle; }
private String mDescription;
String getDescription() { return mDescription; }
PageContent(String title, String content) {
mTitle = title;
mDescription = content;
}
}
片段:
public class PagerFragment extends Fragment {
private String mTitle;
private String mDetail;
private static final String ARG_TITLE = "title";
private static final String ARG_DETAIL = "detail";
public PagerFragment() { }
public static PagerFragment newInstance(String title, String detail) {
PagerFragment fragment = new PagerFragment();
Bundle args = new Bundle();
args.putString(ARG_TITLE, title);
args.putString(ARG_DETAIL, detail);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mTitle = getArguments().getString(ARG_TITLE);
mDetail = getArguments().getString(ARG_DETAIL);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_pager, container, false);
TextView titleView = (TextView) view.findViewById(R.id.title);
EditText detailView = (EditText) view.findViewById(R.id.detail);
titleView.setText(mTitle);
detailView.setText(mDetail);
return view;
}
}
尝试在 PagerFragment
的 onResume
方法中调用 setText
方法。在这里找到答案:。不知道为什么或如何,但当我在你的代码上尝试时它起作用了。
public class PagerFragment extends Fragment {
private String mTitle;
private String mDetail;
private static final String ARG_TITLE = "title";
private static final String ARG_DETAIL = "detail";
TextView titleView;
EditText detailView;
public PagerFragment() {
}
public static PagerFragment newInstance(String title, String detail) {
PagerFragment fragment = new PagerFragment();
Bundle args = new Bundle();
args.putString(ARG_TITLE, title);
args.putString(ARG_DETAIL, detail);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mTitle = getArguments().getString(ARG_TITLE);
mDetail = getArguments().getString(ARG_DETAIL);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.item_pager, container, false);
titleView = (TextView) view.findViewById(R.id.text);
detailView = (EditText) view.findViewById(R.id.edit);
return view;
}
@Override
public void onResume() {
super.onResume();
titleView.setText(mTitle);
detailView.setText(mDetail);
}
}