gpt4 book ai didi

android - 滚动时 SherlockFragments 中 ViewPager 的可见性问题(在 Gingerbread 中)

转载 作者:行者123 更新时间:2023-11-29 01:56:25 25 4
gpt4 key购买 nike

我使用 ViewPager 在 fragment 之间滚动。

在 Gingerbread 上测试时有一个奇怪的问题-

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();
}
}

Fragments 正在调用 onVisibilityChanged 来执行一些滚动操作。这些 fragment 扩展了 SherlockFragment。

提前致谢

最佳答案

使用 setoffscreenpagelimit(int count) 将您的 ViewPager 的 OffscreenPageLimit 设置为您的 ViewPager 实际持有的 View 数。

关于android - 滚动时 SherlockFragments 中 ViewPager 的可见性问题(在 Gingerbread 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14893587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com