31

onResume()当我们在片段之间切换不止一次时,方法不会被调用。那么,有没有更好的方法来处理恢复操作?

4

6 回答 6

41

代码如下:

步骤1

创建接口

public interface YourFragmentInterface {
    void fragmentBecameVisible();
}

第2步

在 setOnPageChangeListener 中附加侦听器

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

fragmentBecameVisible()在片段切换期间此方法调用的所有片段活动中实现此侦听器。

谢谢。

于 2014-05-14T06:59:16.757 回答
4

您可以使用接口简单地为每个片段模拟 onResume() !!!

1-创建一个名为 FragmentLifecycle 的接口,代码如下:

public interface FragmentLifecycle {
   public void onResumeFragment();
}

2-让每个片段实现接口:

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

享受!

于 2016-08-31T07:44:32.413 回答
3

我想,我找到了答案。这是onResume()每次片段变得可见时都会强制调用片段的链接。
来自后台堆栈的片段 onResume

于 2013-07-24T22:25:33.567 回答
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
}
}
于 2013-07-24T22:31:38.420 回答
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();
}
于 2016-01-20T12:18:38.923 回答
0

我已经使用片段覆盖方法解决了这个问题:

 @Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) {
        if (getView() != null) {
            // your code goes here
        }
    }
}

注意:在 Viewpager 第一个片段 setUserVisibleHint 之前调用 onCreateView 所以你需要处理这个。

在 Fragment 中的 onCreateView 之前调用的句柄 setUserVisibleHint

于 2015-12-03T13:31:30.373 回答