How to create dynamic tabs and add different functionality to each fragment in Android?

后端 未结 1 1634
别跟我提以往
别跟我提以往 2021-01-16 04:00

I knew how to create static Tabs, work with a volley to get data in android and work in each fragment but how to create dynamic tabs using JSON and store differ

相关标签:
1条回答
  • 2021-01-16 04:24

    You can do like this.

    public class TabActivity extends AppCompatActivity {
    
    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    // Fragment List
    private final List<Fragment> mFragmentList = new ArrayList<>();
    // Title List
    private final List<String> mFragmentTitleList = new ArrayList<>();
    private ViewPagerAdapter adapter;
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab);
    
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    
        viewPager = (ViewPager) findViewById(R.id.container);
        initTitle();
    
        // edited , you can use multiple titles and one Fragment
        for (int i = 0; i < mFragmentTitleList.size(); i++) {
            mFragmentList.add(new OneFragment());
        }
    
        setupViewPager(viewPager);
    
        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
        // Tab ViewPager setting
        viewPager.setOffscreenPageLimit(mFragmentList.size());
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabsFromPagerAdapter(adapter);
    }
    
    /**
     * load title data ,it muest below ViewPagerAdapter initialize
     */
    private void initTitle() {
        String response = "{\n" +
                "    \"status\": 200,\n" +
                "    \"message\": \"Purchase order updated successfully\",\n" +
                "    \"data\": {\n" +
                "        \"page_title\": \"Home | Welcome\",\n" +
                "        \"menu\": [\n" +
                "            {\n" +
                "                \"cat_id\": \"1\",\n" +
                "                \"cat_name\": \"मुख्य समाचार\",\n" +
                "                \"description\": \"\"             \n" +
                "            },\n" +
                "            {\n" +
                "                \"cat_id\": \"2\",\n" +
                "                \"cat_name\": \"राजनीती \",\n" +
                "                \"description\": \"\"\n" +
                "            }";
    
        try {
            JSONObject jsonObject = new JSONObject(response);
            int status = jsonObject.getInt("status");
            if (status == 200) {
                JSONObject data = jsonObject.getJSONObject("data");
                JSONArray menu = data.getJSONArray("menu");
                for (int i = 0; i < menu.length(); i++) {
                    String cat_name = menu.getJSONObject(i).getString("cat_name");
                    mFragmentTitleList.add(cat_name);
                }
            } else {
                for (int i = 0; i < mFragmentList.size(); i++) {
                    mFragmentTitleList.add("no title");
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    
    private void setupViewPager(ViewPager viewPager) {
    
        adapter = new ViewPagerAdapter(getSupportFragmentManager(), mFragmentList, mFragmentTitleList);
    
        viewPager.setAdapter(adapter);
    }
    
    /**
     * ViewPagerAdapter setting
     */
    class ViewPagerAdapter extends FragmentPagerAdapter {
        private List<Fragment> mFragmentList = new ArrayList<>();
        private List<String> mFragmentTitleList = new ArrayList<>();
    
        public ViewPagerAdapter(FragmentManager fm, List<Fragment> fragments, List<String> titleLists) {
            super(fm);
            this.mFragmentList = fragments;
            this.mFragmentTitleList = titleLists;
        }
    
        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }
    
        @Override
        public int getCount() {
            return mFragmentList == null ? 0 : mFragmentList.size();
        }
    
        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }
    }
    

    Hope to help you.

    0 讨论(0)
提交回复
热议问题