Introduction to Android Development (13) Special Fragment 13.1 listfragment

Source: Internet
Author: User
Tags xmlns

Listframgent is a fragment containing ListView that displays a range of information through a data source (array or cursor). Listfragment is very useful, like RSS, may display a list on the left, the right to display the selected list of the corresponding content.

You can create a Listfragment object by inheriting listfragment. The following shows how to use Listfragment.

1. Create a project: Listfragmentexample.

2. Code in the Main.xml.

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=     
"http://schemas.android.com/apk/" Res/android " 
    android:layout_width=" fill_parent " 
    android:layout_height=" fill_parent " 
    android:o" rientation= "Horizontal" >     
         
<fragment  
    android:name= "Net.manoel.ListFragmentExample.Fragment1" 
    Android:id= "@+id/fragment1" 
    android:layout_weight= "0.5" 
    android:layout_width= "0DP" 
    android:layout _height= "200DP"/>     
         
<fragment  
    android:name= "Net.manoel.ListFragmentExample.Fragment1" 
    Android:id= "@+id/fragment2" 
    android:layout_weight= "0.5" 
    android:layout_width= "0DP" 
    android: layout_height= "300DP"/>     
         
</LinearLayout>

3. Under Re/layout, create a new file: Fragment1.xml.

?

XML version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=     
"http://schemas.android.com/apk/res/" Android " 
    android:orientation=" vertical " 
    android:layout_width=" fill_parent " 
    android:layout_height=" Fill_parent ">     
             
    <listview  
        android:id=" "@id/android:list" 
        android:layout_width= "Match_parent" 
        android:layout_height= "match_parent"                
        android:layout_weight= "1" 
        android:drawselectorontop= false "/>     
                                        
</LinearLayout>

4, under the package path to create a new class: Fragment1.java.

public class Fragment1 extends Listfragment {string[] presidents = {"Dwight D. Eisenhower",     
        "John F. Kennedy", "Lyndon B Johnson", "Richard Nixon", "Gerald Ford",     
        "Jimmy Carter," "Ronald Reagan," "George H. W. Bush", "Bill Clinton,"     
         
    "George W. Bush", "Barack Obama"};  @Override public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate)     
    {return inflater.inflate (R.layout.fragment1, container, false); @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedins     
        Tancestate); Setlistadapter (New arrayadapter<string> (getactivity), Android.     
    R.layout.simple_list_item_1, Presidents)); } public void OnlistitemClick (ListView parent, View v, int position, long id) {Toast.maketext (getactiv     
    ity (), "You have selected" + presidents[position], toast.length_short). Show (); }       
         
}

Related Article

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.