我正在尝试使用Otto来实现以下用例。
我有一个包含片段的Viewpager,每个片段包含RecyclerView
个。 recyclerviews显示用户发布到应用程序的帖子。每个片段都附有一个新的帖子按钮。单击帖子Button
后,用户即可进入新活动,并在此处发布内容。当他再次回到ViewPager
片段屏幕时,我希望片段屏幕刷新并显示更新的内容。我为此使用otto。
当只有一个片段存在时,我能够完成这项工作。
public class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
BusProvider.getInstance().register(this);
}
我在PlaceholderFrament的onCreate方法中调用了register方法。很明显,当多次加载多个片段时,我收到以下错误。我该如何处理?
Process: com.four.xxxx.xx, PID: 9934
java.lang.IllegalArgumentException: Object already registered.
at com.squareup.otto.Bus.register(Bus.java:222)
at com.four.xxxx.xx.PlaceholderFragment.onCreateView(PlaceholderFragment.java:94)
at android.app.Fragment.performCreateView(Fragment.java:2053)
根据要求编辑1:PagerAdapter类:
public class FourScreenActivity extends Activity {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
static final int POST_DEPT_REQUEST = 1;
static final int POST_COLLEGE_REQUEST = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_four_screen);
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void postInSection1(View view) {
Intent intent = new Intent(this, PostInSection1.class);
startActivity(intent);
}
public void postInSection2(View view) {
Intent intent = new Intent(this, PostInSection2.class);
startActivity(intent);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
}
答案 0 :(得分:0)
您的Viewpager似乎很好。将otto
注册商/注销商置于每个onResume
和onPause
,如下所示,而不是onCreateView
。
@Override
public void onResume() {
super.onResume();
BusProvider.getInstance().register(this);
}
@Override
public void onPause() {
super.onPause();
BusProvider.getInstance().unregister(this);
}
ViewPager
实施如下。
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
private SparseArrayCompat<SectionFragmentInterface> fragmentSparseArrayCompat;
public SectionsPagerAdapter (FragmentManager fm) {
super(fm);
fragmentSparseArrayCompat = new SparseArrayCompat<>();
}
@Override
public Fragment getItem(int position) {
SectionFragmentInterface fragment = fragmentSparseArrayCompat.get(position);
switch (position) {
case 0: {
if (fragment == null) {
fragment = SectionOneListFragment.newInstance(position);
}
}
break;
case 1: {
if (fragment == null) {
fragment = SectionTwoListFragment.newInstance(position);
}
}
break;
}
fragmentSparseArrayCompat.put(position, fragment);
return fragment;
}
@Override
public int getCount() {
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
return "";
}
}
答案 1 :(得分:0)
解决了!
我已经为所有三个片段传递了同一个类的实例,并在每个片段onCreate方法中注册到总线,因此尝试一次又一次地注册同一个类。
我现在创建了一个基类,并为片段创建了三个不同的子类,因此我可以在相应的类中注册和注销,而无需多次注册。
这是我在FragmentPagerAdapter类的getItem方法中从问题中给出的片段中更改的代码。
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new SectionOneFragment();
case 1:
return new SectionTwoFragment();
case 2:
return new SectionThreeFragment();
}
//return PlaceholderFragment.newInstance(position + 1);
return null;
}
现在我在这些新类的onCreate方法中调用bus.register(),订阅的方法也在各自的类中实现。