OSDN Git Service

Add support for automatic sorting. Fix visibility bugs.
authorKoushik Dutta <koushd@gmail.com>
Mon, 21 Jan 2013 00:37:30 +0000 (16:37 -0800)
committerKoushik Dutta <koushd@gmail.com>
Mon, 21 Jan 2013 00:37:30 +0000 (16:37 -0800)
Widgets/src/com/koushikdutta/widgets/ActivityBase.java
Widgets/src/com/koushikdutta/widgets/ActivityBaseFragment.java
Widgets/src/com/koushikdutta/widgets/SeparatedListAdapter.java

index 06c9d42..8d9481a 100644 (file)
@@ -29,7 +29,7 @@ public class ActivityBase extends FragmentActivity implements ActivityBaseFragme
     }
     
     ActivityBaseFragment fragment;
-    protected final void onCreate(Bundle savedInstanceState) {
+    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         
         try {
index 99aea0e..4763184 100644 (file)
@@ -1,6 +1,6 @@
 package com.koushikdutta.widgets;
 
-import java.util.HashMap;
+import java.util.Comparator;
 
 import android.content.Context;
 import android.os.Bundle;
@@ -9,7 +9,6 @@ import android.support.v4.app.Fragment;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
-import android.widget.Adapter;
 import android.widget.AdapterView;
 import android.widget.AdapterView.OnItemClickListener;
 import android.widget.AdapterView.OnItemLongClickListener;
@@ -18,12 +17,12 @@ import android.widget.ListView;
 import android.widget.TextView;
 
 public class ActivityBaseFragment extends Fragment {
-    protected boolean mDestroyed = false;
+    boolean mDestroyed = false;
     
-    protected ListView mListView;
-    protected MyAdapter mAdapter;
+    ListView mListView;
+    MyAdapter mAdapter;
     
-    protected class MyAdapter extends SeparatedListAdapter {
+    static class MyAdapter extends SeparatedListAdapter<ListItemAdapter> {
         public MyAdapter(Context context) {
             super(context);
         }
@@ -42,8 +41,8 @@ public class ActivityBaseFragment extends Fragment {
         }
     }
     
-    public class MyListAdapter extends ArrayAdapter<ListItem> {
-        public MyListAdapter(Context context) {
+    public static class ListItemAdapter extends ArrayAdapter<ListItem> {
+        public ListItemAdapter(Context context) {
             super(context, 0);
         }
         
@@ -63,32 +62,93 @@ public class ActivityBaseFragment extends Fragment {
             ListItem item = getItem(position);
             return item.getEnabled();
         }
+        
+        boolean sorting;
+        @Override
+        public void notifyDataSetChanged() {
+            if (sorter != null && !sorting) {
+                sorting = true;
+                super.sort(sorter);
+                sorting = false;
+            }
+            else {
+                super.notifyDataSetChanged();
+            }
+        }
+        
+        private Comparator<ListItem> sorter;
+        public void setSort(Comparator<ListItem> sorter) {
+            this.sorter = sorter;
+            notifyDataSetChanged();
+        }
+        
+        public Comparator<ListItem> getSort() {
+            return sorter;
+        }
+        
+        public Comparator<ListItem> ALPHA = new Comparator<ListItem>() {
+            @Override
+            public int compare(ListItem lhs, ListItem rhs) {
+                return lhs.getTitle().compareTo(rhs.getTitle());
+            }
+        };
+        
+        public Comparator<ListItem> ALPHAIGNORECASE = new Comparator<ListItem>() {
+            @Override
+            public int compare(ListItem lhs, ListItem rhs) {
+                return lhs.getTitle().compareToIgnoreCase(rhs.getTitle());
+            }
+        };
+
+        public Comparator<ListItem> NONE = new Comparator<ListItem>() {
+            @Override
+            public int compare(ListItem lhs, ListItem rhs) {
+                return ((Integer)getPosition(lhs)).compareTo(getPosition(rhs));
+            }
+        };
     }
     
-    HashMap<String, MyListAdapter> mAdapters = new HashMap<String, ActivityBaseFragment.MyListAdapter>();
+//    HashMap<String, MyListAdapter> mAdapters = new HashMap<String, ActivityBaseFragment.MyListAdapter>();
+    
+    public ListItemAdapter ensureHeader(int sectionName) {
+        return ensureHeader(mAdapter.getSectionCount(), getString(sectionName));
+    }
     
-    protected MyListAdapter ensureHeader(int sectionName) {
-        String sn = getString(sectionName);
-        MyListAdapter adapter = mAdapters.get(sn);
+    public ListItemAdapter ensureHeader(String sectionName) {
+        return ensureHeader(mAdapter.getSectionCount(), sectionName);
+    }
+
+    public ListItemAdapter ensureHeader(int index, int sectionName) {
+        return ensureHeader(index, getString(sectionName));
+    }
+    public ListItemAdapter ensureHeader(int index, String sectionName) {
+        ListItemAdapter adapter = mAdapter.getSection(sectionName);
         if (adapter == null) {
-            adapter = new MyListAdapter(getActivity());
-            mAdapters.put(sn, adapter);
-            mAdapter.addSection(sn, adapter);
+            adapter = new ListItemAdapter(getActivity());
+            mAdapter.addSection(index, sectionName, adapter);
             mListView.setAdapter(null);
             mListView.setAdapter(mAdapter);
         }
         return adapter;
     }
+    
+    public ListItemAdapter getSection(int section) {
+        return getSection(getString(section));
+    }
+    
+    public ListItemAdapter getSection(String section) {
+        return mAdapter.getSection(section);
+    }
 
-    protected ListItem addItem(int sectionName, ListItem item) {
+    public ListItem addItem(int sectionName, ListItem item) {
         return addItem(getString(sectionName), item);
     }
 
-    protected ListItem addItem(int sectionName, ListItem item, int index) {
+    public ListItem addItem(int sectionName, ListItem item, int index) {
         return addItem(getString(sectionName), item, index);
     }
     
-    protected ListItem addItem(String sectionName, ListItem item) {
+    public ListItem addItem(String sectionName, ListItem item) {
         return addItem(sectionName, item, -1);
     }
     
@@ -97,17 +157,16 @@ public class ActivityBaseFragment extends Fragment {
     }
     
     public int getSectionItemCount(String section) {
-        MyListAdapter adapter = mAdapters.get(section);
+        ListItemAdapter adapter = mAdapter.getSection(section);
         if (adapter == null)
             return 0;
         return adapter.getCount();
     }
 
-    protected ListItem addItem(String sectionName, ListItem item, int index) {
-        MyListAdapter adapter = mAdapters.get(sectionName);
+    public ListItem addItem(String sectionName, ListItem item, int index) {
+        ListItemAdapter adapter = mAdapter.getSection(sectionName);
         if (adapter == null) {
-            adapter = new MyListAdapter(getActivity());
-            mAdapters.put(sectionName, adapter);
+            adapter = new ListItemAdapter(getActivity());
             mAdapter.addSection(sectionName, adapter);
             if (mListView != null) {
                 mListView.setAdapter(null);
@@ -120,18 +179,18 @@ public class ActivityBaseFragment extends Fragment {
             adapter.insert(item, index);
         else
             adapter.add(item);
-        
+
+        mAdapter.notifyDataSetChanged();
         return item;
     }
     
-    protected ListItem findItem(int item) {
+    public ListItem findItem(int item) {
         String text = getString(item);
         
-        for (Adapter adapter: mAdapter.sections.values())
+        for (ListItemAdapter adapter: mAdapter.getSections())
         {
-            MyListAdapter m = (MyListAdapter)adapter;
-            for (int i = 0; i < m.getCount(); i++) {
-                ListItem li = m.getItem(i);
+            for (int i = 0; i < adapter.getCount(); i++) {
+                ListItem li = adapter.getItem(i);
                 if (text.equals(li.getTitle()))
                     return li;
             }
@@ -215,25 +274,32 @@ public class ActivityBaseFragment extends Fragment {
         return R.layout.list_item_small;
     }
 
-    protected void clear() {
+    public void clear() {
         mAdapter.clear();
-        mAdapters.clear();
     }
     
-    protected void clearSection(int section) {
-        clearSection(getActivity().getString(section));
+    public void clearSection(int section) {
+        clearSection(mListView.getContext().getString(section));
     }
     
-    protected void clearSection(String section) {
-        MyListAdapter adapter = mAdapters.get(section);
+    public void clearSection(String section) {
+        ListItemAdapter adapter = mAdapter.getSection(section);
         if (adapter == null)
             return;
         adapter.clear();
         mAdapter.notifyDataSetChanged();
     }
     
+    public void removeSection(int section) {
+        removeSection(mListView.getContext().getString(section));
+    }
+    
+    public void removeSection(String section) {
+        mAdapter.removeSection(section);
+    }
+    
     public void removeItem(ListItem item) {
-        for (MyListAdapter adapter: mAdapters.values()) {
+        for (ListItemAdapter adapter: mAdapter.getSections()) {
             adapter.remove(item);
         }
         mAdapter.notifyDataSetChanged();
index c0ffe74..ab50106 100644 (file)
@@ -1,7 +1,6 @@
 package com.koushikdutta.widgets;
 
-import java.util.LinkedHashMap;
-import java.util.Map;
+import java.util.HashMap;
 
 import android.content.Context;
 import android.view.View;
@@ -10,35 +9,56 @@ import android.widget.Adapter;
 import android.widget.ArrayAdapter;
 import android.widget.BaseAdapter;
 
-public class SeparatedListAdapter extends BaseAdapter {
+public class SeparatedListAdapter<T extends Adapter> extends BaseAdapter {
     public void clear() {
         sections.clear();
         headers.clear();
         notifyDataSetChanged();
     }
 
-    public final Map<String,Adapter> sections = new LinkedHashMap<String,Adapter>();
-    public final ArrayAdapter<String> headers;
-    public final static int TYPE_SECTION_HEADER = 0;
+    private final HashMap<String, T> sections = new HashMap<String, T>();
+    private final ArrayAdapter<String> headers;
+    private final static int TYPE_SECTION_HEADER = 0;
 
     public SeparatedListAdapter(Context context) {
         headers = new ArrayAdapter<String>(context, R.layout.list_header);
     }
 
-    public void addSection(String section, Adapter adapter) {
+    public void addSection(String section, T adapter) {
         this.headers.add(section);
         this.sections.put(section, adapter);
     }
 
+    public void addSection(int index, String section, T adapter) {
+        this.headers.insert(section, index);
+        this.sections.put(section, adapter);
+    }
+    
+    public void removeSection(String section) {
+        this.headers.remove(section);
+        this.sections.remove(section);
+    }
+    
+    public T getSection(String section) {
+        return sections.get(section);
+    }
+    
+    public Iterable<T> getSections() {
+        return sections.values();
+    }
+
     @Override
     public Object getItem(int position) {
-        for(Object section : this.sections.keySet()) {
+        for (int i = 0; i < headers.getCount(); i++) {
+            String section = headers.getItem(i);
             Adapter adapter = sections.get(section);
             int size = adapter.getCount() + 1;
 
             // check if position inside this section
-            if(position == 0) return section;
-            if(position < size) return adapter.getItem(position - 1);
+            if (position == 0)
+                return section;
+            if (position < size)
+                return adapter.getItem(position - 1);
 
             // otherwise jump into next section
             position -= size;
@@ -49,17 +69,21 @@ public class SeparatedListAdapter extends BaseAdapter {
     public int getCount() {
         // total together all sections, plus one for each section header
         int total = 0;
-        for(Adapter adapter : this.sections.values())
+        for (Adapter adapter : this.sections.values())
             total += adapter.getCount() + 1;
         return total;
     }
 
+    public int getSectionCount() {
+        return headers.getCount();
+    }
+    
     @Override
     public int getViewTypeCount() {
         // assume that headers count as one, and that there will be at least a itemViewType.
         // then total all sections
         int total = 2;
-        for(Adapter adapter : this.sections.values())
+        for (Adapter adapter : this.sections.values())
             total += adapter.getViewTypeCount();
         return total;
     }
@@ -67,13 +91,16 @@ public class SeparatedListAdapter extends BaseAdapter {
     @Override
     public int getItemViewType(int position) {
         int type = 1;
-        for(Object section : this.sections.keySet()) {
+        for (int i = 0; i < headers.getCount(); i++) {
+            String section = headers.getItem(i);
             Adapter adapter = sections.get(section);
             int size = adapter.getCount() + 1;
 
             // check if position inside this section
-            if(position == 0) return TYPE_SECTION_HEADER;
-            if(position < size) return type + adapter.getItemViewType(position - 1);
+            if (position == 0)
+                return TYPE_SECTION_HEADER;
+            if (position < size)
+                return type + adapter.getItemViewType(position - 1);
 
             // otherwise jump into next section
             position -= size;
@@ -90,13 +117,16 @@ public class SeparatedListAdapter extends BaseAdapter {
     @Override
     public View getView(int position, View convertView, ViewGroup parent) {
         int sectionnum = 0;
-        for(Object section : this.sections.keySet()) {
+        for (int i = 0; i < headers.getCount(); i++) {
+            String section = headers.getItem(i);
             Adapter adapter = sections.get(section);
             int size = adapter.getCount() + 1;
 
             // check if position inside this section
-            if(position == 0) return headers.getView(sectionnum, convertView, parent);
-            if(position < size) return adapter.getView(position - 1, convertView, parent);
+            if (position == 0)
+                return headers.getView(sectionnum, convertView, parent);
+            if (position < size)
+                return adapter.getView(position - 1, convertView, parent);
 
             // otherwise jump into next section
             position -= size;