package com.gracecode.android.gojuon.adapter;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import com.gracecode.android.gojuon.Characters;
import com.gracecode.android.gojuon.R;
import com.gracecode.android.gojuon.ui.fragment.CharactersFragment;
import com.viewpagerindicator.IconPagerAdapter;

import java.util.HashMap;

public class CharactersFragmentAdapter extends FragmentPagerAdapter implements IconPagerAdapter {
    protected static final int[] TITLES = new int[]{
            R.string.monographs,
            R.string.monographs_with_diacritics,
            R.string.digraphs,
            R.string.digraphs_with_diacritics
    };
    protected static final HashMap<Integer, CharactersFragment> mFragments =
            new HashMap<Integer, CharactersFragment>();

    private final Context mContext;

    public CharactersFragmentAdapter(Context context, FragmentManager fm) {
        super(fm);
        this.mContext = context;
    }

    @Override
    public Fragment getItem(int position) {
        if (mFragments.containsKey(position)) {
            return mFragments.get(position);
        }

        CharactersFragment fragment;
        switch (TITLES[position]) {
            case R.string.digraphs:
                fragment = new CharactersFragment(Characters.DIGRAPHS, 3);
                break;

            case R.string.monographs_with_diacritics:
                fragment = new CharactersFragment(Characters.MONOGRAPHS_WITH_DIACRITICS);
                break;

            case R.string.digraphs_with_diacritics:
                fragment = new CharactersFragment(Characters.DIGRAPHS_WITH_DIACRITICS, 3);
                break;

            default:
                fragment = new CharactersFragment(Characters.MONOGRAPHS);
        }

        mFragments.put(position, fragment);
        return fragment;
    }

    @Override
    public int getCount() {
        return TITLES.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mContext.getString(TITLES[position]);
    }

    @Override
    public int getIconResId(int index) {
        return android.R.color.transparent;
    }

    public void startSlide(int item) {

        CharactersFragment fragment = mFragments.get(item);
        fragment.startSlide();
    }
}

@@ -72,16 +72,58 @@
 ontext;%0A
+import android.content.SharedPreferences;%0A
 import a
@@ -340,16 +340,67 @@
 juon.R;%0A
+import com.gracecode.android.gojuon.common.Gojuon;%0A
 import c
@@ -1021,16 +1021,72 @@
 Context;
+%0A    private final SharedPreferences mSharedPreferences;
 %0A%0A    pu
@@ -1203,24 +1203,103 @@
  = context;%0A
+        this.mSharedPreferences = Gojuon.getInstance().getSharedPreferences();%0A
     %7D%0A%0A    @
@@ -1343,32 +1343,76 @@
 int position) %7B%0A
+        final CharactersFragment fragment;%0A%0A
         if (mFra
@@ -1455,22 +1455,26 @@
         
-return
+fragment =
  mFragme
@@ -1505,47 +1505,20 @@
    %7D
-%0A%0A        CharactersFragment fragment;%0A
+ else %7B%0A    
     
@@ -1553,32 +1553,36 @@
 ) %7B%0A            
+    
 case R.string.di
@@ -1597,32 +1597,36 @@
                 
+   
+ 
 fragment = new C
@@ -1676,32 +1676,36 @@
                 
+    
 break;%0A%0A        
@@ -1700,32 +1700,36 @@
 k;%0A%0A            
+  
+  
 case R.string.mo
@@ -1746,32 +1746,36 @@
 ith_diacritics:%0A
+    
                 
@@ -1860,32 +1860,36 @@
             
+ 
+   
 break;%0A%0A
             
@@ -1868,32 +1868,36 @@
         break;%0A%0A
+    
             case
@@ -1924,32 +1924,36 @@
 ith_diacritics:%0A
+    
                 
@@ -2039,32 +2039,36 @@
             
+   
+ 
 break;%0A%0A
             
@@ -2047,32 +2047,36 @@
         break;%0A%0A
+    
             defa
@@ -2080,16 +2080,20 @@
 efault:%0A
+    
         
@@ -2162,24 +2162,28 @@
 S);%0A        
+    
 %7D%0A%0A        m
@@ -2181,16 +2181,20 @@
         
+ 
+   
 mFragmen
@@ -2221,16 +2221,27 @@
 gment);%0A
+        %7D%0A%0A
         
@@ -2256,32 +2256,33 @@
 ragment;%0A    %7D%0A%0A
+%0A
     @Override%0A  
@@ -2573,24 +2573,495 @@
 ent;%0A    %7D%0A%0A
+    public CharactersFragment getFragmentByTitle(int monographs) %7B%0A        int position = getTitlePosition(monographs);%0A        if (position != -1) %7B%0A            return (CharactersFragment) getItem(position);%0A        %7D%0A%0A        return null;%0A    %7D%0A%0A    private int getTitlePosition(int monographs) %7B%0A        for (int i = 0; i %3C TITLES.length; i++) %7B%0A            if (monographs == TITLES%5Bi%5D) %7B%0A                return i;%0A            %7D%0A        %7D%0A%0A        return -1;%0A    %7D%0A%0A//
     public v
@@ -3087,17 +3087,18 @@
 item) %7B%0A
-%0A
+//
         
@@ -3145,24 +3145,26 @@
 .get(item);%0A
+//
         frag
@@ -3178,20 +3178,22 @@
 artSlide();%0A
+//
     %7D%0A%7D%0A

重构 Adapter