如何动态更改fragment
中当前TabLayout
的内容。例如,如果当前片段上有列表,并且列表已更改,那么如何更新片段布局?
以下是默认生成的代码(从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;
}
}
答案 0 :(得分:1)
您的问题不是Fragment
问题。由于您使用的是ListView
,因此您可以使用预定义的适配器,也可以创建自己的自定义适配器。检查this answer of mine有关如何创建自定义帐户的信息。
但长话短说,你需要:
使用ArrayAdapter
(如this question所示)或创建自己的,如上所述。
每次修改适配器/数据集时,请调用方法myAdapter#notifyDataSetChanged();