在刷卡ViewPager更新片段片段、ViewPager

2023-09-03 21:56:05 作者:洒脱人生

我有一个问题,我一直在挣扎,在过去2天。

i have a problem that i have been struggling with for the past 2 days.

我建立一个使用动作条,ViewPager和放大器的应用程序; FragmentPagerAdapter。 在code的活动,碎片和放大器; FragmentPagerAdapter正是为那些对http://developer.android.com/reference/android/support/v4/view/ViewPager.html

I am building an app that uses ActionBar, ViewPager & FragmentPagerAdapter. The code for the Activity, Fragments & FragmentPagerAdapter are exactly as the ones stated in the android example on http://developer.android.com/reference/android/support/v4/view/ViewPager.html

我面临的问题是 - 假如我只有2个在viewPager片段。切换时/两者之间刷卡时,碎片不会得到更新。 onResume不会被调用因为viewPager至少1片段缓存于所显示的片段任一侧。

The problem i am facing is -- assuming i have only 2 fragments in the viewPager. when switching/swiping between the two, the fragments are not getting updated. onResume does not get called because the viewPager caches a minimum of 1 fragment to either side of the displayed fragment.

我试着用onTabSelected检测当选择一个片段,然后从一个接口的帮助下(低于code)该片段开始的方法。

I tried using the onTabSelected to detect when a fragment is selected and then start a method from that fragment with the help of an interface (code below).

public void onTabSelected(Tab tab, FragmentTransaction ft) {
    TabInfo tag = (TabInfo)tab.getTag();
    for (int i=0; i<mTabs.size(); i++) {
        if (mTabs.get(i) == tag) {
            mViewPager.setCurrentItem(i);
        }
    }
    ((IStartStop)getItem(tab.getPosition())).Start();
}

然而,当开始方法用于NullPointerException的尝试更新一个TextView时被触发。 start方法的code是:

However, when the Start method is used a NullPointerException is fired when trying to update a textview. The start method's code is:

public void Start() {
    TextView tv = _view.findViewById(R.id.text);
    tv.setText("test");
}

引发异常的行:

The exception is thrown at line:

TextView tv = _view.findViewById(R.id.text);

该IStartStop界面相当简单:

The IStartStop interface is quite simple:

public interface IStartStop {
    public void Start();
    public void Stop();
}

我不想使用notifyDataSetChanged();与POSITION_NONE因为每次我刷到一个新的片段,它需要几秒钟的加载片段

I don't want to use notifyDataSetChanged(); with POSITION_NONE because every time I swipe to a new fragment, it takes a few seconds to load the fragments

目前时间,所述片段仅包括一个TextView,在将来,他们将有一个动画等,重要的是:

At this time, the fragments only include a textview, in the future they will have an animation and so it is important to:

1-仅运行一个动画时所述片段被选择而不是当它旁边的片段被选择(方式ViewPager缓存和恢复片段)。

1- Only run an animation when the fragment is selected and not when the fragment next to it is selected (the way ViewPager caches and resumes fragments).

2 - 停止时的片段不再被选中,以避免浪费设备资源的动画。

2- Stop the animation when the fragment is no longer selected to avoid wasting device resources.

是的,我已经检查可用的一切在互联网上,但似乎没有和我一起工作。

Yes, i already checked everything available on the internet but nothing seems to work with me.

非常感谢您的帮助!

推荐答案

令人惊讶的是 ViewPager 不会做这种原生(除其他事项外)。但并非所有的损失。

Surprisingly the ViewPager doesn't do this "natively" (among other things). But not all is lost.

首先,你必须修改你的碎片,所以它们只是运行,当你告诉他们,这是确定,而不是当他们被实例化的动画。这样,您就可以与viewpager偏移量(默认= 3)播放,并有2-3个片段ploaded $ P $但不是动画。

First you have to modify your fragments so they only run the animation when you tell them that it's ok and not when they are instantiated. This way you can play with the viewpager offset (default = 3) and have 2-3 fragments preloaded but not animated.

第二个步骤是创建一个定义当片段已成为可见的接口或相似的。

Second step is to create an interface or similar that defines when the "fragment has become visible".

第三步是将附加一个新的OnPageScrollListener您viewpager。

Third step would be to attach a new OnPageScrollListener to your viewpager.

code以下(处于半untested- code):

Code follows (in semi-untested-code):

1)将监听器:

mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(final int i, final float v, final int i2) {
            }
            @Override
            public void onPageSelected(final int i) {
                YourFragmentInterface fragment = (YourFragmentInterface) mPagerAdapter.instantiateItem(mViewPager, i);
                if (fragment != null) {
                    fragment.fragmentBecameVisible();
                } 
            }
            @Override
            public void onPageScrollStateChanged(final int i) {
            }
        });

2)这是你的接口:

2) This is your Interface:

public interface YourFragmentInterface {
    void fragmentBecameVisible();
}

3)改变你的片段,使他们实现这一点:

3) Change your fragments so they implement this:

public class YourLovelyFragment extends Fragment implements YourFragmentInterface {

4)实现该接口在片段

4) Implement the interface in the fragment

@Override
public void fragmentBecameVisible() {
    // You can do your animation here because we are visible! (make sure onViewCreated has been called too and the Layout has been laid. Source for another question but you get the idea.
}

在哪里何去何从?

您可能想实现一个方法/监听器通知其他的片段,他们将不再可见(即当一个是可见的,其他的都没有)。但是,这可能并不需要。

You might want to implement a method/listener to notify the "other" fragments that they are no longer visible (i.e. when one is visible, the others are not). But that may not be needed.