Learn to fragment UI fragment Manager
Create a fragment XML interface, and then in Java code, implement the Fragment life cycle:
public class Crimefragment extends fragment{
Private Crime Mcrime;
Private EditText Mtitlefieid;
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Mcrime=new Crime ();
}
The creation and configuration of fragment attempts is accomplished in this one fragment life cycle approach:
Public View Oncreateview (Layoutinflater inflater, ViewGroup container,bundle savedinstancestate)
This method instantiates the layout of the fragment view, and then returns the materialized view to the managed acrivity.
Layoutinflater and ViewGroup are necessary parameters for instantiating the layout, and bundles are used to store the recovered data
In Oncreateview (... ) Method: Fragment tries to make a direct call through the Layoutinflater,inflate (...) Method and generates the resource ID of the incoming layout.
The second parameter is the parent view of the view, which requires the parent view to properly configure the component.
The third parameter tells the layout builder whether to add the generated attempt to the parent view.
The listener method in fragment is set exactly the same as in activity. Creating an anonymous inner class that implements the Textwatcher listener interface
There are three ways to Textwatcher, and now you need to focus on the ontextchanged (..) Method. In its method, call the ToString method of Charsequence (on behalf of user input)
The method is used to return the string used to set the crime caption.
Mtitlefieid.addtextchangedlistener (New Textwatcher () {
@Override
public void beforetextchanged (Charsequence s, int. start, int count, int after) {
}
public void ontextchanged (charsequence s,int start,int before,int count) {
Mcrime.setmtitle (S.tostring ());
}
@Override
public void aftertextchanged (Editable s) {
}
The fragment transaction is used to add, remove, attach, detach, or replace fragment in the fragment queue.
is the key to using fragment to assemble and reload the user interface at run time. The Fragmentmanager manages the fallback stack for fragment transactions.
How the Android:layout_weight property works:
Tells LinearLayout how to arrange subassemblies, although the same values are set for the components, but does not mean that they occupy the same width on the screen. When determining the width of the subassembly view, the linear
Layouts use mixed values for the layout_width and Layout_weight parameters
2016/11/28
Library: Nineth Chapter using Recyclerview display list
Singleton is a special Java class, when creating a singleton, a singleton class allows only one instance to be created.
When Android removes an app from memory, the singleton is no longer there.
List<e> is a Java ordered array class that supports storing objects of the specified data type, with methods for acquiring, adding, and deleting elements in an array.
The common list implementations are 1.ArrayList LinkedList, voctor
Android is recommended to use the list interface type when declaring variables
The difference between ArrayList and LinkedList and Voctor:
1.ArrayList is the realization of the data structure based on dynamic array, LinkedList data structure based on linked list.
2. For random access get and set,arraylist feel better than LinkedList, because linkedlist to move the pointer.
3. Add and Remove,linedlist are the dominant for new and deleted operations because ArrayList is moving the data.
Vectors and ArrayList are almost identical, the only difference being that the vector is a synchronous class (synchronized).
Therefore, the cost is bigger than the ArrayList. Normally, most Java programmers use ArrayList instead of vectors,
Because synchronization can be controlled entirely by the programmer itself.
Recyclerview, Adapter, Viewholder
Recyclerview is a subclass of Viewgoup, and each view is a viewing object, Recyclerview itself does not create a view, creates a viewhodler, and Viewholder
Quoting a Itemview
Adapter is a Controller object
Role:
1. Create the necessary Viewhodler 2. Binding Viewhoder to model-level data
The role of Viewholder:
is a holder of the class, there is no method, only attributes, function as a retail storage, put the GetView method
Each return of the view saved up, can be used again, the advantages: do not have to go to the layout file every time to take the view, improve efficiency Fragment argument:fragment There are two ways to get the data in intent, one simple and direct, the other is Fragment Argument
Fragment argument:
Each fragment instance can have a bundle object attached to it. The bundle contains key-value pairs that we can use as if they were attached extra to the activity's intent. A key-value pair is a argument
To create the fragment argument, you first need to create the bundle object and then use the "put" method in the bundle to add the argument to the bundle
Additional argument to fragment:
Attaching the fragment bundle to fragment requires calling the Fragment.setargument (bundle) method, which must be completed before being added to the activity after the fragment is created:
1. Add a static method named Newinstance (), and for the fragment creation Argument,activity can pass in any required parameters to the Newinstanc () method
public static crimefragment newinstance (UUID crimeid) { bundle args=new bundle (); Args.putserializable (Arg_crime_id,crimeid); Crimefragment fragment=new crimefragment (); Fragment.setarguments (args); return fragment;
Viewpager:
Viewpager and Pageradapter, Pageradapter's subclass Fragmentstatepageradapter provides two methods:
GetCount () and getitem (int)
Fragmentstatepageradapter and Fragmentpageradapter:
Fragmentpageradapter is another available pageradapter its usage is basically the same as the Fragmentstatepageradapter method:
When you unload unwanted fragment, you use different processing methods:
Fragmentstatepageradapter will destroy unwanted fragment, and after the transaction commits, fragment in Fragmentmanager will be completely removed
State in the Fragmentstatepageradaoter class indicates that fragment bundle information can be saved in the Onsaveinstancestate (bundle) method when the fragment is destroyed
When you switch back, the saved instance state can be restored to a new fragment
Fragementpageradapter:
When an unwanted Fragment is destroyed, Fragment invokes the detach (Fragment) method of the transaction, not the Remove (Fragment) method,
Fragmentpageradapter just destroys the fragment view, fragment instances remain in Fragmentmanager
So the fragment created by Fragmentpageradapter will never be destroyed.
How the Viewpager works:
Pageradapter than adapter, because to handle more view management work,
Pageradapter does not use a onbindviewholder (...) that can be returned. Method
1.public Object Instantiateitem (viewgroup container,int position): tells Pager adapter to create a list item view at the specified location and then add it to the ViewGroup view container
2.public void Destroyitem (ViewGroup container,int position,object Object): tells Pager adapter to destroy a built view
3.piblic Abstract Boolean isviewfromobject (View view,object Object)
Instantiateitem (ViewGroup, Position) method is not created immediately fragment,pageradapter can decide when to create a view at its discretion
Invokes the Isviewfromobject (View,object) method object for the returned objects
public boolean isviewfromobject (View view,object Object) {
Return ((Fragment) object). GetView () ==view;
}
dialog box
There are three buttons in Android that can be used for dialog boxes:
Positive button, negative button, neutral button when the user clicks the Position button, accepts the dialog box to display the message, if the same dialog box has multiple buttons, the type of the button
and naming determine where they appear on the dialog box
To add dialogfragment to Fragmentmanager management and place it on the screen, you can invoke the fragment instance:
public void Show (Fragmentmanager manager,string tag)
public void Show (fragmenttransaction transaction,string tag)
When the created fragment are destroyed and rebuilt, the operating system also associates them, calling the fragment method to create the association:
public void Settargetfragment (Fragment fragment,int requestcode):
The method has two parameters: the target fragment and similar to the incoming Startactivityforresult (...). The request code for the method. When needed, the target fragment uses the request code to confirm which fragment is recovering the data
Activity.onactivityresult (..) The Activitymanager method is the parent activity method that is called after the child activity is destroyed, and the activity is automatically called when the data returned from the activity is processed.
. Onactivityresult (... ) method
1. Write apps that require a lot of input from users, and require more space to display input, and use Onactivityresult (...) When you want your app colleagues to support phones and tablet devices. method returns data to the target
Fragment is more convenient.
2. Mobile screen space is limited, so it is often necessary to use activity to host a full-screen fragment interface to display user input requirements, the child activity will be fragment by the parent activity to invoke Startactivityforresult (..) Method of
Mode to start. After the child activity is destroyed, the parent activity receives the Onactivityresult (... ) method, and forwards it to the fragment of the Promoter activity
Use the AppCompat library
to fully integrate the AppCompat library:
1. Add AppCompat dependencies
2. Use a AppCompat topic
3. Make sure all activity is appcompatactivity subclass
4. Let the activity class inherit the Appcompatactivity class
Theme setting: 1. Theme.appcompat: Black theme 2.theme.appcompat.light: Light theme 3.theme.appcompat.darkactionbar: Light theme with Black toolbar
The
Fragment.oncreateoptionmenu (Menu,menuinflater) method is called by Fragmentmanager. When the activity receives the operating system's Oncreateoptionmenu (..) Method callback request, we need to tell fragment
its managed fragment should receive Oncreateoptionsmenu (... ) method, call the following method:
public void Sethasoptionsmenu (Boolean hasmenu)
Click the menu item in the menu, Fragment receives a callback request for the onoptionsitemselected (MenuItem) method. The parameter passed in to the method is a MenuItem instance that describes the user's choice
public boolean onoptionsitemselected (MenuItem item) { switch (Item.getitemid ()) {case R.id.menu_item_new_ Crime: Crime crime=new Crime (); Crimelab.get (Getactivity ()). Addcrime (crime); Intent intent=crimepageractivity.newintent (Getactivity (), Crime.getmid ()); StartActivity (intent); return true; Case R.id.menu_item_show_subtitle: msubtitlevisible=!msubtitlevisible; Getactivity (). Invalidateoptionsmenu (); Updatesubtitle (); return true; If the menu item ID does not exist, the superclass version method is called default: return super.onoptionsitemselected (item);} }
Menus typically contain multiple menu items, and by examining the menu item ID, you can determine which menu item is selected, and then respond accordingly, which is typically the resource ID of the menu item resource that is given in the menu definition file
MenuItem Subtitleitem=menu.finditem (r.id.menu_item_show_subtitle); if (msubtitlevisible) { subtitleitem.settitle (r.string.hide_subtitle); } else { subtitleitem.settitle (r.string.show_subtitle); }
Problems with hierarchical navigation in Android: The target activity that navigates back to is completely rebuilt, since the parent activity is a new activity, the value of the instance variable and the saved instance state display will
Completely lost
There are two ways to guarantee the visible state of a child caption when navigating up:
if (savedinstancestate!=null) { Msubtitlevisible=savedinstancestate.getboolean (saved_subtitle_visible); }
public void Onsaveinstancestate (Bundle outstate) { super.onsaveinstancestate (outstate); Outstate.putboolean (saved_subtitle_visible,msubtitlevisible); }
1. Overlay up-navigation mechanism: The Finish method in crimepageractivity directly falls back to an activity interface, but this can only be returned to one level. But in the actual development of the application needs multi-level navigation
2. When the crimepageractivity is started, the status of the header is passed to it as extra information, and the Getparentactivityintent () method is covered in crimepageractivity with additional extra
Intent reconstruction of Information crimelistactivity
Android Basics (iii)