我使用 ViewPager 在片段之间滚动。
在姜饼上进行测试时,有一个奇怪的问题——
After scrolling first two tabs, the visibility of all fragments GONE completely
这一直持续到我改变方向。此问题仅在姜饼设备启动时出现。它在 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。
提前致谢