SoFunction
Updated on 2025-03-01

Do not use ListFragment to add ListView

The starting idea is to combine Fragment and ViewPager,

Then I suddenly had a whim and added a ListView to the first Fragment.

According to the suggestions online, I extended ListFragment and then reported various errors.

After a closer look, it turned out to be MainActivity here:

Copy the codeThe code is as follows:

//Construct the adapter
   List<Fragment> fragments=new ArrayList<Fragment>();
  (new Fragment ());
  (new Fragment ());
  (new Fragment ());
  FPAdapter adapter = new FPAdapter(getSupportFragmentManager(), fragments);

Because it's

List<Fragment>

Fragment1 will naturally report an error when using ListFragment1.

Modify the code in Fragment1 and add the ListView as follows:

public class Fragment extends Fragment {
    private ListView listView;
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      View view= ( , container, false);
      listView = (ListView)();
      ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(),
          .simple_list_item_ ,getData());
      (arrayAdapter);
      return view;
    }
    private List<String> getData(){
      List<String> data = new ArrayList<String>();
      for(int i = ;i < ;i++) {
        (i+"");
      }
      return data;
    }
  }

 
in

.simple_list_item_1

It comes with it, no definition.

This way the ListView can be displayed normally.

The above is all about this article, I hope you like it.