2
我已經使用ViewPager滾動片段。SherlockFragments在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。
在此先感謝
感謝您的快速響應。 現在正在做薑餅 – anurag 2013-02-15 11:32:45
不用客氣^^ btw。這是一種解決方法,默認設置爲2.它告訴Android在內存中保存靜態數量的視圖,通常內容不會超過兩個視圖,因爲動態內容可能會導致內存堆 – Nickolaus 2013-02-15 11:42:28