Mikheil Zhghenti
Mikheil Zhghenti

Reputation: 728

How to change data of the current fragment in TabLayout?

How is it possible to change dynamically the content of the current fragment in TabLayout. For example, if there is a list on the current fragment, and the list is changed, how to update the fragment layout?

Here is the default generated code (selected from New-> Activity->Tabbed Activity)

 public static class PlaceholderFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    public PlaceholderFragment() {
    }

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        TextView textView = (TextView) rootView.findViewById(R.id.section_label);
        int areaID = getArguments().getInt(ARG_SECTION_NUMBER);
        textView.setText(getString(R.string.section_format, areaID));
        return rootView;
    }
}

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        return PlaceholderFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "SECTION 1";
            case 1:
                return "SECTION 2";
            case 2:
                return "SECTION 3";
        }
        return null;
    }
}

Upvotes: 0

Views: 551

Answers (1)

Mauker
Mauker

Reputation: 11487

Your question is not a Fragment problem per say. Since you're using a ListView, you could either use a predefined adapter, or create your own custom adapter. Check this answer of mine on how to create a custom one.

But long story short you'll need to:

  • Either use an ArrayAdapter (As seem on this question), or create your own, as stated above on my answer.

  • Every time you modify your adapter/dataset, call the method myAdapter#notifyDataSetChanged();

Upvotes: 1

Related Questions