onResume()
方法。那么,有没有更好的方法来处理恢复操作?
答案 0 :(得分:40)
代码如下:
步骤:1
创建界面:
public interface YourFragmentInterface {
void fragmentBecameVisible();
}
步骤:2
在setOnPageChangeListener中附加Listner :
mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(final int position, final float v, final int i2) {
}
@Override
public void onPageSelected(final int position) {
YourFragmentInterface fragment = (YourFragmentInterface) mPagerAdapter.instantiateItem(mViewPager, position);
if (fragment != null) {
fragment.fragmentBecameVisible();
}
}
@Override
public void onPageScrollStateChanged(final int position) {
}
});
步骤:3
在片段中实现界面:
public class yourActivity extends SherlockFragment implements YourFragmentInterface{
@Override
public void fragmentBecameVisible() {
System.out.println("TestFragment");
}
}
在片段切换期间,在此方法调用的所有yor片段活动fragmentBecameVisible()
中实现此列表器。
感谢。
答案 1 :(得分:3)
我想,我找到了答案。这是链接,每当片段变得可见时,将强制调用片段的onResume()
Fragments onResume from back stack
答案 2 :(得分:2)
您可以通过在选择页面的每个public
上提供fragment
方法来恢复工作,我举个例子:
public class MainActivity extends FragmentActivity {
private TabHost mTabHost;
private ViewPager mViewPager;
private TabsAdapter mTabsAdapter;
private ArrayList<Fragment> mFragments;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
mViewPager = (ViewPager) findViewById(R.id.pager);
mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager);
mFragments = new ArrayList<Fragment>();
mFragments.add(new TestFragment1());
mFragments.add(new TestFragment2());
mFragments.add(new TestFragment3());
mTabsAdapter.addTab(mTabHost.newTabSpec("fragment1").setIndicator("fragment1"));
mTabsAdapter.addTab(mTabHost.newTabSpec("fragment2").setIndicator("fragment2"));
mTabsAdapter.addTab(mTabHost.newTabSpec("fragment3").setIndicator("fragment3"));
if (savedInstanceState != null) {
mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("tab", mTabHost.getCurrentTabTag());
}
private void refreshPage(int i) {
Fragment fragment = mFragments.get(i);
switch (i) {
case 0:
((TestFragment1) fragment).refreshView();
break;
case 1:
((TestFragment2) fragment).refreshView();
break;
case 2:
((TestFragment3) fragment).refreshView();
break;
}
}
class TabsAdapter extends FragmentPagerAdapter implements TabHost.OnTabChangeListener,
ViewPager.OnPageChangeListener {
private final Context mContext;
private final TabHost mTabHost;
private final ViewPager mViewPager;
public TabsAdapter(FragmentActivity activity, TabHost tabHost, ViewPager pager) {
super(activity.getSupportFragmentManager());
mContext = activity;
mTabHost = tabHost;
mViewPager = pager;
mTabHost.setOnTabChangedListener(this);
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}
public void addTab(TabHost.TabSpec tabSpec) {
tabSpec.setContent(new DummyTabFactory(mContext));
mTabHost.addTab(tabSpec);
notifyDataSetChanged();
}
@Override
public Fragment getItem(int i) {
return mFragments.get(i);
}
@Override
public void onPageScrolled(int i, float v, int i2) {
}
@Override
public void onPageSelected(int i) {
TabWidget widget = mTabHost.getTabWidget();
int oldFocusability = widget.getDescendantFocusability();
widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
mTabHost.setCurrentTab(i);
widget.setDescendantFocusability(oldFocusability);
refreshPage(i);
}
@Override
public void onPageScrollStateChanged(int i) {
}
@Override
public void onTabChanged(String s) {
int postion = mTabHost.getCurrentTab();
mViewPager.setCurrentItem(postion, true);
}
@Override
public int getCount() {
return mFragments.size();
}
class DummyTabFactory implements TabHost.TabContentFactory {
private final Context mContext;
public DummyTabFactory(Context context) {
mContext = context;
}
@Override
public View createTabContent(String s) {
View v = new View(mContext);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
return v;
}
}
}
}
然后你就像这样写Fragment
:
public class TestFragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.main, container, false);
}
public void refreshView() {
//do whatever you want
}
}
答案 3 :(得分:2)
您可以使用界面!!!
简单地为每个片段模拟onResume()1-创建一个名为FragmentLifecycle的接口,代码如下:
public interface FragmentLifecycle {
public void onResumeFragment();
}
2-让每个Fragment实现接口:
public class FragmentOne extends Fragment implements FragmentLifecycle
3-在每个片段中实现接口方法:
@Override
public void onResumeFragment() {
Log.i(TAG, "onResumeFragment()");
Toast.makeText(getActivity(), "onResumeFragment():" + TAG,Toast.LENGTH_SHORT).show();
}
4-调用ViewPager页面上的接口方法:
viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
FragmentLifecycle fragmentToShow = (FragmentLifecycle)adapter.getItem(position);
fragmentToShow.onResumeFragment();
}
});
享受!
答案 4 :(得分:1)
你可以试试这个,
步骤1:覆盖活动中的Tabselected方法
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
try {
if(MyEventsFragment!=null && tab.getPosition()==3)
{
MyEvents.fragmentChanged();
}
}
catch (Exception e)
{
}
mViewPager.setCurrentItem(tab.getPosition());
}
步骤2:使用静态方法在片段中执行您想要的操作,
public static void fragmentChanged()
{
Toast.makeText(actvity, "Fragment Changed", Toast.LENGTH_SHORT).show();
}
答案 5 :(得分:0)
我使用片段覆盖方法解决了这个问题:
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
if (getView() != null) {
// your code goes here
}
}
}
注意:在onCreateView之前调用的Viewpager第一个片段setUserVisibleHint中,所以你需要处理它。
handle setUserVisibleHint called before onCreateView in Fragment