1ListFragment
Today, we first learned a very common display scenario: a list display.
Yesterday I learned to use Fragmet instead of activity to design, today on the basis of hosting a single fragment, master a layout list. First look at the effect:
Because the fragment list needs to be saved using ArrayList, and in order for the fragment object to be affected by the acrivity and other life cycle, create the following singleton class:
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, to establish the corresponding fragment layout files and class files, in fact, there is a special support list fragment class: Listfragment. Inherit this class, and then use its built-in ListAdapter.
Why use adapter? Because we have created 100 new fragment objects in our Fragmentlab, we cannot display them all in one page, but we need to display them to create the object. Adapter is to get the data from the model layer and provide it to the bridge shown by the ListView.
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; } }
Implementation of their own custom adapater code, the reason to achieve their own adapter, because in the list of each item we customized their own layout. such as the list_item_crime in the code.
This gives the list the fragment that is displayed.
2ViewPager
The Viewpager can be used to toggle the function of viewing different list items by sliding the screen left and right.
Viewpager needs to be able to provide views with the help of adapter. 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 examples are 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 data transfer
Similar to activity, data transmission can also be carried out between fragment. And the fragment level of data transfer will make programming more flexible.
Consider the following scenario: In Crimefragment, you need to press the key to call up Datepickerfragment, which initializes the data that is provided by the former. Also, the return value of datepickerfragment is used for crimefragment.
Here are a few things to do:
1) when initializing datepickerfragment from Crimefragment, the data is passed as a construction parameter
2) When Datepickerfragment 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 achieve the above process, the first thing to write the Newinstance method in Datepickerfragment, the method can be called when the datepickerfragment is instantiated and accept parameters, while the fragment create the data before the
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