我正在使用ViewPager和FragmentStatePagerAdapter。 我的应用程序中有以下结构:
在索引1到7片段B:
每个片段B有4个按钮:
ViewPager设置时默认加载Fragment C. 我想要实现的目标如下: - 单击按钮D应使用与按钮关联的片段替换当前片段。这个完成了 问题是当按下按钮时,当前片段被替换,前一个和下一个片段也被替换。我怎样才能避免这种情况发生?
这是我的FragmentStatePageAdapter
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
private final FragmentManager mFragmentManager;
private Fragment mFragmentAtPos0;
public FirstPageListener listener = new FirstPageListener();
private final class FirstPageListener implements
ChannelPageFragmentListener {
public void onSwitchToNextFragment() {
mFragmentManager.beginTransaction().remove(mFragmentAtPos0)
.commit();
if (mFragmentAtPos0 instanceof LatestPostFragment) {
mFragmentAtPos0 = new LiveLoungeFragment(listener);
} else { // Instance of NextFragment
mFragmentAtPos0 = new LatestPostFragment(listener);
}
notifyDataSetChanged();
}
public void searchFields() {
if (mFragmentAtPos0 instanceof LatestPostFragment) {
((LatestPostFragment) mFragmentAtPos0).reloadData("", false);
}
notifyDataSetChanged();
}
}
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
mFragmentManager = fm;
}
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
String tag = null;
if(position == 0)
{
Log.i("Latest Posts","getItem pos : 0");
tag = DASHBOARD_FRAGMENT;
fragment = new DashboardFragment();
}
else
{
Log.i("Latest Posts","getItem pos : " + position);
if(label.equalsIgnoreCase("latest"))
{
fragment = new LatestPostFragment(listener);
mFragmentAtPos0 = fragment;
Log.i("Latest Posts","getItem latest");
}
else if(label.equalsIgnoreCase("events"))
{
fragment = new LiveLoungeFragment(listener);
mFragmentAtPos0 = fragment;
Log.i("Latest Posts","getItem events");
label = "latest";
}
fragment = mFragmentAtPos0;
showDrawerIcon();
}
return fragment;
}
@Override
public int getItemPosition(Object object)
{
Log.i("Latest Posts","getItemPosition : " + object.toString());
if (object instanceof LatestPostFragment) {
Log.i("Latest Posts","getItemPosition latestpost");
return POSITION_NONE;
}
if (object instanceof LiveLoungeFragment) {
Log.i("Latest Posts","getItemPosition livelounge");
return POSITION_NONE;
}
return POSITION_UNCHANGED;
}
@Override
public int getCount() {
return channels.size();
}
@Override
public CharSequence getPageTitle(int position) {
Log.i(TAG,"title : " + position);
return channels.get(position).getSport().name();
}
}`
这是按钮的触发器:
((Button) findViewById(R.id.latest)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
label = "latest";
sub_pressed = true;
sub_position = "current";
currentPosition = mViewPager.getCurrentItem();
getCategoryFromName((String) mSectionsPagerAdapter.getPageTitle(currentPosition).toString());
LatestPostFragment.firstPageListener.onSwitchToNextFragment();
}
});((Button) findViewById(R.id.events)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
label = "events";
sub_pressed = true;
sub_position = "current";
currentPosition = mViewPager.getCurrentItem();
getCategoryFromName((String) mSectionsPagerAdapter.getPageTitle(currentPosition).toString());
LatestPostFragment.firstPageListener.onSwitchToNextFragment();
}
});
基本上按下按钮时,会调用onSwitchToNextFragment的监听器并调用notifyDataSetChanged。
这是来自getItemPosition的日志
04-28 10:24:46.940 5127-5127 / com.breathesport.breathesportapp I /最新帖子:getItemPosition:LatestPostFragment {395c1afe#0 id = 0x7f0a008a} 04-28 10:24:46.940 5127-5127 / com.breathesport.breathesportapp I /最新帖子:getItemPosition latestpost 04-28 10:24:46.940 5127-5127 / com.breathesport.breathesportapp I /最新帖子:getItemPosition:LatestPostFragment {1ae5125f#2 id = 0x7f0a008a} 04-28 10:24:46.940 5127-5127 / com.breathesport.breathesportapp I /最新帖子:getItemPosition latestpost 04-28 10:24:46.940 5127-5127 / com.breathesport.breathesportapp I /最新帖子:getItemPosition:LatestPostFragment {139bf4ac#1 id = 0x7f0a008a} 04-28 10:24:46.940 5127-5127 / com.breathesport.breathesportapp I /最新帖子:getItemPosition latestpost 04-28 10:24:46.946 5127-5127 / com.breathesport.breathesportapp I /最新帖子:getItem pos:3 04-28 10:24:46.946 5127-5127 / com.breathesport.breathesportapp I /最新帖子:getItem latest 04-28 10:24:46.946 5127-5127 / com.breathesport.breathesportapp I /最新帖子:getItem pos:2 04-28 10:24:46.946 5127-5127 / com.breathesport.breathesportapp I /最新帖子:getItem latest 04-28 10:24:46.946 5127-5127 / com.breathesport.breathesportapp I /最新帖子:getItem pos:4 04-28 10:24:46.946 5127-5127 / com.breathesport.breathesportapp I /最新帖子:getItem latest
我的猜测是getItemPosition无法理解哪一个是currentFragment,我可能在我的实现中做错了。 我希望有人可以提供帮助