进入视图时通知片段?

时间:2014-04-07 00:58:15

标签: android android-fragments android-viewpager

我试图知道用户何时在我的viewpager中滑入或滑出碎片。我设法做到了但问题是,当ViewPager首次创建时,片段0永远不会知道它是可见的。

我尝试在MainActivity onCreate上手动调用onPageSelected,但它崩溃了我的应用。

如何进入视图时,如何通知第一个片段?如果只有在片段完全可见的情况下才能通知片段(有时在此之前稍微调用onPageSelected),这将是一个加号。

    mPager = (ViewPager) findViewById(R.id.pager);

    // Listen for swipes
    final ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageScrolled(int i, float v, int i2) {
        }

        @Override
        public void onPageSelected(int position) {
            MiniApp fragment = (MiniApp) mPagerAdapter.instantiateItem(mPager, position); 
            // Error
            if (fragment != null) {
                fragment.onFragmentEntered();
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    };
    mPager.setOnPageChangeListener(pageChangeListener);
    pageChangeListener.onPageSelected(0);

    mPager.setAdapter(mPagerAdapter);

如何告诉第一个屏幕上的片段是否可见?


这是错误日志:

04-11 20:56:23.697    3537-3537/com.example.android.launcher E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.android.launcher, PID: 3537
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.launcher/com.example.android.launcher.app.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:374)
            at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:369)
            at com.example.android.launcher.app.fragments.Clock.onFragmentEntered(Clock.java:54)
            at com.example.android.launcher.app.MainActivity$1.onPageSelected(MainActivity.java:154)
            at com.example.android.launcher.app.MainActivity.onCreate(MainActivity.java:137)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:0)

手动拨打OnPageChangeListener.onPageSelected

    final OnPageChangeListener pageChangeListener = new SimpleOnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);
            // When the page changes tell fragment visibility changed
            VisibilityChange fragment = (VisibilityChange) mPagerAdapter.instantiateItem(
                    mPager, position);

            if (fragment != null) {
                fragment.onVisibilityChange(true);
            }
            fragment = (VisibilityChange) mPagerAdapter.instantiateItem(mPager, lastPosition);
            if (fragment != null) {
                fragment.onVisibilityChange(false);
            }
            lastPosition = position;
        }
    };

    mPager.setOnPageChangeListener(pageChangeListener);
    pageChangeListener.onPageSelected(0);

答案 1 :(得分:0)

试试这个: -

 // Manages the Tab changes, synchronizing it with Pages
    public void onTabChanged(String tag) 
       {
        int pos = this.mTabHost.getCurrentTab();
        mTabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.Drawable.XYZ); 
        this.mViewPager.setCurrentItem(pos);
       }

    // Manages the Page changes, synchronizing it with Tabs
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
    int pos = this.mViewPager.getCurrentItem();
    this.mTabHost.setCurrentTab(pos);

}

答案 2 :(得分:0)

您可以覆盖片段setUserVisibleHint(boolean isVisibleToUser)。如果isVisibleToUsertrue,则您的片段在寻呼机中可见。