我有一个Book的活动,其中包含一个类型为“page”的arraylist,并处理我决定使用FragmentStatePagerAdapter的所有页面,其中我的一个片段包含一个页面.
我为寻呼机中的每个片段和父活动之间的通信创建了一个接口.我在这个接口中需要的方法是更新页面中显示的页面,所以我为父活动创建了工具,用于从片段接收数据的接口并将它们存储在页面中以保存在arraylist中.
每个片段都有一个EditText,用户可以在其中编写,我设置addTextChangedListener来捕获用户停止写入的时刻.
当用户停止在EditText中写入时,在onTextChanged()中,我调用activity father中实现的函数.
这是我的活动代码
public class BookEditorActivity implements BookEditorFragment.EditorFrToEditorActInterface { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.book_editor_activity); Bundle extras = getIntent().getExtras(); b = (Book) extras.get("book"); setBookViewPager(b); } private void setBookViewPager(Book b) { mBookViewPager = (ViewPager) findViewById(R.id.book_editor_pager); mBookViewPagerAdapter = new BookViewPagerAdapter(getSupportFragmentManager(),b); mBookViewPager.setAdapter(mBookViewPagerAdapter); } @Override public void saveBookPageTextContent(String textContent) { int current = mBookViewPager.getCurrentItem(); if (b.getPages().get(current) instanceof BookPageText) { ((BookPageText) b.getPages().get(current)).setContentPageText(textContent); } } @Override public void newBookPageText() { int current = mBookViewPager.getCurrentItem(); BookPageText bookPageText = new BookPageText(); bookPageText.setContentPageText(""); b.getPages().add(b.getPages().size() - 1,bookPageText); setIndicator(current + 1,b.getPages().size()); mBookViewPagerAdapter.notifyDataSetChanged(); } }
这是片段代码
public class BookEditorFragment extends Fragment { private EditorFrToEditorActInterface mCallback; public interface EditorFrToEditorActInterface { void newBookPageText(); void saveBookPageTextContent(String s); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Bundle pageContent = getArguments(); if (pageContent != null) { page = pageContent.getParcelable("page"); } } @Nullable @Override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) { if (page instanceof BookPageText) { BookPageText bookPage = (BookPageText) page; mView = inflater.inflate(R.layout.book_page_text_fragment,container,false); contentPage = (EditText) mView.findViewById(R.id.content_text); String contentPageText = bookPage.getContentPageText(); contentPage.setText(contentPageText.isEmpty() ? "" : Html.fromHtml(contentPageText)); contentPage.addTextChangedListener(new TextWatcher() { public void onTextChanged(CharSequence c,int start,int before,int count) { mCallback.saveBookPageTextContent(c.toString()); } public void beforeTextChanged(CharSequence c,int count,int after) { } public void afterTextChanged(Editable c) { } }); } return mView; } @Override public void onAttach(Context context) { super.onAttach(context); try { mCallback = (EditorFrToEditorActInterface) context; } catch (ClassCastException e) { throw new ClassCastException(context.toString() + " must implement NewPageInterface"); } } @Override public void onDetach() { mCallback = null; super.onDetach(); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.editor_new_text: mCallback.newBookPageText(); break; } } }
这是FragmentStatePagerAdapter代码
public class BookViewPagerAdapter extends FragmentStatePagerAdapter { private ArrayList<Object> bookPages = new ArrayList<>(); private final SparseArray<WeakReference<BookEditorFragment>> instantiatedFragments = new SparseArray<>(); public BookViewPagerAdapter(FragmentManager fm,Book b) { super(fm); this.bookPages = b.getPages(); } @Override public Fragment getItem(int position) { Object page = bookPages.get(position); Bundle pageContent = new Bundle(); if (page instanceof BookPageText) { BookPageText bookPageText = (BookPageText) page; pageContent.putParcelable("page",bookPageText); } BookEditorFragment fragment = new BookEditorFragment(); fragment.setArguments(pageContent); return fragment; } @Override public int getCount() { return bookPages.size(); } @Override public Object instantiateItem(final ViewGroup container,final int position) { final BookEditorFragment fragment = (BookEditorFragment) super.instantiateItem(container,position); instantiatedFragments.put(position,new WeakReference<>(fragment)); return fragment; } @Override public void destroyItem(final ViewGroup container,final int position,final Object object) { instantiatedFragments.remove(position); super.destroyItem(container,position,object); } @Nullable public Fragment getFragment(final int position) { final WeakReference<BookEditorFragment> wr = instantiatedFragments.get(position); if (wr != null) { return wr.get(); } else { return null; } } @Override public int getItemPosition(Object object) { int position = bookPages.indexOf(object); return position == -1 ? POSITION_NONE : position; } }
由于代码长度,我删除了一些部分,如onClickListener,onPageSelectionListener和我正在创建的其他类型的页面.
问题
这个逻辑似乎工作正常,但每次我修改片段中EditText的内容时,左边的close片段也会更新,我不知道为什么.
正如你在图像中看到的那样,如果我在EditText2中写一些东西,EditText1中会出现相同的内容,这没有任何意义.
另一个问题是,当我在书中添加新页面时,页面是正确创建的,但是使用上一页的内容,这没有任何意义. (再次!)
我的尝试
1)我试图捕获onFocusChange而不是使用addTextChangedListener,但没有改变.
2)我尝试使用onChangePageListener()实现不同的逻辑,但在这种情况下,我松开了mBookViewPager.getCurrentItem(),因此更新出错了.
3)我尝试了所有这些帖子:one,two,three和four.
我怎样才能纠正这种奇怪的行为?
是否有可能问题出现在我引用本书ArrayList中的页面的方式中?
谢谢
解决方法
>管理当前页面构成工具
mViewPager.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int p) { current = p; Log.e("Postion","" + p); } @Override public void onPageScrolled(int arg0,float arg1,int arg2) {} @Override public void onPageScrollStateChanged(int arg0) {} });
> viewpager自动加载私有和下一个视图
通过调用mathod来设置寻呼机OffscreenPageLimit
mViewPager.setOffscreenPageLimit();
>总是确保如果你在适配器中使用if语句,你应该通过else语句将它的计数器部分放在任何默认值中.
if (page instanceof BookPageText) { BookPageText bookPageText = (BookPageText) page; pageContent.putParcelable("page",bookPageText); }