Android five days Music (third day) Listfragment and Viewpager

Source: Internet
Author: User

1ListFragment

Today, we first learned a very often used display scenario: List display.

Yesterday, I learned to use Fragmet to replace activity for design. Today, based on hosting a single fragment, master a layout list. First look at the effect:

Because the fragment list needs to be saved using ArrayList. In order for the fragment object to be affected by the life cycle such as acrivity, the following Singleton class is created:

public class Crimelab {private arraylist<crime> mcrimes;    private static Crimelab Scrimelab;    Private Context Mappcontext;        Private Crimelab (Context context) {Mappcontext=context;        Mcrimes=new arraylist<crime> ();            for (int i=0;i<100;i++) {Crime C =new Crime ();            C.settitle ("Crime #" +i);            C.setsolved (i%2==0);        Mcrimes.add (c); }} public static Crimelab get (Context c) {if (scrimelab==null) {scrimelab=new Crimelab (c.getapplic        Ationcontext ());    } return Scrimelab;    } public arraylist<crime> Getmcrimes () {return mcrimes; Crime getcrime (UUID ID) {for (Crime c:mcrimes) {if (C.getid (). Equals (ID)) {RE            Turn C;    }} return null; }}

At the same time, the corresponding fragment layout files and class files are also created. In fact, there are classes that specifically support list fragment: Listfragment. Inheriting this class is possible. You can then use its built-in ListAdapter.

Why use adapter? Since we've created 100 new fragment objects in our Fragmentlab, it's not possible to show them all in one page. Instead, the object is created only when it needs to be displayed.

Adapter is getting data from the model layer. And make it available to the ListView display Bridge.

Private class Crimeadapter extends arrayadapter<crime>{public crimeadapter (arraylist<crime> CR                IMEs) {super (Getactivity (), 0,crimes);                    } @Override public View getView (int position,view convertview,viewgroup parent) { if (convertview==null) {convertview=getactivity (). Getlayoutinflater (). Inflate (R.layout.list_                    Item_crime,null);                    } Crime C=getitem (position);                    TextView titletextview= (TextView) Convertview.findviewbyid (R.id.crime_list_item_titletextview);                    Titletextview.settext (C.gettitle ());                    TextView datetextview= (TextView) Convertview.findviewbyid (R.id.crime_list_item_datetextview);                    Datetextview.settext (C.getdate (). toString ());               CheckBox solvedcheckbox= (checkbox) Convertview.findviewbyid (R.id.crime_list_item_solvedcheckbox);     Solvedcheckbox.setchecked (c.issolved ());        return convertview; }    }

Implement your own custom Adapater code. The reason to implement your own adapter is that we have customized our layouts for each entry in the list.

such as the list_item_crime in the code.

This gives the list the fragment that is displayed.

2ViewPager

Viewpager enables the ability to toggle the left and right side of the screen to view different list items.

Viewpager need to use adapter to provide a view.

Through the Pageradapter subclass: Fragmentstatepageradapter to deal with the problem of coordination between the two.

Two methods need to be implemented here, GetCount () and GetItem ().

The code is scaled as follows:

Mviewpager.setadapter (new FRAGMENTSTATEPAGERADAPTER (FM) {            @Override public            int GetCount () {                return Mcrimes.size ();            }            @Override public            Fragment getItem (int pos) {                Crime crime=mcrimes.get (POS);                Return Crimefragment.newinstance (Crime.getid ());            }        );




3 Fragment Transfer data

Similar to activity. Data can also be transferred between fragment. And the transfer of data at the fragment level will make programming more flexible.

Imagine, for example, the following scenario: In Crimefragment, you need to press the key to call up Datepickerfragment, which is initialized with the data provided by the former. At the same time, the return value of Datepickerfragment is also used for crimefragment.

There are a few things you need to do, such as the following steps:

1) when initializing datepickerfragment from Crimefragment, the data is passed as a construction parameter

2) When the datepickerfragment is constructed. Save the passed-in value to argument

3) Datepickerfragment when rendering, take arguments median

4) Datepickerfragment pass value back to Crimefragment

In order to implement the above procedure, we first write the Newinstance method in Datepickerfragment, the method can be called and accept the parameters when the datepickerfragment is instantiated. At the same time, prepare the data before fragment create.

public static datepickerfragment newinstance (date date) {        bundle args=new bundle ();        Args.putserializable (extra_date,date);        Datepickerfragment fragment=new datepickerfragment ();        Fragment.setarguments (args);        return fragment;    }


When the data is returned, the Onactivityresult method is overwritten.

Interactive processes such as:

Android five days Music (third day) Listfragment and Viewpager

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.