From 8d4840fce3288c5a5bcf5b10a44761ae4f45703c Mon Sep 17 00:00:00 2001 From: Ricky Barrette Date: Wed, 29 Aug 2012 12:57:58 -0400 Subject: [PATCH] Added an other master detail flow activity for Forums Signed-off-by: Ricky Barrette --- OSJ Forum/AndroidManifest.xml | 2 +- OSJ Forum/project.properties | 2 +- .../res/layout/activity_forum_detail.xml | 6 + OSJ Forum/res/layout/activity_forum_list.xml | 9 ++ .../res/layout/activity_forum_twopane.xml | 23 ++++ .../res/layout/fragment_forum_detail.xml | 8 ++ .../osj/forum/ForumDetailActivity.java | 55 +++++++++ .../osj/forum/ForumDetailFragment.java | 54 +++++++++ .../osj/forum/ForumListActivity.java | 60 ++++++++++ .../osj/forum/ForumListFragment.java | 108 ++++++++++++++++++ 10 files changed, 325 insertions(+), 2 deletions(-) create mode 100644 OSJ Forum/res/layout/activity_forum_detail.xml create mode 100644 OSJ Forum/res/layout/activity_forum_list.xml create mode 100644 OSJ Forum/res/layout/activity_forum_twopane.xml create mode 100644 OSJ Forum/res/layout/fragment_forum_detail.xml create mode 100644 OSJ Forum/src/com/RickBarrette/osj/forum/ForumDetailActivity.java create mode 100644 OSJ Forum/src/com/RickBarrette/osj/forum/ForumDetailFragment.java create mode 100644 OSJ Forum/src/com/RickBarrette/osj/forum/ForumListActivity.java create mode 100644 OSJ Forum/src/com/RickBarrette/osj/forum/ForumListFragment.java diff --git a/OSJ Forum/AndroidManifest.xml b/OSJ Forum/AndroidManifest.xml index 87acb4c..46a8042 100644 --- a/OSJ Forum/AndroidManifest.xml +++ b/OSJ Forum/AndroidManifest.xml @@ -1,5 +1,5 @@ diff --git a/OSJ Forum/res/layout/activity_forum_list.xml b/OSJ Forum/res/layout/activity_forum_list.xml new file mode 100644 index 0000000..fae1483 --- /dev/null +++ b/OSJ Forum/res/layout/activity_forum_list.xml @@ -0,0 +1,9 @@ + diff --git a/OSJ Forum/res/layout/activity_forum_twopane.xml b/OSJ Forum/res/layout/activity_forum_twopane.xml new file mode 100644 index 0000000..d59c0e5 --- /dev/null +++ b/OSJ Forum/res/layout/activity_forum_twopane.xml @@ -0,0 +1,23 @@ + + + + + + + diff --git a/OSJ Forum/res/layout/fragment_forum_detail.xml b/OSJ Forum/res/layout/fragment_forum_detail.xml new file mode 100644 index 0000000..21e3204 --- /dev/null +++ b/OSJ Forum/res/layout/fragment_forum_detail.xml @@ -0,0 +1,8 @@ + diff --git a/OSJ Forum/src/com/RickBarrette/osj/forum/ForumDetailActivity.java b/OSJ Forum/src/com/RickBarrette/osj/forum/ForumDetailActivity.java new file mode 100644 index 0000000..4c0b62e --- /dev/null +++ b/OSJ Forum/src/com/RickBarrette/osj/forum/ForumDetailActivity.java @@ -0,0 +1,55 @@ +/** + * ForumDetailActivity + * @date Aug 29, 2012 + * @author ricky barrette + * + * Copyright 2012 Richard Barrette + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ +package com.RickBarrette.osj.forum; + +import android.content.Intent; +import android.os.Bundle; +import android.support.v4.app.FragmentActivity; +import android.support.v4.app.NavUtils; +import android.view.MenuItem; + +public class ForumDetailActivity extends FragmentActivity { + + @Override + protected void onCreate(final Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_forum_detail); + + getActionBar().setDisplayHomeAsUpEnabled(true); + + if (savedInstanceState == null) { + final Bundle arguments = new Bundle(); + arguments.putString(ForumDetailFragment.ARG_ITEM_ID, getIntent().getStringExtra(ForumDetailFragment.ARG_ITEM_ID)); + final ForumDetailFragment fragment = new ForumDetailFragment(); + fragment.setArguments(arguments); + getSupportFragmentManager().beginTransaction().add(R.id.forum_detail_container, fragment).commit(); + } + } + + @Override + public boolean onOptionsItemSelected(final MenuItem item) { + if (item.getItemId() == android.R.id.home) { + NavUtils.navigateUpTo(this, new Intent(this, ForumListActivity.class)); + return true; + } + + return super.onOptionsItemSelected(item); + } +} diff --git a/OSJ Forum/src/com/RickBarrette/osj/forum/ForumDetailFragment.java b/OSJ Forum/src/com/RickBarrette/osj/forum/ForumDetailFragment.java new file mode 100644 index 0000000..ac3e3ad --- /dev/null +++ b/OSJ Forum/src/com/RickBarrette/osj/forum/ForumDetailFragment.java @@ -0,0 +1,54 @@ +/** + * ForumDetailFragment + * @date Aug 29, 2012 + * @author ricky barrette + * + * Copyright 2012 Richard Barrette + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ +package com.RickBarrette.osj.forum; + +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; + +import com.RickBarrette.osj.forum.dummy.DummyContent; + +public class ForumDetailFragment extends Fragment { + + public static final String ARG_ITEM_ID = "item_id"; + + DummyContent.DummyItem mItem; + + public ForumDetailFragment() { + } + + @Override + public void onCreate(final Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + if (getArguments().containsKey(ARG_ITEM_ID)) + mItem = DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID)); + } + + @Override + public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) { + final View rootView = inflater.inflate(R.layout.fragment_forum_detail, container, false); + if (mItem != null) + ((TextView) rootView.findViewById(R.id.forum_detail)).setText(mItem.content); + return rootView; + } +} diff --git a/OSJ Forum/src/com/RickBarrette/osj/forum/ForumListActivity.java b/OSJ Forum/src/com/RickBarrette/osj/forum/ForumListActivity.java new file mode 100644 index 0000000..180948e --- /dev/null +++ b/OSJ Forum/src/com/RickBarrette/osj/forum/ForumListActivity.java @@ -0,0 +1,60 @@ +/** + * ForumListActivity + * @date Aug 29, 2012 + * @author ricky barrette + * + * Copyright 2012 Richard Barrette + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ +package com.RickBarrette.osj.forum; + +import android.content.Intent; +import android.os.Bundle; +import android.support.v4.app.FragmentActivity; + +import com.TwentyCodes.android.exception.ExceptionHandler; + +public class ForumListActivity extends FragmentActivity implements ForumListFragment.Callbacks { + + private boolean mTwoPane; + + @Override + public void onCreate(final Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this)); + + setContentView(R.layout.activity_forum_list); + + if (findViewById(R.id.forum_detail_container) != null) { + mTwoPane = true; + ((ForumListFragment) getSupportFragmentManager().findFragmentById(R.id.forum_list)).setActivateOnItemClick(true); + } + } + + @Override + public void onItemSelected(final String id) { + if (mTwoPane) { + final Bundle arguments = new Bundle(); + arguments.putString(ForumDetailFragment.ARG_ITEM_ID, id); + final ForumDetailFragment fragment = new ForumDetailFragment(); + fragment.setArguments(arguments); + getSupportFragmentManager().beginTransaction().replace(R.id.forum_detail_container, fragment).commit(); + + } else { + final Intent detailIntent = new Intent(this, ForumDetailActivity.class); + detailIntent.putExtra(ForumDetailFragment.ARG_ITEM_ID, id); + startActivity(detailIntent); + } + } +} diff --git a/OSJ Forum/src/com/RickBarrette/osj/forum/ForumListFragment.java b/OSJ Forum/src/com/RickBarrette/osj/forum/ForumListFragment.java new file mode 100644 index 0000000..98d5dd7 --- /dev/null +++ b/OSJ Forum/src/com/RickBarrette/osj/forum/ForumListFragment.java @@ -0,0 +1,108 @@ +/** + * ForumListFragment + * @date Aug 27, 2012 + * @author ricky barrette + * + * Copyright 2012 Richard Barrette + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ +package com.RickBarrette.osj.forum; + +import android.R; +import android.app.Activity; +import android.os.Bundle; +import android.support.v4.app.ListFragment; +import android.view.View; +import android.widget.AbsListView; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; +import android.widget.ListView; + +import com.RickBarrette.osj.forum.dummy.DummyContent; + +public class ForumListFragment extends ListFragment { + + public interface Callbacks { + + public void onItemSelected(String id); + } + + private static final String STATE_ACTIVATED_POSITION = "activated_position"; + private Callbacks mCallbacks = sDummyCallbacks; + + private int mActivatedPosition = ListView.INVALID_POSITION; + + private static Callbacks sDummyCallbacks = new Callbacks() { + @Override + public void onItemSelected(final String id) { + } + }; + + public ForumListFragment() { + } + + @Override + public void onAttach(final Activity activity) { + super.onAttach(activity); + if (!(activity instanceof Callbacks)) + throw new IllegalStateException("Activity must implement fragment's callbacks."); + + mCallbacks = (Callbacks) activity; + } + + @Override + public void onCreate(final Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setListAdapter(new ArrayAdapter(getActivity(), R.layout.simple_list_item_activated_1, R.id.text1, DummyContent.ITEMS)); + } + + @Override + public void onDetach() { + super.onDetach(); + mCallbacks = sDummyCallbacks; + } + + @Override + public void onListItemClick(final ListView listView, final View view, final int position, final long id) { + super.onListItemClick(listView, view, position, id); + mCallbacks.onItemSelected(DummyContent.ITEMS.get(position).id); + } + + @Override + public void onSaveInstanceState(final Bundle outState) { + super.onSaveInstanceState(outState); + if (mActivatedPosition != AdapterView.INVALID_POSITION) + outState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition); + } + + @Override + public void onViewCreated(final View view, final Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + if (savedInstanceState != null && savedInstanceState.containsKey(STATE_ACTIVATED_POSITION)) + setActivatedPosition(savedInstanceState.getInt(STATE_ACTIVATED_POSITION)); + } + + public void setActivatedPosition(final int position) { + if (position == AdapterView.INVALID_POSITION) + getListView().setItemChecked(mActivatedPosition, false); + else + getListView().setItemChecked(position, true); + + mActivatedPosition = position; + } + + public void setActivateOnItemClick(final boolean activateOnItemClick) { + getListView().setChoiceMode(activateOnItemClick ? AbsListView.CHOICE_MODE_SINGLE : AbsListView.CHOICE_MODE_NONE); + } +}