ViewPager删除页面后显示错误的内容

时间:2017-01-02 21:15:21

标签: android android-fragments android-viewpager fragmentstatepageradapter

我使用ViewPager创建了FragmentStatePagerAdapter实现,让我的应用用户浏览并编辑可能有大量的记录。父活动的菜单包括删除按钮,允许用户删除当前显示的片段。片段包括准静态(例如TextView)以及直接可编辑数据(例如EditText)。数据在编辑时保存(为简洁起见,此处未显示)。

当我删除页面时,现在发生的是显示下一条记录,这是预期的。 TextView正确显示,但当前页面的EditText显示已删除页面的相应值,并且下一页的值也来自错误记录。

如何让ViewPager正确显示EditText次观看次数?

例如,在删除包含内容3的第3页后,我得到以下内容:

enter image description here

以下是ActivityFragmentStatePagerAdapterFragment的代码。

活动:

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;
    }
}

1 个答案:

答案 0 :(得分:1)

尝试在setText onResume方法中调用PagerFragment方法。在这里找到答案:https://stackoverflow.com/a/12987279/2121682。不知道为什么或如何,但是当我在你的代码上尝试它时它就起作用了。

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);
    }
}