diff options
author | Camil Staps | 2015-04-08 22:19:04 +0200 |
---|---|---|
committer | Camil Staps | 2015-04-08 22:19:04 +0200 |
commit | 19687378e6c59233725e0b7b1580140642e13a47 (patch) | |
tree | 9fc92307c0fc4b1404151dcf5ba65986a140de92 /app/src/main | |
parent | Fixed bug not updating reading after language change (diff) |
Made an incomplete, messy start with ViewPager
Diffstat (limited to 'app/src/main')
-rw-r--r-- | app/src/main/AndroidManifest.xml | 8 | ||||
-rw-r--r-- | app/src/main/java/com/camilstaps/taize/CollectionDemoActivity.java | 61 | ||||
-rw-r--r-- | app/src/main/java/com/camilstaps/taize/DailyReadingFragment.java | 99 | ||||
-rw-r--r-- | app/src/main/java/com/camilstaps/taize/DemoObjectFragment.java | 30 | ||||
-rw-r--r-- | app/src/main/java/com/camilstaps/taize/MainActivity.java | 198 | ||||
-rw-r--r-- | app/src/main/res/layout/activity_main.xml | 118 | ||||
-rw-r--r-- | app/src/main/res/layout/pager.xml | 10 | ||||
-rw-r--r-- | app/src/main/res/layout/pager_content.xml | 54 |
8 files changed, 374 insertions, 204 deletions
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 6ae08cb..3ebad4a 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -27,6 +27,14 @@ android:name="android.support.PARENT_ACTIVITY" android:value=".MainActivity" /> </activity> + <activity + android:name=".CollectionDemoActivity" + android:label="Demo" + android:parentActivityName=".MainActivity" > + <meta-data + android:name="android.support.PARENT_ACTIVITY" + android:value=".MainActivity" /> + </activity> </application> </manifest> diff --git a/app/src/main/java/com/camilstaps/taize/CollectionDemoActivity.java b/app/src/main/java/com/camilstaps/taize/CollectionDemoActivity.java new file mode 100644 index 0000000..9558af7 --- /dev/null +++ b/app/src/main/java/com/camilstaps/taize/CollectionDemoActivity.java @@ -0,0 +1,61 @@ +package com.camilstaps.taize; + +import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentActivity; +import android.support.v4.app.FragmentManager; +import android.support.v4.app.FragmentStatePagerAdapter; +import android.support.v4.view.ViewPager; + +/** + * Created by camilstaps on 8-4-15. + */ +public class CollectionDemoActivity extends FragmentActivity { + private static final int NUM_PAGES = 3; + + // When requested, this adapter returns a DemoObjectFragment, + // representing an object in the collection. + DemoCollectionPagerAdapter mDemoCollectionPagerAdapter; + ViewPager mViewPager; + + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.pager); + + // ViewPager and its adapters use support library + // fragments, so use getSupportFragmentManager. + mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter(getSupportFragmentManager()); + mViewPager = (ViewPager) findViewById(R.id.pager); + mViewPager.setAdapter(mDemoCollectionPagerAdapter); + } + + @Override + public void onBackPressed() { + if (mViewPager.getCurrentItem() == 0) { + super.onBackPressed(); + } else { + mViewPager.setCurrentItem(mViewPager.getCurrentItem() - 1); + } + } + + private class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter { + public DemoCollectionPagerAdapter(FragmentManager fm) { + super(fm); + } + + @Override + public Fragment getItem(int i) { + return new DemoObjectFragment(); + } + + @Override + public int getCount() { + return NUM_PAGES; + } + + @Override + public CharSequence getPageTitle(int position) { + return "OBJECT " + (position + 1); + } + } +}
\ No newline at end of file diff --git a/app/src/main/java/com/camilstaps/taize/DailyReadingFragment.java b/app/src/main/java/com/camilstaps/taize/DailyReadingFragment.java new file mode 100644 index 0000000..cc774f7 --- /dev/null +++ b/app/src/main/java/com/camilstaps/taize/DailyReadingFragment.java @@ -0,0 +1,99 @@ +package com.camilstaps.taize; + +import android.content.SharedPreferences; +import android.os.Bundle; +import android.preference.PreferenceManager; +import android.support.v4.app.Fragment; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import com.camilstaps.common.Date; +import com.camilstaps.common.Listener; + +/** + * Created by camilstaps on 8-4-15. + */ +public class DailyReadingFragment extends Fragment { + + private SharedPreferences.OnSharedPreferenceChangeListener sharedPreferenceChangeListener; + + View rootView; + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + // The last two arguments ensure LayoutParams are inflated + // properly. + rootView = inflater.inflate(R.layout.pager_content, container, false); + + //Bundle args = getArguments(); + //((TextView) rootView.findViewById(R.id.pager_content_text)).setText(args.getString("text")); + + setupContent(); + + return rootView; + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + + sharedPreferenceChangeListener = new SharedPreferences.OnSharedPreferenceChangeListener() { + @Override + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { + switch (key) { + case "dailyreadings": + case "dailyreadingbible": + setupContent(); + break; + } + } + }; + PreferenceManager.getDefaultSharedPreferences(getActivity()).registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener); + } + + protected void setupContent() { + setDailyReading(); + setDailyReadingBibleText(); + + Log.d("MAIN", "Setting up content"); + } + + private void setDailyReading() { + Date date = new Date(); + + Taize.getDailyReading(getActivity(), new Listener<DailyReading>() { + @Override + public void success(DailyReading data) { + ((TextView) rootView.findViewById(R.id.textDailyReading)).setText(data.getTextWithoutReference()); + BibleText bt = data.getBibleReference(); + if (bt != null) + ((TextView) rootView.findViewById(R.id.refDailyReadingBibleText)).setText(bt.toString()); + } + + @Override + public void failure() { + ((TextView) rootView.findViewById(R.id.textDailyReading)).setText("No reading found."); + } + }, date); + + ((TextView) rootView.findViewById(R.id.dateDailyReading)).setText(date.toNiceString(getActivity())); + } + + private void setDailyReadingBibleText() { + Taize.getDailyReadingBibleText(getActivity(), new Listener<String>() { + @Override + public void success(String data) { + ((TextView) rootView.findViewById(R.id.textDailyReadingBibleText)).setText(data); + } + + @Override + public void failure() { + ((TextView) rootView.findViewById(R.id.textDailyReadingBibleText)).setText("No bible text found."); + } + }); + } + +} diff --git a/app/src/main/java/com/camilstaps/taize/DemoObjectFragment.java b/app/src/main/java/com/camilstaps/taize/DemoObjectFragment.java new file mode 100644 index 0000000..17a0858 --- /dev/null +++ b/app/src/main/java/com/camilstaps/taize/DemoObjectFragment.java @@ -0,0 +1,30 @@ +package com.camilstaps.taize; + +import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +/** + * Created by camilstaps on 8-4-15. + */ + + +// Instances of this class are fragments representing a single +// object in our collection. +public class DemoObjectFragment extends Fragment { + public static final String ARG_OBJECT = "object"; + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + // The last two arguments ensure LayoutParams are inflated + // properly. + View rootView = inflater.inflate(R.layout.pager_content, container, false); + Bundle args = getArguments(); + //((TextView) rootView.findViewById(R.id.pager_content_text)).setText(args.getString("text")); + return rootView; + } +} + diff --git a/app/src/main/java/com/camilstaps/taize/MainActivity.java b/app/src/main/java/com/camilstaps/taize/MainActivity.java index 4f22a38..e9157ef 100644 --- a/app/src/main/java/com/camilstaps/taize/MainActivity.java +++ b/app/src/main/java/com/camilstaps/taize/MainActivity.java @@ -1,55 +1,36 @@ package com.camilstaps.taize; -import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.preference.PreferenceManager; -import android.support.v7.app.ActionBarActivity; -import android.support.v7.app.ActionBar; import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; +import android.support.v4.app.FragmentStatePagerAdapter; +import android.support.v4.view.ViewPager; +import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.util.Log; -import android.view.LayoutInflater; import android.view.Menu; +import android.view.MenuInflater; import android.view.MenuItem; -import android.view.View; -import android.view.ViewGroup; -import android.support.v4.widget.DrawerLayout; import android.widget.TextView; import com.camilstaps.common.Date; import com.camilstaps.common.Listener; -import com.camilstaps.common.NullListener; - -import java.util.HashSet; - - -public class MainActivity extends ActionBarActivity - implements NavigationDrawerFragment.NavigationDrawerCallbacks { - - /** - * Fragment managing the behaviors, interactions and presentation of the navigation drawer. - */ - private NavigationDrawerFragment mNavigationDrawerFragment; - /** - * Used to store the last screen title. For use in {@link #restoreActionBar()}. - */ - private CharSequence mTitle; +public class MainActivity extends ActionBarActivity { private SharedPreferences.OnSharedPreferenceChangeListener sharedPreferenceChangeListener; + ViewPager pager; + PagerAdapter pagerAdapter; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main); - - mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_drawer); - mTitle = getTitle(); - // Set up the drawer. - mNavigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout)); + setContentView(R.layout.activity_main); setupContent(); @@ -65,6 +46,14 @@ public class MainActivity extends ActionBarActivity } }; PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener); + + /*Intent intent = new Intent(this, CollectionDemoActivity.class); + startActivity(intent);*/ + + pager = (ViewPager) findViewById(R.id.pager); + pagerAdapter = new PagerAdapter(getSupportFragmentManager()); + pager.setAdapter(pagerAdapter); + } @Override @@ -79,6 +68,14 @@ public class MainActivity extends ActionBarActivity setupContent(); } + public void onBackPressed() { + if (pager.getCurrentItem() == 0) { + super.onBackPressed(); + } else { + pager.setCurrentItem(pager.getCurrentItem() - 1); + } + } + protected void setupContent() { setDailyReading(); setDailyReadingBibleText(); @@ -87,78 +84,44 @@ public class MainActivity extends ActionBarActivity } private void setDailyReading() { - Date date = new Date(); - - Taize.getDailyReading(this, new Listener<DailyReading>() { - @Override - public void success(DailyReading data) { - ((TextView) findViewById(R.id.textDailyReading)).setText(data.getTextWithoutReference()); - BibleText bt = data.getBibleReference(); - if (bt != null) - ((TextView) findViewById(R.id.refDailyReadingBibleText)).setText(bt.toString()); - } - - @Override - public void failure() { - ((TextView) findViewById(R.id.textDailyReading)).setText("No reading found."); - } - }, date); - - ((TextView) findViewById(R.id.dateDailyReading)).setText(date.toNiceString(this)); +// Date date = new Date(); +// +// Taize.getDailyReading(this, new Listener<DailyReading>() { +// @Override +// public void success(DailyReading data) { +// ((TextView) findViewById(R.id.textDailyReading)).setText(data.getTextWithoutReference()); +// BibleText bt = data.getBibleReference(); +// if (bt != null) +// ((TextView) findViewById(R.id.refDailyReadingBibleText)).setText(bt.toString()); +// } +// +// @Override +// public void failure() { +// ((TextView) findViewById(R.id.textDailyReading)).setText("No reading found."); +// } +// }, date); +// +// ((TextView) findViewById(R.id.dateDailyReading)).setText(date.toNiceString(this)); } private void setDailyReadingBibleText() { - Taize.getDailyReadingBibleText(this, new Listener<String>() { - @Override - public void success(String data) { - ((TextView) findViewById(R.id.textDailyReadingBibleText)).setText(data); - } - - @Override - public void failure() { - ((TextView) findViewById(R.id.textDailyReadingBibleText)).setText("No bible text found."); - } - }); - } - - @Override - public void onNavigationDrawerItemSelected(int position) { - // update the main content by replacing fragments - FragmentManager fragmentManager = getSupportFragmentManager(); - fragmentManager.beginTransaction() - .replace(R.id.container, PlaceholderFragment.newInstance(position + 1)) - .commit(); - } - - public void onSectionAttached(int number) { - switch (number) { - case 1: - mTitle = getString(R.string.title_section1); - break; - case 2: - mTitle = getString(R.string.title_section2); - break; - } +// Taize.getDailyReadingBibleText(this, new Listener<String>() { +// @Override +// public void success(String data) { +// ((TextView) findViewById(R.id.textDailyReadingBibleText)).setText(data); +// } +// +// @Override +// public void failure() { +// ((TextView) findViewById(R.id.textDailyReadingBibleText)).setText("No bible text found."); +// } +// }); } - public void restoreActionBar() { - ActionBar actionBar = getSupportActionBar(); - actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); - actionBar.setDisplayShowTitleEnabled(true); - actionBar.setTitle(mTitle); - } - - @Override public boolean onCreateOptionsMenu(Menu menu) { - if (!mNavigationDrawerFragment.isDrawerOpen()) { - // Only show items in the action bar relevant to this screen - // if the drawer is not showing. Otherwise, let the drawer - // decide what to show in the action bar. - getMenuInflater().inflate(R.menu.main, menu); - restoreActionBar(); - return true; - } + MenuInflater inflater = getMenuInflater(); + inflater.inflate(R.menu.global, menu); return super.onCreateOptionsMenu(menu); } @@ -179,49 +142,28 @@ public class MainActivity extends ActionBarActivity return super.onOptionsItemSelected(item); } - /** - * A placeholder fragment containing a simple view. - */ - public static class PlaceholderFragment extends Fragment { - /** - * The fragment argument representing the section number for this - * fragment. - */ - private static final String ARG_SECTION_NUMBER = "section_number"; - - /** - * Returns a new instance of this fragment for the given section - * number. - */ - public static PlaceholderFragment newInstance(int sectionNumber) { - PlaceholderFragment fragment = new PlaceholderFragment(); - Bundle args = new Bundle(); - args.putInt(ARG_SECTION_NUMBER, sectionNumber); - fragment.setArguments(args); - return fragment; - } - - public PlaceholderFragment() { + private class PagerAdapter extends FragmentStatePagerAdapter { + public PagerAdapter(FragmentManager fm) { + super(fm); } @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - View rootView = inflater.inflate(R.layout.fragment_main, container, false); - - return rootView; + public Fragment getItem(int i) { + DailyReadingFragment fragment = new DailyReadingFragment(); + /*Bundle args = new Bundle(); + args.putString("text", "Hello world! " + i); + fragment.setArguments(args);*/ + return fragment; } @Override - public void onAttach(Activity activity) { - super.onAttach(activity); - ((MainActivity) activity).onSectionAttached(getArguments().getInt(ARG_SECTION_NUMBER)); + public int getCount() { + return Integer.MAX_VALUE; } @Override - public void onResume() { - super.onResume(); - + public CharSequence getPageTitle(int position) { + return "OBJECT " + (position + 1); } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index c4e3253..193db2d 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,78 +1,44 @@ -<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. --> -<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" - xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" - android:layout_width="match_parent" android:layout_height="match_parent" - tools:context=".MainActivity"> - - <!-- As the main content view, the view below consumes the entire - space available using match_parent in both dimensions. --> - <FrameLayout android:id="@+id/container" android:layout_width="match_parent" - android:layout_height="match_parent" > - - <ScrollView - android:layout_width="fill_parent" +<!-- As the main content view, the view below consumes the entire + space available using match_parent in both dimensions. --> +<RelativeLayout + xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:tools="http://schemas.android.com/tools" + android:id="@+id/container" + android:layout_width="match_parent" + android:layout_height="match_parent" + tools:context=".MainActivity" + android:orientation="vertical" + android:padding="0dp"> + + <LinearLayout + android:id="@+id/tabs" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:orientation="horizontal" + android:layout_alignParentBottom="true"> + + <Button + android:layout_width="0dip" android:layout_height="wrap_content" - android:paddingLeft="10dp" - android:paddingBottom="10dp" - android:paddingTop="10dp" - android:paddingRight="0dp"> - - <LinearLayout - android:layout_width="match_parent" - android:layout_height="match_parent" - android:orientation="vertical" - android:layout_weight="1.0" - android:paddingRight="10dp"> - - <TextView - android:layout_width="wrap_content" - android:layout_height="wrap_content" - android:text="Date" - android:id="@+id/dateDailyReading" - android:layout_gravity="center" - android:layout_marginBottom="6dp" - android:textStyle="bold"/> - - <TextView - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:text="Daily Reading" - android:id="@+id/textDailyReading" - android:layout_gravity="left|top" - android:layout_marginBottom="10dp" - android:textIsSelectable="true"/> + android:layout_weight=".5" + android:text="Button 1"/> - <TextView - android:layout_width="wrap_content" - android:layout_height="wrap_content" - android:text="Bible Text" - android:id="@+id/refDailyReadingBibleText" - android:layout_gravity="center" - android:layout_marginBottom="6dp" - android:textStyle="bold"/> - - <TextView - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:text="Daily Reading Bible text" - android:id="@+id/textDailyReadingBibleText" - android:layout_gravity="left|top" - android:textIsSelectable="true"/> - - </LinearLayout> - </ScrollView> - </FrameLayout> - - <!-- android:layout_gravity="start" tells DrawerLayout to treat - this as a sliding drawer on the left side for left-to-right - languages and on the right side for right-to-left languages. - If you're not building against API 17 or higher, use - android:layout_gravity="left" instead. --> - <!-- The drawer is given a fixed width in dp and extends the full height of - the container. --> - <fragment android:id="@+id/navigation_drawer" - android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent" - android:layout_gravity="start" android:name="com.camilstaps.taize.NavigationDrawerFragment" - tools:layout="@layout/fragment_navigation_drawer" /> - -</android.support.v4.widget.DrawerLayout> + <Button + android:layout_width="0dip" + android:layout_height="wrap_content" + android:layout_weight=".5" + android:text="Button 2"/> + + </LinearLayout> + + <android.support.v4.view.ViewPager + xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:tools="http://schemas.android.com/tools" + android:id="@+id/pager" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:layout_above="@id/tabs" + android:layout_margin="0dp"> + + </android.support.v4.view.ViewPager> +</RelativeLayout> diff --git a/app/src/main/res/layout/pager.xml b/app/src/main/res/layout/pager.xml new file mode 100644 index 0000000..35b8e01 --- /dev/null +++ b/app/src/main/res/layout/pager.xml @@ -0,0 +1,10 @@ +<?xml version="1.0" encoding="utf-8"?> +<android.support.v4.view.ViewPager + xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:tools="http://schemas.android.com/tools" + android:id="@+id/pager" + android:layout_width="match_parent" + android:layout_height="match_parent" + tools:context=".CollectionDemoActivity"> + +</android.support.v4.view.ViewPager>
\ No newline at end of file diff --git a/app/src/main/res/layout/pager_content.xml b/app/src/main/res/layout/pager_content.xml new file mode 100644 index 0000000..0f53c69 --- /dev/null +++ b/app/src/main/res/layout/pager_content.xml @@ -0,0 +1,54 @@ +<?xml version="1.0" encoding="utf-8"?> +<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" + android:id="@+id/content" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:paddingLeft="10dp" + android:paddingBottom="10dp" + android:paddingTop="10dp" + android:paddingRight="0dp"> + + <LinearLayout + android:layout_width="match_parent" + android:layout_height="match_parent" + android:orientation="vertical" + android:layout_weight="1.0" + android:paddingRight="10dp"> + + <TextView + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text="Date" + android:id="@+id/dateDailyReading" + android:layout_gravity="center" + android:layout_marginBottom="6dp" + android:textStyle="bold"/> + + <TextView + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:text="Daily Reading" + android:id="@+id/textDailyReading" + android:layout_gravity="left|top" + android:layout_marginBottom="10dp" + android:textIsSelectable="true"/> + + <TextView + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text="Bible Text" + android:id="@+id/refDailyReadingBibleText" + android:layout_gravity="center" + android:layout_marginBottom="6dp" + android:textStyle="bold"/> + + <TextView + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:text="Daily Reading Bible text" + android:id="@+id/textDailyReadingBibleText" + android:layout_gravity="left|top" + android:textIsSelectable="true"/> + + </LinearLayout> +</ScrollView>
\ No newline at end of file |