Add a ListView in Fragment without using ListFragment. Add a listview in fragment.
The initial idea was to combine Fragment and ViewPager,
Then, in a whim, The ListView is added to the first Fragment,
According to the suggestions on the internet, extends has ListFragment, followed by various errors.
After reading it carefully, it turns out to be MainActivity here:
1 // construct the adapter 2 List <Fragment> fragments = new ArrayList <Fragment> (); 3 fragments. add (new Fragment1 (); 4 fragments. add (new Fragment2 (); 5 fragments. add (new Fragment3 (); 6 FPAdapter adapter = new FPAdapter (getSupportFragmentManager (), fragments );
Because yes
List <Fragment>
Fragment1 will naturally report an error when using ListFragment.
Modify the code in Fragment1 and add the ListView as follows:
1 public class Fragment1 extends Fragment { 2 3 private ListView listView; 4 5 6 public View onCreateView(LayoutInflater inflater, ViewGroup container, 7 Bundle savedInstanceState) { 8 // TODO Auto-generated method stub 9 View view= inflater.inflate(R.layout.layout1, container, false);10 listView = (ListView)view.findViewById(R.id.lv);11 ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(),12 android.R.layout.simple_list_item_1,getData());13 listView.setAdapter(arrayAdapter);14 15 return view;16 }17 18 private List<String> getData(){19 List<String> data = new ArrayList<String>();20 for(int i = 0;i <20;i++) {21 data.add(i+"");22 }23 return data;24 }25 }
Where
Android. R. layout. simple_list_item_1
Is built-in and does not need to be defined.
In this way, the ListView will be displayed normally.