Implemented sub forums.
When the user selects a category, the sub forums are now displayed to them. Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
<manifest package="com.RickBarrette.osj.forum"
|
<manifest package="com.RickBarrette.osj.forum"
|
||||||
android:versionCode="30"
|
android:versionCode="41"
|
||||||
android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto">
|
android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto">
|
||||||
|
|
||||||
<uses-sdk
|
<uses-sdk
|
||||||
|
|||||||
@@ -20,19 +20,27 @@
|
|||||||
package com.RickBarrette.osj.forum;
|
package com.RickBarrette.osj.forum;
|
||||||
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.v4.app.Fragment;
|
import android.support.v4.app.ListFragment;
|
||||||
import android.view.LayoutInflater;
|
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.widget.AbsListView;
|
||||||
import android.widget.TextView;
|
import android.widget.AdapterView;
|
||||||
|
import android.widget.ListView;
|
||||||
|
|
||||||
import com.RickBarrette.osj.forum.dummy.DummyContent;
|
import com.RickBarrette.osj.forum.content.ForumContent;
|
||||||
|
import com.RickBarrette.osj.forum.content.ForumContent.ForumItem;
|
||||||
|
import com.RickBarrette.osj.forum.content.SubForumAdapter;
|
||||||
|
|
||||||
public class ForumDetailFragment extends Fragment {
|
/**
|
||||||
|
* This fragment will be used to display information about the forum
|
||||||
|
* @author ricky barrette
|
||||||
|
*/
|
||||||
|
public class ForumDetailFragment extends ListFragment {
|
||||||
|
|
||||||
public static final String ARG_ITEM_ID = "item_id";
|
public static final String ARG_ITEM_ID = "item_id";
|
||||||
|
private int mActivatedPosition = ListView.INVALID_POSITION;
|
||||||
|
private static final String STATE_ACTIVATED_POSITION = "activated_position";
|
||||||
|
|
||||||
DummyContent.DummyItem mItem;
|
ForumItem mItem;
|
||||||
|
|
||||||
public ForumDetailFragment() {
|
public ForumDetailFragment() {
|
||||||
}
|
}
|
||||||
@@ -41,14 +49,36 @@ public class ForumDetailFragment extends Fragment {
|
|||||||
public void onCreate(final Bundle savedInstanceState) {
|
public void onCreate(final Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
if (getArguments().containsKey(ARG_ITEM_ID))
|
if (getArguments().containsKey(ARG_ITEM_ID))
|
||||||
mItem = DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
|
mItem = ForumContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
|
||||||
|
|
||||||
|
if(mItem != null)
|
||||||
|
this.setListAdapter(new SubForumAdapter(getActivity(), Integer.parseInt(getArguments().getString(ARG_ITEM_ID))));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
|
public void onSaveInstanceState(final Bundle outState) {
|
||||||
final View rootView = inflater.inflate(R.layout.fragment_forum_detail, container, false);
|
super.onSaveInstanceState(outState);
|
||||||
if (mItem != null)
|
if (mActivatedPosition != AdapterView.INVALID_POSITION)
|
||||||
((TextView) rootView.findViewById(R.id.forum_detail)).setText(mItem.content);
|
outState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition);
|
||||||
return rootView;
|
}
|
||||||
|
|
||||||
|
@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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -92,7 +92,7 @@ public class ForumListFragment extends ListFragment {
|
|||||||
@Override
|
@Override
|
||||||
public void onListItemClick(final ListView listView, final View view, final int position, final long id) {
|
public void onListItemClick(final ListView listView, final View view, final int position, final long id) {
|
||||||
super.onListItemClick(listView, view, position, id);
|
super.onListItemClick(listView, view, position, id);
|
||||||
mCallbacks.onItemSelected(DummyContent.ITEMS.get(position).id);
|
mCallbacks.onItemSelected(ForumContent.ITEMS.get(position).id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -24,22 +24,19 @@ import android.view.LayoutInflater;
|
|||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.BaseAdapter;
|
import android.widget.BaseAdapter;
|
||||||
import android.widget.CheckBox;
|
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import com.RickBarrette.osj.forum.R;
|
import com.RickBarrette.osj.forum.R;
|
||||||
import com.RickBarrette.osj.forum.content.ForumContent.ForumItem;
|
import com.RickBarrette.osj.forum.content.ForumContent.ForumItem;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* This adapter will be used to populate a list view with forum entries
|
||||||
* @author ricky barrette
|
* @author ricky barrette
|
||||||
*/
|
*/
|
||||||
public class ForumAdapter extends BaseAdapter {
|
public class ForumAdapter extends BaseAdapter {
|
||||||
|
|
||||||
class ViewHolder {
|
class ViewHolder {
|
||||||
TextView title;
|
TextView title;
|
||||||
TextView description;
|
|
||||||
CheckBox checkbox;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private LayoutInflater mInflater;
|
private LayoutInflater mInflater;
|
||||||
|
|||||||
@@ -51,6 +51,10 @@ public class ForumContent {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
return (String) content.get("forum_name");
|
return (String) content.get("forum_name");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Object[] getChildren(){
|
||||||
|
return (Object[]) this.content.get("child");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<ForumItem> ITEMS = new ArrayList<ForumItem>();
|
public static List<ForumItem> ITEMS = new ArrayList<ForumItem>();
|
||||||
|
|||||||
@@ -0,0 +1,121 @@
|
|||||||
|
/**
|
||||||
|
* SubForumAdapter.java
|
||||||
|
* @date Aug 30, 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.content;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.view.LayoutInflater;
|
||||||
|
import android.view.View;
|
||||||
|
import android.view.ViewGroup;
|
||||||
|
import android.widget.BaseAdapter;
|
||||||
|
import android.widget.TextView;
|
||||||
|
|
||||||
|
import com.RickBarrette.osj.forum.R;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author ricky barrette
|
||||||
|
*/
|
||||||
|
public class SubForumAdapter extends BaseAdapter {
|
||||||
|
|
||||||
|
class ViewHolder {
|
||||||
|
TextView title;
|
||||||
|
}
|
||||||
|
|
||||||
|
private LayoutInflater mInflater;
|
||||||
|
private Object[] mSubForums;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author ricky barrette
|
||||||
|
*/
|
||||||
|
public SubForumAdapter(Context context, int position) {
|
||||||
|
mInflater = LayoutInflater.from(context);
|
||||||
|
mSubForums = ForumContent.ITEMS.get(position).getChildren();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see android.widget.Adapter#getCount()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int getCount() {
|
||||||
|
if(mSubForums != null)
|
||||||
|
return mSubForums.length;
|
||||||
|
else
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see android.widget.Adapter#getItem(int)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public HashMap<?,?> getItem(int position) {
|
||||||
|
return (HashMap<?,?>) mSubForums[position];
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see android.widget.Adapter#getItemId(int)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public long getItemId(int position) {
|
||||||
|
return position;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public View getView(int position, View convertView, ViewGroup parent) {
|
||||||
|
// A ViewHolder keeps references to children views to avoid unnecessary
|
||||||
|
// calls to findViewById() on each row.
|
||||||
|
ViewHolder holder;
|
||||||
|
|
||||||
|
// When convertView is not null, we can reuse it directly, there is no
|
||||||
|
// need
|
||||||
|
// to reinflate it. We only inflate a new View when the convertView
|
||||||
|
// supplied
|
||||||
|
// by ListView is null.
|
||||||
|
if (convertView == null) {
|
||||||
|
convertView = mInflater.inflate(R.layout.forum_item, null);
|
||||||
|
|
||||||
|
// Creates a ViewHolder and store references to the two children
|
||||||
|
// views
|
||||||
|
// we want to bind data to.
|
||||||
|
holder = new ViewHolder();
|
||||||
|
holder.title = (TextView) convertView.findViewById(R.id.textView1);
|
||||||
|
|
||||||
|
convertView.setTag(holder);
|
||||||
|
} else
|
||||||
|
// Get the ViewHolder back to get fast access to the TextView
|
||||||
|
// and the ImageView.
|
||||||
|
holder = (ViewHolder) convertView.getTag();
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Bind the data efficiently with the holder. Remember that you should
|
||||||
|
* always call setChecked() after calling setOnCheckedChangedListener.
|
||||||
|
* This will prevent the list from changing the values on you.
|
||||||
|
*/
|
||||||
|
holder.title.setText(new String((byte[]) getItem(position).get("forum_name")));
|
||||||
|
return convertView;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user