Added an other master detail flow activity for Forums
Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
<manifest package="com.RickBarrette.osj.forum"
|
||||
android:versionCode="1"
|
||||
android:versionCode="2"
|
||||
android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto">
|
||||
|
||||
<uses-sdk
|
||||
|
||||
@@ -12,4 +12,4 @@
|
||||
|
||||
# Project target.
|
||||
target=android-16
|
||||
android.library.reference.1=../exception_handler_library/ExceptionHandlerLib
|
||||
android.library.reference.1=../../exception_handler_library/ExceptionHandlerLib
|
||||
|
||||
6
OSJ Forum/res/layout/activity_forum_detail.xml
Normal file
6
OSJ Forum/res/layout/activity_forum_detail.xml
Normal file
@@ -0,0 +1,6 @@
|
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:id="@+id/forum_detail_container"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".ForumDetailActivity" />
|
||||
9
OSJ Forum/res/layout/activity_forum_list.xml
Normal file
9
OSJ Forum/res/layout/activity_forum_list.xml
Normal file
@@ -0,0 +1,9 @@
|
||||
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:name="com.RickBarrette.osj.forum.ForumListFragment"
|
||||
android:id="@+id/forum_list"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_marginLeft="16dp"
|
||||
android:layout_marginRight="16dp"
|
||||
tools:context=".ForumListActivity" />
|
||||
23
OSJ Forum/res/layout/activity_forum_twopane.xml
Normal file
23
OSJ Forum/res/layout/activity_forum_twopane.xml
Normal file
@@ -0,0 +1,23 @@
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:orientation="horizontal"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_marginLeft="16dp"
|
||||
android:layout_marginRight="16dp"
|
||||
android:divider="?android:attr/dividerHorizontal"
|
||||
android:showDividers="middle"
|
||||
tools:context=".ForumListActivity">
|
||||
|
||||
<fragment android:name="com.RickBarrette.osj.forum.ForumListFragment"
|
||||
android:id="@+id/forum_list"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_weight="1" />
|
||||
|
||||
<FrameLayout android:id="@+id/forum_detail_container"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_weight="3" />
|
||||
|
||||
</LinearLayout>
|
||||
8
OSJ Forum/res/layout/fragment_forum_detail.xml
Normal file
8
OSJ Forum/res/layout/fragment_forum_detail.xml
Normal file
@@ -0,0 +1,8 @@
|
||||
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
style="?android:attr/textAppearanceLarge"
|
||||
android:id="@+id/forum_detail"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:padding="16dp"
|
||||
tools:context=".ForumDetailFragment" />
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
108
OSJ Forum/src/com/RickBarrette/osj/forum/ForumListFragment.java
Normal file
108
OSJ Forum/src/com/RickBarrette/osj/forum/ForumListFragment.java
Normal file
@@ -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<DummyContent.DummyItem>(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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user