导航到带有选项卡的页面以外的页面并返回后,所有选项卡的内容都会消失

时间:2018-05-11 11:40:00

标签: android tabs

我有动态创建标签的代码。

这是我的MyActivity.java:

public class MyActivity extends AppCompatActivity {
.
.
.
.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.audit_form_activity);


        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        tabLayout = (TabLayout) findViewById(R.id.tabLayout);


        try {
            JSONObject jsonObj = new JSONObject(page);

            JSONArray jssArray = jsonObj.getJSONArray("AuditQuestions");
            int jsonArrLength = jssArray.length();

            no_of_categories = jsonArrLength;

            for (int i = 0; i < jsonArrLength; i++) {
                JSONObject jsonChildObj = jssArray.getJSONObject(i);


                categoryName.add(jsonChildObj.getString("categoryName"));


            }

        } catch (JSONException e) {
            e.printStackTrace();
        }


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


        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        viewPager = (ViewPager) findViewById(R.id.pager);

        viewPager.setOffscreenPageLimit(no_of_categories);

        setupViewPager(viewPager);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setOnTabSelectedListener(onTabSelectedListener(viewPager));

    }

    private void setupViewPager(ViewPager viewPager) {
        Pager adapter = new Pager(getSupportFragmentManager());
        for (int i = 0; i < no_of_categories; i++) {
            Tab1 tab1 = new Tab1();
            adapter.addFragment(tab1, categoryName.get(i));

            viewPager.setAdapter(adapter);
            viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
            onTabSelectedListener(viewPager);
        }
    }

    private TabLayout.OnTabSelectedListener onTabSelectedListener(final ViewPager pager) {
        return new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                pager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        };
    }

}

这是我的适配器Pager.java:

public class Pager extends FragmentStatePagerAdapter  {
    int mNumOfTabs;
    Fragment fragment = null;

    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public Pager(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

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

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

在从MyActivity.java中删除此行时,即使在离开标签页并返回后,内容也会显示在标签中:

    viewPager.setOffscreenPageLimit(no_of_categories);

但它并没有解决问题,因为标签中显示的内容不一致,即应该出现在一个标签中的内容出现在其他标签上。

我试图解决它,但没有成功。可能与片段的生命周期有关吗?

我该如何解决这个问题?

无法为此找到任何解决方案。请帮忙。

任何帮助将不胜感激。

提前致谢!

0 个答案:

没有答案