From 390a7ec82598ba25f3dfa536ae819e771393f65b Mon Sep 17 00:00:00 2001 From: knuthy Date: Fri, 15 Aug 2014 14:27:13 +0200 Subject: first shot for improved expandable lists --- .../java/com/zeapo/pwdstore/PasswordFragment.java | 31 ++++-- .../com/zeapo/pwdstore/utils/PasswordAdapter.java | 104 ++++++++++++++++++++- app/src/main/res/layout/child_row_layout.xml | 30 ++++++ app/src/main/res/layout/fragment_password_list.xml | 5 +- 4 files changed, 155 insertions(+), 15 deletions(-) create mode 100644 app/src/main/res/layout/child_row_layout.xml (limited to 'app/src') diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java b/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java index ab7c8a77..a8dc67bb 100644 --- a/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java +++ b/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java @@ -10,6 +10,7 @@ import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.AdapterView; import android.widget.ArrayAdapter; +import android.widget.ExpandableListView; import android.widget.ListAdapter; import android.widget.ListView; @@ -27,14 +28,14 @@ import java.util.List; * with a GridView. *

*/ -public class PasswordFragment extends Fragment implements AbsListView.OnItemClickListener { +public class PasswordFragment extends Fragment implements ExpandableListView.OnGroupClickListener { private OnFragmentInteractionListener mListener; /** * The fragment's ListView/GridView. */ - private ListView mListView; + private ExpandableListView mListView; /** * The Adapter which will be used to populate the ListView/GridView with @@ -62,11 +63,11 @@ public class PasswordFragment extends Fragment implements AbsListView.OnItemClic View view = inflater.inflate(R.layout.fragment_password, container, false); // Set the adapter - mListView = (ListView) view.findViewById(R.id.pass_list); - ((AdapterView) mListView).setAdapter(mAdapter); + mListView = (ExpandableListView) view.findViewById(R.id.pass_list); + mListView.setAdapter((android.widget.ExpandableListAdapter) mAdapter); // Set OnItemClickListener so we can be notified on item clicks - mListView.setOnItemClickListener(this); + mListView.setOnGroupClickListener(this); mListView.setSelectionFromTop(getArguments().getInt("Position"), 0); return view; @@ -97,12 +98,22 @@ public class PasswordFragment extends Fragment implements AbsListView.OnItemClic } @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { - if (null != mListener) { - // Notify the active callbacks interface (the activity, if the - // fragment is attached to one) that an item has been selected. - mListener.onFragmentInteraction(mAdapter.getItem(position)); + public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) { + if( ((PasswordItem) mAdapter.getGroup(i)).getType() == PasswordItem.TYPE_CATEGORY ){ + if (null != mListener) { + // Notify the active callbacks interface (the activity, if the + // fragment is attached to one) that an item has been selected. + mListener.onFragmentInteraction(mAdapter.getItem(i)); + } + } else { + if (expandableListView.isGroupExpanded(i)) { + expandableListView.collapseGroup(i); + } else { + expandableListView.expandGroup(i); + } } + + return true; } public interface OnFragmentInteractionListener { diff --git a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordAdapter.java b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordAdapter.java index b0cdb5a5..c18b1ab8 100644 --- a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordAdapter.java +++ b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordAdapter.java @@ -1,18 +1,21 @@ package com.zeapo.pwdstore.utils; import android.content.Context; +import android.database.DataSetObserver; import android.graphics.Typeface; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; +import android.widget.ExpandableListAdapter; import android.widget.TextView; import com.zeapo.pwdstore.R; import java.util.ArrayList; -public class PasswordAdapter extends ArrayAdapter { +public class PasswordAdapter extends ArrayAdapter implements ExpandableListAdapter{ private final Context context; private final ArrayList values; @@ -28,9 +31,57 @@ public class PasswordAdapter extends ArrayAdapter { } @Override - public View getView(int position, View convertView, ViewGroup parent) { + public void registerDataSetObserver(DataSetObserver dataSetObserver) { + + } + + @Override + public void unregisterDataSetObserver(DataSetObserver dataSetObserver) { + + } + + @Override + public int getGroupCount() { + return values.size(); + } + + @Override + public int getChildrenCount(int i) { + if (values.get(i).getType() == PasswordItem.TYPE_CATEGORY) + return 0; + else + return 1; + } + + @Override + public Object getGroup(int i) { + return values.get(i); + } + + @Override + public Object getChild(int i, int i2) { + return null; + } + + @Override + public long getGroupId(int i) { + return 0; + } + + @Override + public long getChildId(int i, int i2) { + return 0; + } + + @Override + public boolean hasStableIds() { + return false; + } + + @Override + public View getGroupView(int i, boolean b, View convertView, ViewGroup viewGroup) { View rowView = convertView; - PasswordItem pass = values.get(position); + PasswordItem pass = values.get(i); // reuse for performance, holder pattern! if (rowView == null) { @@ -60,4 +111,51 @@ public class PasswordAdapter extends ArrayAdapter { return rowView; } + + @Override + public View getChildView(int i, int i2, boolean b, View view, ViewGroup viewGroup) { + PasswordItem pass = values.get(i); + + LayoutInflater inflater = (LayoutInflater) context + .getSystemService(Context.LAYOUT_INFLATER_SERVICE); + view = inflater.inflate(R.layout.child_row_layout, null); + Log.i("ADAPTER", "Child clicked"); + + return view; + } + + @Override + public boolean isChildSelectable(int i, int i2) { + return false; + } + + @Override + public boolean areAllItemsEnabled() { + return false; + } + + @Override + public boolean isEmpty() { + return false; + } + + @Override + public void onGroupExpanded(int i) { + + } + + @Override + public void onGroupCollapsed(int i) { + + } + + @Override + public long getCombinedChildId(long l, long l2) { + return 0; + } + + @Override + public long getCombinedGroupId(long l) { + return 0; + } } \ No newline at end of file diff --git a/app/src/main/res/layout/child_row_layout.xml b/app/src/main/res/layout/child_row_layout.xml new file mode 100644 index 00000000..a60ead53 --- /dev/null +++ b/app/src/main/res/layout/child_row_layout.xml @@ -0,0 +1,30 @@ + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_password_list.xml b/app/src/main/res/layout/fragment_password_list.xml index 64dd3d94..6ae0c0d8 100644 --- a/app/src/main/res/layout/fragment_password_list.xml +++ b/app/src/main/res/layout/fragment_password_list.xml @@ -5,11 +5,12 @@ android:layout_height="match_parent" tools:context="com.zeapo.pwdstore.PasswordFragment"> - + android:divider="@android:color/transparent" + android:groupIndicator="@android:drawable/screen_background_light_transparent"/> -- cgit v1.2.3