1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
package com.customlist;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewParent;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.FrameLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
/**
* View displaying the list with sectioned header.
*/
public class SectionListView extends ListView implements OnScrollListener {
private View transparentView;
public SectionListView(final Context context, final AttributeSet attrs,
final int defStyle) {
super(context, attrs, defStyle);
commonInitialisation();
}
public SectionListView(final Context context, final AttributeSet attrs) {
super(context, attrs);
commonInitialisation();
}
public SectionListView(final Context context) {
super(context);
commonInitialisation();
}
protected final void commonInitialisation() {
setOnScrollListener(this);
setVerticalFadingEdgeEnabled(false);
setFadingEdgeLength(0);
}
@Override
public void setAdapter(final ListAdapter adapter) {
if (!(adapter instanceof SectionListAdapter)) {
throw new IllegalArgumentException(
"The adapter needds to be of type "
+ SectionListAdapter.class + " and is "
+ adapter.getClass());
}
super.setAdapter(adapter);
final ViewParent parent = getParent();
if (!(parent instanceof FrameLayout)) {
throw new IllegalStateException(
"Section List should have FrameLayout as parent!");
}
if (transparentView != null) {
((FrameLayout) parent).removeView(transparentView);
}
transparentView = ((SectionListAdapter) adapter)
.getTransparentSectionView();
final FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
((FrameLayout) parent).addView(transparentView, lp);
if (adapter.isEmpty()) {
transparentView.setVisibility(View.INVISIBLE);
}
}
@Override
public void onScroll(final AbsListView view, final int firstVisibleItem,
final int visibleItemCount, final int totalItemCount) {
final SectionListAdapter adapter = (SectionListAdapter) getAdapter();
if (adapter != null) {
adapter.makeSectionInvisibleIfFirstInList(firstVisibleItem);
}
}
@Override
public void onScrollStateChanged(final AbsListView view,
final int scrollState) {
// do nothing
}
} |
Partager