"The authoritative Guide to Android Programming"-Reading notes (11) Perfecting Criminalintent

Source: Internet
Author: User
Tags naming convention uuid

"The authoritative Guide to Android Programming"-Reading notes (11) Perfecting Criminalintent

In the previous article, we followed the author of the book, using a single fragment for the simplest insert operation. This article will follow the author to further refine the various functions.

Objectives of this chapter:

    • Using the Listfragment display list
    • Passing data between fragment
    • Use Viewpager to achieve screen display
    • dialog box

Using the Listfragment display list

If it's a challenge, or if I don't have a UI, I'll give you a sketch. or hand-drawn or axure prototypes. Every example of the book gives the final UI, so this step is basically saved. This chapter uses listfragment in the example Criminalintent. To achieve the following effect:

Resource-related

All the data in this UI is displayed based on Dynamic data, because no image resource or string resource is used. So there is no modification to the place. (The current resource defaults to the button text, the text appears on the interface, etc.)

Data related

Now you need to display a string of data, and a new ArrayList class that can hold multiple crime objects is added to the book. It defines 2 private variables.

private static Crimelab Scrimelab;

Private Context Mappcontext;

The variable that starts with S is the naming convention for Development. It represents the variable Scrimelab is a static variable.

The code for this class is as follows:

http://git.oschina.net/canglin/CriminalIntent/commit/2f5580a992c804949a14a921dae7535a6097532f

This class defines a get method for passing in a context. In line 18, the constructor is called directly by Scrimelab, and the constructor parameter is Getapplicationcontext (), because the context may be an activity or another context object, such as a service. There is no guarantee that as long as the crimelab needs to be used, the context,context will exist for the entire life of the application. So we use Getapplicationcontext (). As for the 12-line public constructor I am wrong, I will fix it in the next version of the GIT image.

Save some crime objects to the Crimelab. Add a crime list of ArrayList and the Getter () method.

Then add a query operation in Crimelab for Getcrime (UUID ID). I prefer to use the Getcrimebyid () method.

The most clean version of Crimelab after completion does not contain the analog data code as follows:

http://git.oschina.net/canglin/CriminalIntent/commit/e4a9e6fac36f7eca76e063b406e194338f3fa778

Logical correlation

is the overall planning and design of criminalintent application

This app displays the list in the container view. We're going to create a listfragment and an activity, and a layout that matches the listfragment.

Fragment

Create the Crimelistfragment class extension from listfragment. The honeycomb system version introduces the Listfragment class, and the support library also introduces the class.

Import android.support.v4.app.ListFragment;

Listfragment is to show the list item to the user through the ListView. The ListView uses adapter to request the View object.

Adapter is responsible for:

Create the necessary view objects;

Populate the View object with the model layer data;

Returns the prepared view object to the ListView.

The random example uses Setlistadapter (ListAdapter) to set adapter for the Crimelistfragment management built-in ListView. (See the following code link for details, and now you can open it for comparison)

Fragmentactivity

Since each activityfragment has similar code, the author creates a Singlefragmentactivity abstract class to reduce future code input. The example in the book is to add a fragment dynamically in a fragmentcontainer, so the only different code is the code that was created dynamically before the thing was added fragment.

Modify the crimeactivity extension from singlefragmentactivity.

Create crimelistactivity extension from singleframentactivity.

Because the only difference between these 2 classes

are just 23 rows. With an abstract approach, each class that extends from singlefragmentactivity must be @override out of createfragment ().

View-related

The following are required in Res/layout/list_item_crime.xml:

The code is as follows:

http://git.oschina.net/canglin/CriminalIntent/commit/05a1da49e017dbdcef2d795d6da07eca41a3a006

In this chapter, focus only on the following class and layout resource files. Other deletions are OK and will not affect the normal operation of the app.

Object

Crime objects for list child elements

Crimelab can create and get a list of crime.

Activity

Crimelistactivity expanding from singlefragmentactivity creating crimelistfragment things

Singlefragmentactivity

Fragment

Crimelistfragment generates the corresponding view according to the List_item_crime.xml

Res/layout

Activity_fragment.xml defines the easy view

List_item_crime.xml defines a view of the list child elements

Because the default activity is not crimelistactivity

<activity android:name= ". Crimelistactivity ">

<intent-filter>

<action android:name= "Android.intent.action.MAIN"/>

<category android:name= "Android.intent.category.LAUNCHER"/>

</intent-filter>

</activity>

The default will be crimelistactivity after this is set.

At the beginning of the example is very casual, to prepare for the following several goals, I will make some adjustments to the source code, the following code:

http://git.oschina.net/canglin/CriminalIntent/commit/05a1da49e017dbdcef2d795d6da07eca41a3a006

Using Fragment Argment

We have learned that in activity it is called startactivity (Intent) to start another activity. StartActivity (Intent) is now invoked in fragment to start the activity.

The detailed information will be displayed when you click Listfragment. The details interface should be completed in the 8th chapter.

When the interface is set up, the string for the resource file initially looks like this. Because several strings have been added, but not yet strings.xml, the effect is as follows:

After completing the resource file

The code is as follows:

Http://git.oschina.net/canglin/CriminalIntent/commit/918aed3dd490b12f39f00701e4806cada3ad0b63

So far the interface has been basically shaped, with all the events in it, the data processing, and the business logic I've reduced to the lowest level. There are many places in the middle of the book that require some non-UI code to be added, and I'm not. As for the data of the list, it is just to make the interface logic complete.

Populating crimefragment with Data

Use the previously learned method to pass data with intent, and read the data from intent in Crimefragment's OnCreate ().

First define an ID

public static final String extra_crime_id =

"COM.EXAMPLE.LIJING.CRIMINALINTENT.CRIME_ID";

.....

Then in the OnCreate ()

UUID Crimeid = (UUID) getactivity (). Getintent (). Getserializableextra (extra_crime_id);

Mcrime = Crimelab.get (Getactivity ()). Getcrime (Crimeid);

Data in Crimelistfragment startactivity ()

PutExtra (Crimefragment.extra_crime_id,c.getid ());

After the completion of the interface is like this:

The code changes as follows:

http://git.oschina.net/canglin/CriminalIntent/commit/657c3d4164e08968dca123c3babec0ee80f97327

Improvements to transfer data between interfaces

The original code is as follows

UUID Crimeid = (UUID) getactivity (). Getintent (). Getserializableextra (extra_crime_id);

In this code, Crimeid is stored in the crimeactivity. Now change it from extra in crimeactivity intent to arguments bundle. Each fragment instance can have a bundle object attached to it. The bundle contains key-value pairs, and we can use them as if they were attached extra to the activity's intent. A Key-value pair is a argument.

UUID Crimeid = (UUID) getarguments (). getserializable (extra_crime_id);

and give fragment a newinstance () to create yourself, and create arguments in this static method.

In general, the parameters obtained from the activity previously were changed to get the parameters from their own storage area. The parameters in your own storage area are written in the fragment that you created.

This is the code change:

http://git.oschina.net/canglin/CriminalIntent/commit/95d7da1d76c2ac316cdb1f1af823df3cb745e133

Use Viewpager to achieve screen display

To achieve this, we need to create a Viewpager activity that is named Crimepageractivity to replace the crimeactivity. This chapter takes a code-based approach to creating a view that contains the following steps:

    • Create a resource ID for Viewpager;
    • Create Viewpager instances and assign values to Mviewpager;
    • Assigns the resource ID to the Viewpager, and configures it;
    • Sets the content view for the activity Viewpager.

Create a stand-alone resource ID (res/values/ids.xml)

Defining a standalone resource ID is no different from defining a string resource ID: Create a project element in an XML file in the Res/values directory. Create an Android XML resource file named Res/values/ids.xml.

Create a content view in code (CRIMEPAGERACTIVITY.JAVA)

Mviewpager = new Viewpager (this);

Mviewpager.setid (R.id.viewpager);

Setcontentview (Mviewpager);

Then we use Fragmentstatepageradaper as our agent, responsible for managing conversations with Viewpager and working together.

The code is as follows:

Http://git.oschina.net/canglin/CriminalIntent/commits/master

Here, drag is implemented in the details interface.

Now the program has a bug is that when I click on a detailed information, the details of the interface will always show the first one. Now we are selecting the option by setting Setcurrnetitem (index) to be the current detailed page information.

About Viewpager.onpagechangelistener

In the example in this book, the title is set to Crimepageractivity when the page changes. In my code I seem to have removed the location of the title.

https://developer.android.com/reference/android/support/v4/view/ViewPager.OnPageChangeListener.html# onpagescrollstatechanged (int)

This is the official API address.

This method must rewrite 3 abstract methods, if you do not know what to write now can be 3 methods to copy in it.

Mviewpager.setonpagechangelistener (New Viewpager.onpagechangelistener () {

public void onpagescrollstatechanged (int state) {}

public void onpagescrolled (int position, float positionoffset, int positionoffsetpixels) {}

public void onpageselected (int position) {

Crime Crime = mcrimes.get (position);

if (crime.gettitle () = null) {

Settitle (Crime.gettitle ());

}

}

});

Like this, but this code in the current program, there is no interface changes. So here's a special emphasis.

The following code is completed:

Http://git.oschina.net/canglin/CriminalIntent/commits/master

dialog box

There is a time setting button in our crimefragment corresponding detail interface. Now let's refine the dialog box for this button as requested in the book.

The author uses the method of encapsulating Alertdialog in dialogfragment to display the dialog box because of the following advantages:

    • Using the Fragmentmanager Management dialog box, you can use more configuration options to display the dialog box;
    • Alertdialog that are encapsulated in fragment when rotation occurs do not disappear

When Dialogfragment is displayed on the screen, the Fragmentmanager that hosts the activity calls Oncreatedialog (). In Oncreatedialog we need to return a alertdialog.builder.

Be careful when displaying the dialog box. To add dialogfragment to Fragmentmanager management and place it on the screen, you can call the fragment Show method.

public void Show (Fragmentmanager Manager, String tag)

public void Show (Fragmenttransactiong transaction, String tag)

The string parameter is the dialogfragment used in the queue. In the choice of Fragmentmanager and Fragmenttransaction, the book has chosen Fragmentmanager because the parameter is passed in, things can automatically create commits.

The simplest interface effect is as follows:

The current code is as follows:

Http://git.oschina.net/canglin/CriminalIntent/commit/ddf4b0365937ea287d0f64f7b437c1822f8745b0

The data interaction between dialogs has been briefly described in this chapter. The book is here to finish. In line with the simplest code principles to achieve the goal, the code is not committed to git first.

At the end of each small paragraph there will be a git code address for the current stage.

Summary:

So far, basically some of the most basic Android interface, the most basic business logic, the data logic has been completed. Each stage of the code is minimized to minimize, in order to facilitate viewing in git the process of modification, to avoid unnecessary misleading.

"The authoritative Guide to Android Programming"-Reading notes (11) Perfecting Criminalintent

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.