diff options
author | laco0416 <laco0416@gmail.com> | 2014-03-25 13:26:27 +0900 |
---|---|---|
committer | laco0416 <laco0416@gmail.com> | 2014-03-25 13:26:27 +0900 |
commit | ce45d028944137e281f2583f4b13b2644ce24928 (patch) | |
tree | 05d1d151332855858f589c22ad69379756093594 | |
parent | b021c940b779ec0aa46779ad2b13ba8443221c5e (diff) | |
download | SmileEssence-ce45d028944137e281f2583f4b13b2644ce24928.tar.gz |
Implement layout system with ActionBar and ViewPager.
5 files changed, 250 insertions, 11 deletions
diff --git a/SmileEssenceTest/src/net/lacolaco/smileessence/activity/MainActivityTest.java b/SmileEssenceTest/src/net/lacolaco/smileessence/activity/MainActivityTest.java index ebec2270..908c0250 100644 --- a/SmileEssenceTest/src/net/lacolaco/smileessence/activity/MainActivityTest.java +++ b/SmileEssenceTest/src/net/lacolaco/smileessence/activity/MainActivityTest.java @@ -94,4 +94,14 @@ public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActiv { assertNotNull(getActivity().getActionBar()); } + + public void testGetViewPager() throws Exception + { + assertNotNull(getActivity().getViewPager()); + } + + public void testGetPagerAdapter() throws Exception + { + assertNotNull(getActivity().getPagerAdapter()); + } } diff --git a/res/layout/main.xml b/res/layout/main.xml index d955476f..8376458f 100644 --- a/res/layout/main.xml +++ b/res/layout/main.xml @@ -23,16 +23,13 @@ ~ SOFTWARE. --> -<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" - android:orientation="vertical" - android:layout_width="fill_parent" - android:layout_height="fill_parent" - > +<android.support.v4.view.ViewPager + xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:tools="http://schemas.android.com/tools" + android:id="@+id/viewPager" + android:orientation="vertical" + android:layout_width="fill_parent" + android:layout_height="fill_parent" + tools:context=".MainActivity"/> - <TextView - android:layout_width="fill_parent" - android:layout_height="wrap_content" - android:text="Hello World, MainActivity" - /> -</LinearLayout> diff --git a/src/net/lacolaco/smileessence/activity/MainActivity.java b/src/net/lacolaco/smileessence/activity/MainActivity.java index cbc7fdaa..e2412974 100644 --- a/src/net/lacolaco/smileessence/activity/MainActivity.java +++ b/src/net/lacolaco/smileessence/activity/MainActivity.java @@ -24,22 +24,30 @@ package net.lacolaco.smileessence.activity; +import android.app.ActionBar; import android.app.Activity; import android.content.Intent; import android.os.Bundle; +import android.support.v4.view.PagerAdapter; +import android.support.v4.view.ViewPager; import net.lacolaco.smileessence.R; import net.lacolaco.smileessence.preference.PreferenceHelper; import net.lacolaco.smileessence.property.PropertyHelper; import net.lacolaco.smileessence.resource.ResourceHelper; +import net.lacolaco.smileessence.view.adapter.TextFragment; +import net.lacolaco.smileessence.viewmodel.PageListAdapter; import java.io.IOException; public class MainActivity extends Activity { + private static final String STATE_PAGE = "page"; private ResourceHelper resourceHelper; private PreferenceHelper preferenceHelper; private PropertyHelper propertyHelper; + private ViewPager viewPager; + private PageListAdapter pagerAdapter; /** * Called when the activity is first created. @@ -49,6 +57,8 @@ public class MainActivity extends Activity { super.onCreate(savedInstanceState); setContentView(R.layout.main); + viewPager = (ViewPager) findViewById(R.id.viewPager); + initializeViewPager(savedInstanceState); try { setupHelpers(); @@ -60,6 +70,30 @@ public class MainActivity extends Activity } } + private void initializeViewPager(Bundle savedInstanceState) + { + ActionBar bar = getActionBar(); + bar.setTitle(null); + bar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); + pagerAdapter = new PageListAdapter(this, viewPager); + Bundle args = new Bundle(); + args.putString(TextFragment.ARG_TEXT, "test"); + pagerAdapter.addTab("Passion", TextFragment.class, args); + pagerAdapter.addTab("Star", TextFragment.class, args); + pagerAdapter.addTab("Stream", TextFragment.class, args); + if(savedInstanceState != null) + { + bar.setSelectedNavigationItem(savedInstanceState.getInt(STATE_PAGE, 0)); + } + } + + @Override + protected void onSaveInstanceState(Bundle outState) + { + super.onSaveInstanceState(outState); + outState.putInt(STATE_PAGE, getActionBar().getSelectedNavigationIndex()); + } + @Override protected void onResume() { @@ -126,4 +160,14 @@ public class MainActivity extends Activity { return propertyHelper; } + + public ViewPager getViewPager() + { + return viewPager; + } + + public PagerAdapter getPagerAdapter() + { + return pagerAdapter; + } } diff --git a/src/net/lacolaco/smileessence/view/adapter/TextFragment.java b/src/net/lacolaco/smileessence/view/adapter/TextFragment.java new file mode 100644 index 00000000..d6abb466 --- /dev/null +++ b/src/net/lacolaco/smileessence/view/adapter/TextFragment.java @@ -0,0 +1,55 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2012-2014 lacolaco.net + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.lacolaco.smileessence.view.adapter; + +import android.app.Fragment; +import android.os.Bundle; +import android.text.TextUtils; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +public class TextFragment extends Fragment +{ + + public static final String ARG_TEXT = "text"; + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) + { + TextView textView = new TextView(getActivity()); + Bundle args = getArguments(); + if(args != null) + { + String text = args.getString(ARG_TEXT); + if(!TextUtils.isEmpty(text)) + { + textView.setText(text); + } + } + return textView; + } +} diff --git a/src/net/lacolaco/smileessence/viewmodel/PageListAdapter.java b/src/net/lacolaco/smileessence/viewmodel/PageListAdapter.java new file mode 100644 index 00000000..df019980 --- /dev/null +++ b/src/net/lacolaco/smileessence/viewmodel/PageListAdapter.java @@ -0,0 +1,133 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2012-2014 lacolaco.net + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.lacolaco.smileessence.viewmodel; + +import android.app.ActionBar; +import android.app.Activity; +import android.app.Fragment; +import android.content.Context; +import android.os.Bundle; +import android.support.v13.app.FragmentStatePagerAdapter; +import android.support.v4.view.ViewPager; +import android.widget.ArrayAdapter; + +import java.util.ArrayList; + +public class PageListAdapter extends FragmentStatePagerAdapter implements ViewPager.OnPageChangeListener, ActionBar.OnNavigationListener +{ + + private final Context context; + private final ActionBar actionBar; + private final ViewPager viewPager; + private final ArrayList<PageInfo> pages = new ArrayList<>(); + + static final class PageInfo + { + + private final String name; + private final Class<?> fragmentClass; + private final Bundle args; + + PageInfo(String name, Class<?> clss, Bundle args) + { + this.name = name; + this.fragmentClass = clss; + this.args = args; + } + } + + public PageListAdapter(Activity activity, ViewPager viewPager) + { + super(activity.getFragmentManager()); + this.context = activity; + this.actionBar = activity.getActionBar(); + this.viewPager = viewPager; + viewPager.setAdapter(this); + viewPager.setOnPageChangeListener(this); + } + + /** + * Add new tab and new page + * + * @param name Page name + * @param clss Fragment class + * @param args Bundle for Fragment instantiate + */ + public void addTab(String name, Class<?> clss, Bundle args) + { + PageInfo info = new PageInfo(name, clss, args); + pages.add(info); + refreshListNavigation(); + notifyDataSetChanged(); + } + + private void refreshListNavigation() + { + ArrayList<String> itemList = new ArrayList<>(); + for(PageInfo page : pages) + { + itemList.add(page.name); + } + ArrayAdapter<String> adapter = new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, android.R.id.text1, itemList); + actionBar.setListNavigationCallbacks(adapter, this); + } + + @Override + public Fragment getItem(int position) + { + PageInfo info = pages.get(position); + return Fragment.instantiate(context, info.fragmentClass.getName(), info.args); + } + + @Override + public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) + { + } + + @Override + public void onPageSelected(int position) + { + //Synchronize pager and navigation. + actionBar.setSelectedNavigationItem(position); + } + + @Override + public void onPageScrollStateChanged(int state) + { + } + + @Override + public int getCount() + { + return pages.size(); + } + + @Override + public boolean onNavigationItemSelected(int itemPosition, long itemId) + { + viewPager.setCurrentItem(itemPosition); + return true; + } +} |