Add a ListView in fragment without using listfragment

Source: Internet
Author: User

The original idea was to combine fragment and Viewpager,

Then the whim, adding a ListView to the first fragment,

According to the suggestions on the Internet, extends Listfragment, followed by various error.

Carefully looked under, turned out to be mainactivity here:

1//Construction 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 it is

List<fragment>

Fragment1 with Listfragment naturally will error.

To modify the code in the Fragment1, add the ListView in the following way:

1 public class Fragment1 extends Fragment {2  3     private listview ListView; 4  5  6 public     View Oncreatev Iew (Layoutinflater inflater, ViewGroup container, 7                              Bundle savedinstancestate) {8         //TODO auto-generated method Stub 9         View view= inflater.inflate (R.LAYOUT.LAYOUT1, container, false);         ListView = (ListView) View.findviewbyid (r.id.lv);         arrayadapter<string> arrayadapter = new Arrayadapter<string> ( Getactivity (),                 Android. R.layout.simple_list_item_1,getdata ());         Listview.setadapter (Arrayadapter);         return view;16     }17     Private list<string> getData () {         list<string> data = new arraylist<string> (); 20 for         (int i = 0;i <20;i++) {             data.add (i+ "");         }23         return data;24}25     }

which

Android. R.layout.simple_list_item_1

is self-bringing, not defined.

This allows the ListView to display normally.

Add a ListView in fragment without using listfragment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.