#Android Web and Data Storage

Chapter 1 Learning


#### Creation of a boot page ####

Summary:

This time to make the App boot page, mainly use two knowledge “SharedPreferences and ViewPager”

Genymotion’s Android emulator, version 5.1.0 is the default root, you can use RootExplorer4 software operating system directory, other including the SDK official emulator… Go to bed… This is so annoying, not the next Genymotion, blood lesson


#####1.SharedPreferences#####

SharedPreferences SharedPreferences is an Android class for storing data permanently on a mobile phone. The class uses Key and Value pairs to save data and generate XML files.

In order to use SharedPreferences, the following steps are required

** Get the SharedPreferences object. Android provides three main methods to get this object, as shown in the code below

Public Abstract SharedPreferences getSharedPreferences(String name,int mode);  Public SharedPreferences getPreferences(int mode) {public SharedPreferences getPreferences(int mode) {returngetSharedPreferences(getLocalClassName(), mode); } / / 3. The PreferenceManager getDefaultSharedPreferences in class () method of the public static SharedPreferences getDefaultSharedPreferences(Context context) {return context.getSharedPreferences(
getDefaultSharedPreferencesName(context)
,getDefaultSharedPreferencesMode());
    }
Copy the code

After all, all three methods are the getSharedPreferences() method in the calling Context class, which passes in two parameters:

  • The first parameter specifies the name of the SharedPreferences file, or one will be created if the specified document does not exist. SharedPreferences files are stored in /data/data/”package name”/shared_prefs/.
  • The second parameter specifies the mode of operation. Use MODE_PRIVATE. All other methods are out of date.

** Step 2: ** Get the SharedPreferences.Editor object and write it.

SharedPreferences.Editor editor = sharedPreferences.edit(); // Get Editor editor.putBoolean(SharedPreferences instance"isRead".true); Editor.mit (); // Add data, provide various put methods corresponding to different types of data, key value pair input editor.mit (); Call commit() to commit the added data immediately, but AS recommends using apply(). See the link belowCopy the code

http://blog.csdn.net/s04103037/article/details/40372659

Step 3: Read from the SharedPreferences object. After using the method in the first step, you can directly use the various GET methods to get the corresponding type of value.

SharedPreferences pref = getSharedPreferences("data",MODE_PRIVATE);
String name = pref.getString("name"."");
int age = pref.getInt("age", 0);
boolean married = pref.getBoolean("married".false);
Copy the code

The first argument in the GET method is the key name, and the second is the default value returned if there is no such key-value pair. This takes the data from the file saved in SharedPreferences, perpetuating simple data.


#####2.ViewPager##### Draw a picture to intuitively understand the structure sequence

Here’s the code. Look at the picture above

activity_start.xml

<?xml version="1.0" encoding="utf-8"? >
<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary">

</android.support.v4.view.ViewPager>
Copy the code

StartActivity.java

public class StartActivity extends FragmentActivity {
    public static final int PAGE_NUM = 2;
    private FragmentPagerAdapter mAdapter;
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.i(TAG, "onCreate: ");

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);

        mViewPager = (ViewPager) findViewById(R.id.pager);
        // instantiate the ViewPager with the layout
        mViewPager.setPageTransformer(true.new ZoomOutPageTransformer());
        // add animation effect
        mAdapter = new StartPagerAdapter(getSupportFragmentManager());
        //③ Instantiate FragmentPagerAdapter by subclass
        mViewPager.setAdapter(mAdapter);
        / / (4) binding Adapter}}Copy the code

ZoomOutPageTransformer is a custom animation class, this part is also very free expression, do not write examples, omit.

StartPagerAdapter.java

public class StartPagerAdapter extends FragmentPagerAdapter {

    String TAG = getClass().getSimpleName();

    public StartPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public int getCount(a) {
        return StartActivity.PAGE_NUM;
    }

    @Override
    public Fragment getItem(int position) {
        Log.i(TAG, "getItem: " + position);
        if (position == 0) {
            return new PageOneFragment();
        } else {
            return newPageTwoFragment(); }}}Copy the code

We all know how to do Fragment, omission, I made two, called PageOneFragment and PageTwoFragment. A custom inherited from FragmentPagerAdapter Adaper class used to manage fragments, another FragmentStatePagerAdapter is also used in fragments, but different from the former effect, difference is as follows:

  • FragmentPageAdapter: Like the PagerAdapter, it will cache only the current Fragment and one Fragment to the left and one Fragment to the right. If there are 1, 2, 3, and 4 fragments, the Fragment will cache only three fragments. Cache 1, 2, and 3 on 3 pages: destroy 1 page, cache 2, 3, and 4 on 4 pages: destroy 2 pages, cache 3, 4 more pages, and so on ~

  • FragmentStatePagerAdapter: when the user does not necessarily from fragments, the fragments will be destroyed, will only save fragments of state! When the page needs to be displayed again, a new page is generated!

Ok, a new custom class has been created and two important methods must be overridden:

  1. GetCount () : Gets how many views there are in the viewPager. I wrote 2 here
  2. GetItem (int position) : Determines which Fragment to return based on position. Position appears as integer numbers, such as 0,1,2,3.

That’s basically it. Look at the effect

– the –