フラグメント間をスクロールするために ViewPager を使用しました。
ジンジャーブレッドのテスト中に、奇妙な問題が発生しました-
After scrolling first two tabs, the visibility of all fragments GONE completely
これは向きを変えるまで続きます。この問題は、gingerbread デバイスのみで起動時にのみ発生します。ICSなどでは問題なく動作しています。
これがTabsAdapterのコードです
/**
* This is a helper class that implements the management of tabs and all
* details of connecting a ViewPager with associated TabHost. It relies on a
* trick. Normally a tab host has a simple API for supplying a View or
* Intent that each tab will show. This is not sufficient for switching
* between pages. So instead we make the content part of the tab host 0dp
* high (it is not shown) and the TabsAdapter supplies its own dummy view to
* show as the tab content. It listens to changes in tabs, and takes care of
* switch to the correct paged in the ViewPager whenever the selected tab
* changes.
*/
private class TabsAdapter extends FragmentPagerAdapter implements
ViewPager.OnPageChangeListener, ActionBar.TabListener {
private final Context mContext;
private final ActionBar mActionBar;
private final ViewPager mViewPager;
private final List<String> mTabs = new ArrayList<String>();
private final List<Integer> mTabsId = new ArrayList<Integer>();
private boolean hasClearedDetails = false;
private int mCurrentPosition = -1;
/**
* Used during page migration, to remember the next position
* {@link #onPageSelected(int)} specified.
*/
private int mNextPosition = -1;
public TabsAdapter(FragmentActivity activity, ActionBar actionBar, ViewPager pager) {
super(activity.getSupportFragmentManager());
mContext = activity;
mActionBar = actionBar;
mViewPager = pager;
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}
public void addTab(ActionBar.Tab tab, Class<?> clss, int tabId) {
mTabs.add(clss.getName());
mTabsId.add(tabId);
mActionBar.addTab(tab.setTabListener(this));
notifyDataSetChanged();
}
public void removeTabAt(int location) {
mTabs.remove(location);
mTabsId.remove(location);
mActionBar.removeTabAt(location);
notifyDataSetChanged();
}
public Integer getIdForPosition(int position) {
if(position >= 0 && position < mTabsId.size()) {
return mTabsId.get(position);
}
return null;
}
public Integer getPositionForId(int id) {
int fPos = mTabsId.indexOf(id);
if(fPos >= 0) {
return fPos;
}
return null;
}
@Override
public int getCount() {
return mTabs.size();
}
@Override
public Fragment getItem(int position) {
return Fragment.instantiate(mContext, mTabs.get(position), new Bundle());
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
clearDetails();
if (mViewPager.getCurrentItem() != tab.getPosition()) {
Log.e(THIS_FILE+"Anurag debugging","ISSUE HERE");
mViewPager.setCurrentItem(tab.getPosition(), true);
}
}
@Override
public void onPageSelected(int position) {
mActionBar.setSelectedNavigationItem(position);
if (mCurrentPosition == position) {
Log.w(THIS_FILE, "Previous position and next position became same (" + position
+ ")");
Log.e(THIS_FILE, "Previous position and next position became same (" + position
+ ")");
}
mNextPosition = position;
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// Nothing to do
}
@Override
public int getItemPosition(Object object){
return mCurrentPosition;
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// Nothing to do
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// Nothing to do
}
@Override
public void onPageScrollStateChanged(int state) {
switch (state) {
case ViewPager.SCROLL_STATE_IDLE: {
Log.e(THIS_FILE, "SCROLL_STATE_IDLE"+mCurrentPosition);
if (mCurrentPosition >= 0) {
sendFragmentVisibilityChange(mCurrentPosition, false);
}
if (mNextPosition >= 0) {
sendFragmentVisibilityChange(mNextPosition, true);
}
invalidateOptionsMenu();
mCurrentPosition = mNextPosition;
break;
}
case ViewPager.SCROLL_STATE_DRAGGING:
Log.e(THIS_FILE, "SCROLL_STATE_DRAGGING");
clearDetails();
hasClearedDetails = true;
break;
case ViewPager.SCROLL_STATE_SETTLING:
Log.e(THIS_FILE, "SCROLL_STATE_SETTLING");
hasClearedDetails = false;
break;
default:
break;
}
}
private void clearDetails() {
if (mDualPane && !hasClearedDetails) {
Log.e(THIS_FILE, "check in clear detalis");
FragmentTransaction ft = SipHome.this.getSupportFragmentManager()
.beginTransaction();
ft.replace(R.id.details, new Fragment(), null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
}
}
sendFragmentVisibilityChange のコードは次のとおりです。
private void sendFragmentVisibilityChange(int position, boolean visibility) {
try {
final Fragment fragment = getFragmentAt(position);
if (fragment instanceof ViewPagerVisibilityListener) {
((ViewPagerVisibilityListener) fragment).onVisibilityChanged(visibility);
}
}catch(IllegalStateException e) {
Log.e(THIS_FILE, "Fragment not anymore managed");
e.printStackTrace();
}catch(Exception e) {
Log.e(THIS_FILE, "Fragment not anymore managed");
e.printStackTrace();
}
}
フラグメントは onVisibilityChanged を呼び出して、スクロールに関する操作を実行しています。フラグメントは SherlockFragment を拡張します。
前もって感謝します