加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 刷新时刷新Android Viewpager片段

发布时间:2020-12-15 05:03:38 所属栏目:Java 来源:网络整理
导读:我是 Android的新手,我真的不明白为什么动态添加的Fragment内容(例如在按钮点击后添加的某些图像)在滚动一些Fragment然后再返回后消失了. 有很简单的代码Activity和Fragment: public class MyActivity extends FragmentActivity { @Override public void on
我是 Android的新手,我真的不明白为什么动态添加的Fragment内容(例如在按钮点击后添加的某些图像)在滚动一些Fragment然后再返回后消失了.

有很简单的代码Activity和Fragment:

public class MyActivity extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
        final CustomAdapter adapter = new CustomAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
    }

    class CustomFragment extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment,container,false);
        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            getView().findViewById(R.id.clickMeButton).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getView().findViewById(R.id.image).setVisibility(View.VISIBLE);
                }
            });
        }

    }

    class CustomAdapter extends FragmentStatePagerAdapter {

        private List<CustomFragment> fragments = Arrays.asList(
            new CustomFragment(),new CustomFragment(),new CustomFragment()
        );

        public CustomAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            return fragments.get(i);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }

}

和适当的xmls:

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">

    <android.support.v4.view.ViewPager
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/viewPager" />

</LinearLayout>

fragment.xml之

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

    <Button
            android:id="@+id/clickMeButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="click me"/>

    <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher"
            android:visibility="gone"/>

</LinearLayout>

逻辑很简单.在每个片段上,我可以单击一个按钮,结果会出现一个图像.

它有效.但……

当我在第一个片段上显示图像时,滚动到第三个片段并再次返回到第一个片段,图像消失了.

我该怎么做才能防止这种情况发生?我应该以某种方式保存可见度吗?

解决方法

发生这种情况是因为当Fragment滚动出视图时,FragmentStatePagerAdapter会释放与Fragment关联的内存.从 docs:

When pages are not visible to the user,their entire fragment may be
destroyed,only keeping the saved state of that fragment. This allows
the pager to hold on to much less memory associated with each visited
page as compared to FragmentPagerAdapter at the cost of potentially
more overhead when switching between pages.

您可以尝试以下四种选项之一:

1.由于页面数量较少,因此可以使用FragmentPagerAdapter而不是FragmentStatePagerAdapter. FragmentPagerAdapter保留创建的碎片,并且不会在滑动时销毁它们.

2.在Fragment中,将一个布尔值存储在SharedPreference中,并在使图像可见或不可见时设置(或清除)其值.然后在Fragment的onResume()方法中,根据SharedPreference的值使图像可见或不可见.

3.在Fragment的onCreate()中,调用setRetainInstance(true);.

4.如果碎片的数量是固定的,并且相对较小,然后在你的onCreate()中添加以下代码:

mViewPager = (ViewPager)findViewById(R.id.pager);
mViewPager.setOffscreenPageLimit(limit);         /* limit is a fixed integer*/

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读