TabLayout+ViewPager+Fragment, 为什么Fragment不走onHiddenChanged方法 TabLayout+ViewPager+Fragment, 为什么Fragment不走onHiddenChanged方法 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 因为Fragment都是被销毁后新建的,不是原来的 先设置viewpager的setOffscreenPageLimit为fragment的总数,防止fragment销毁,其次,切换fragment会调用setUserVisibleHint方法,而不是onHiddenChanged。具体看FragmentPagerAdapter中的代码,参考public abstract class FragmentPagerAdapter extends PagerAdapter { …… public Object instantiateItem(ViewGroup container, int position) { if(this.mCurTransaction == null) { this.mCurTransaction = this.mFragmentManager.beginTransaction(); } long itemId = this.getItemId(position); String name = makeFragmentName(container.getId(), itemId); Fragment fragment = this.mFragmentManager.findFragmentByTag(name); if(fragment != null) { this.mCurTransaction.attach(fragment); } else { fragment = this.getItem(position); this.mCurTransaction.add(container.getId(), fragment, makeFragmentName(container.getId(), itemId)); } if(fragment != this.mCurrentPrimaryItem) { fragment.setMenuVisibility(false); fragment.setUserVisibleHint(false); } return fragment; } public void destroyItem(ViewGroup container, int position, Object object) { if(this.mCurTransaction == null) { this.mCurTransaction = this.mFragmentManager.beginTransaction(); } this.mCurTransaction.detach((Fragment)object); } public void setPrimaryItem(ViewGroup container, int position, Object object) { Fragment fragment = (Fragment)object; if(fragment != this.mCurrentPrimaryItem) { if(this.mCurrentPrimaryItem != null) { this.mCurrentPrimaryItem.setMenuVisibility(false); this.mCurrentPrimaryItem.setUserVisibleHint(false); } if(fragment != null) { fragment.setMenuVisibility(true); fragment.setUserVisibleHint(true); } this.mCurrentPrimaryItem = fragment; } }} 只有FragmentTransaction的show()和hide()方法才会调用onHiddenChanged()方法 getUserVisibleHint获得当前是否可见 android文件监控 如何做基于MMS协议的音乐播放器,请大家给提供个思路。 android问题想在java类里面创建TextView然后放进布局里怎么写。。不想在配置文件里写 (自己开发的开源引擎)遇到问题希望高手解答! adb shell 如何做任务切换 这么通过代码设置里面的号码 网站手机版问题 图片过大显示不正常 极度依赖Android Studio的代码补全功能,这样会不会很危险?还有你们是熟记一些常见代码,还是临时copy修改? 【求助】夜神模拟器打不开蓝牙 droidplugin插件化开发,卡在“努力加载中”怎么办? 安卓报错
……
public Object instantiateItem(ViewGroup container, int position) {
if(this.mCurTransaction == null) {
this.mCurTransaction = this.mFragmentManager.beginTransaction();
} long itemId = this.getItemId(position);
String name = makeFragmentName(container.getId(), itemId);
Fragment fragment = this.mFragmentManager.findFragmentByTag(name);
if(fragment != null) {
this.mCurTransaction.attach(fragment);
} else {
fragment = this.getItem(position);
this.mCurTransaction.add(container.getId(), fragment, makeFragmentName(container.getId(), itemId));
} if(fragment != this.mCurrentPrimaryItem) {
fragment.setMenuVisibility(false);
fragment.setUserVisibleHint(false);
} return fragment;
} public void destroyItem(ViewGroup container, int position, Object object) {
if(this.mCurTransaction == null) {
this.mCurTransaction = this.mFragmentManager.beginTransaction();
} this.mCurTransaction.detach((Fragment)object);
} public void setPrimaryItem(ViewGroup container, int position, Object object) {
Fragment fragment = (Fragment)object;
if(fragment != this.mCurrentPrimaryItem) {
if(this.mCurrentPrimaryItem != null) {
this.mCurrentPrimaryItem.setMenuVisibility(false);
this.mCurrentPrimaryItem.setUserVisibleHint(false);
} if(fragment != null) {
fragment.setMenuVisibility(true);
fragment.setUserVisibleHint(true);
} this.mCurrentPrimaryItem = fragment;
}
}
}