Last active
August 29, 2015 14:09
-
-
Save zhanghai/54936fd75d787c52e145 to your computer and use it in GitHub Desktop.
A ListAdapter (and a StickyListHeaders version) that merges multiple ListAdapters.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2014 Zhang Hai | |
* | |
* 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.yourapplication; | |
import android.database.DataSetObserver; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.BaseAdapter; | |
import android.widget.ListAdapter; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* A ListAdapter that merges multiple ListAdapters. | |
* Initially it propagates data set observer notifications from each of its children to it's view. | |
*/ | |
public class MergeListAdapter extends BaseAdapter { | |
private final DataSetObserver dataSetObserver = new DataSetObserver() { | |
@Override | |
public void onChanged() { | |
if (dataSetObserverSuppressed) { | |
notifyDataSetChanged(); | |
} | |
} | |
@Override | |
public void onInvalidated() { | |
if (dataSetObserverSuppressed) { | |
notifyDataSetInvalidated(); | |
} | |
} | |
}; | |
protected List<ListAdapter> adapterList = new ArrayList<>(); | |
private boolean dataSetObserverSuppressed = false; | |
// NOTE: No no-args constructor provided so that users get to know the observer issue. | |
public MergeListAdapter(boolean dataSetObserverSuppressed) { | |
this.dataSetObserverSuppressed = dataSetObserverSuppressed; | |
} | |
@Override | |
public int getCount() { | |
int count = 0; | |
for (ListAdapter adapter : adapterList) { | |
count += adapter.getCount(); | |
} | |
return count; | |
} | |
@Override | |
public Object getItem(int position) { | |
for (ListAdapter adapter : adapterList) { | |
int count = adapter.getCount(); | |
if (position < count) { | |
return adapter.getItem(position); | |
} else { | |
position -= count; | |
} | |
} | |
throw new IllegalStateException("Unknown position: " + position); | |
} | |
@Override | |
public long getItemId(int position) { | |
int i = 0; | |
for (ListAdapter adapter : adapterList) { | |
int count = adapter.getCount(); | |
if (position < count) { | |
return 31 * i + adapter.getItemId(position); | |
} else { | |
position -= count; | |
++i; | |
} | |
} | |
throw new IllegalStateException("Unknown position: " + position); | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
for (ListAdapter adapter : adapterList) { | |
int count = adapter.getCount(); | |
if (position < count) { | |
return adapter.getView(position, convertView, parent); | |
} else { | |
position -= count; | |
} | |
} | |
throw new IllegalStateException("Unknown position: " + position); | |
} | |
@Override | |
public boolean isEnabled(int position) { | |
for (ListAdapter adapter : adapterList) { | |
int count = adapter.getCount(); | |
if (position < count) { | |
return adapter.isEnabled(position); | |
} else { | |
position -= count; | |
} | |
} | |
throw new IllegalStateException("Unknown position: " + position); | |
} | |
@Override | |
public boolean areAllItemsEnabled() { | |
for (ListAdapter adapter : adapterList) { | |
if (!adapter.areAllItemsEnabled()) { | |
return false; | |
} | |
} | |
return true; | |
} | |
@Override | |
public boolean hasStableIds() { | |
for (ListAdapter adapter : adapterList) { | |
if (!adapter.hasStableIds()) { | |
return false; | |
} | |
} | |
return true; | |
} | |
@Override | |
public int getViewTypeCount() { | |
int count = 0; | |
for (ListAdapter adapter : adapterList) { | |
count += adapter.getViewTypeCount(); | |
} | |
return count; | |
} | |
// NOTICE: Assumes that view type of each adapter starts at 0 and increments by 1. | |
@Override | |
public int getItemViewType(int position) { | |
int type = 0; | |
for (ListAdapter adapter : adapterList) { | |
int count = adapter.getCount(); | |
if (position < count) { | |
return type + adapter.getItemViewType(position); | |
} else { | |
position -= count; | |
type += adapter.getViewTypeCount(); | |
} | |
} | |
throw new IllegalStateException("Unknown position: " + position); | |
} | |
public void addAdapter(ListAdapter adapter) { | |
adapterList.add(adapter); | |
adapter.registerDataSetObserver(dataSetObserver); | |
} | |
public void addView(View view, boolean enabled) { | |
addAdapter(new SingleViewAdapter(view, enabled)); | |
} | |
public void addView(View view) { | |
addView(view, true); | |
} | |
public boolean isDataSetObserverSuppressed() { | |
return dataSetObserverSuppressed; | |
} | |
public void setDataSetObserverSuppressed(boolean dataSetObserverSuppressed) { | |
this.dataSetObserverSuppressed = dataSetObserverSuppressed; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2014 Zhang Hai | |
* | |
* 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.yourapplication; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ListAdapter; | |
import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter; | |
public class MergeStickyHeaderListAdapter extends MergeListAdapter | |
implements StickyListHeadersAdapter { | |
public interface HeaderViewAdapter { | |
public View getHeaderView(View convertView, ViewGroup parent); | |
} | |
public MergeStickyHeaderListAdapter(boolean dataSetObserverSuppressed) { | |
super(dataSetObserverSuppressed); | |
} | |
@Override | |
public View getHeaderView(int position, View convertView, ViewGroup parent) { | |
for (ListAdapter adapter : adapterList) { | |
int count = adapter.getCount(); | |
if (position < count) { | |
return ((HeaderViewAdapter)adapter).getHeaderView(convertView, parent); | |
} else { | |
position -= count; | |
} | |
} | |
throw new IllegalStateException("Unknown position: " + position); | |
} | |
@Override | |
public long getHeaderId(int position) { | |
int i = 0; | |
for (ListAdapter adapter : adapterList) { | |
int count = adapter.getCount(); | |
if (position < count) { | |
return i; | |
} else { | |
position -= count; | |
++i; | |
} | |
} | |
throw new IllegalStateException("Unknown position: " + position); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2014 Zhang Hai | |
* | |
* 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.yourapplication; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.BaseAdapter; | |
public class SingleViewAdapter extends BaseAdapter { | |
private View view; | |
private boolean enabled; | |
public SingleViewAdapter() { | |
this(true); | |
} | |
public SingleViewAdapter(boolean enabled) { | |
this.enabled = enabled; | |
} | |
public SingleViewAdapter(View view) { | |
this(view, true); | |
} | |
public SingleViewAdapter(View view, boolean enabled) { | |
this.view = view; | |
this.enabled = enabled; | |
} | |
public View getView() { | |
return view; | |
} | |
public void setView(View view) { | |
this.view = view; | |
} | |
public boolean isEnabled() { | |
return enabled; | |
} | |
public void setEnabled(boolean enabled) { | |
this.enabled = enabled; | |
} | |
protected View createView(ViewGroup parent) { | |
// Returns null by default. | |
return null; | |
} | |
@Override | |
public int getCount() { | |
return 1; | |
} | |
@Override | |
public Object getItem(int position) { | |
return null; | |
} | |
@Override | |
public long getItemId(int position) { | |
return 0; | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
if (view == null) { | |
setView(createView(parent)); | |
} | |
return view; | |
} | |
@Override | |
public boolean hasStableIds() { | |
return true; | |
} | |
@Override | |
public boolean areAllItemsEnabled() { | |
return enabled; | |
} | |
@Override | |
public boolean isEnabled(int position) { | |
return enabled; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment