标签内容未显示

时间:2018-06-18 12:58:20

标签: android android-viewpager

使用片段管理器无法停止在标签更改上重新加载片段后,我使用viewpager实现了它。 但内容并未显示在标签中。一切似乎都是正确的。我已经尝试了几个小时,无法弄清问题是什么。 这是我活动的代码:

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

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

        button = (Button) findViewById(R.id.button);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setOffscreenPageLimit(no_of_categories);

        setupViewPager(viewPager);

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

        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener()

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

                viewPager.setCurrentItem(tab.getPosition());
            }

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

            }

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

            }
        });

    }


    private void setupViewPager(ViewPager viewPager) {


        //       Outside Loop
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());


//        Get the JSON Data from DataSource

        try {
            JSONObject pageObj = new JSONObject(page);
            JSONArray auditQuestionsArray = pageObj.getJSONArray("AuditQuestions");
            int auditQuestionsArrayLength = auditQuestionsArray.length();
            no_of_categories = auditQuestionsArrayLength;


//        Outer-Loop Through the CategoryNames to Create Fragments for each CategoryName.

            for (int i = 0; i < no_of_categories; i++) {
                JSONObject auditQuestionsArrayObject = auditQuestionsArray.getJSONObject(i);
                category = auditQuestionsArrayObject.getString("categoryName");
                //JSONArray questionListArray = auditQuestionsArrayObject.getJSONArray("QuestionList");


                Tab1 myFrag = Tab1.newInstance(category);


                adapter.addFrag(myFrag, category);

            }


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


        viewPager.setAdapter(adapter);


    }



    public void bringBackBoolean(Boolean booleanReceived) {
        //bList.add(booleanReceived);


    }

这是我的寻呼机适配器的代码,它是早期活动中的内部类:

class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

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

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

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

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

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

请帮我解决问题。

0 个答案:

没有答案