Android Open Source - android-tools Header List Adapter






From Project

Back to project page android-tools.

License

The source code is released under:

MIT License

If you think the Android project android-tools listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.comfreeze.lib.adapter;
//from w  w w  .  j av  a  2  s  . c om
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.SectionIndexer;
import android.widget.TextView;

import net.comfreeze.lib.views.HeaderListView;

abstract public class HeaderListAdapter extends CursorAdapter implements SectionIndexer, OnScrollListener, IHeaderListAdapter {
    private static final String TAG = HeaderListAdapter.class.getSimpleName();
    public SectionIndexer indexer;
    public int headerId = -1;
    public int dividerId = -1;
    private int backgroundId;

    public HeaderListAdapter(Context context, Cursor cursor, int flags) {
        super(context, cursor, flags);
    }

    public void onScroll(AbsListView list, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if (list instanceof HeaderListView) {
            ((HeaderListView) list).configureHeaderView(firstVisibleItem);
        }
    }

    public void onScrollStateChanged(AbsListView view, int scrollState) {
    }

    public HeaderListAdapter setIndexer(SectionIndexer indexer) {
        this.indexer = indexer;
        return this;
    }

    public int getPositionForSection(int section) {
        if (indexer == null)
            return -1;
        return indexer.getPositionForSection(section);
    }

    public int getSectionForPosition(int position) {
        if (indexer == null)
            return -1;
        return indexer.getSectionForPosition(position);
    }

    public Object[] getSections() {
        if (indexer == null)
            return new String[]{""};
        else
            return indexer.getSections();
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        bindSectionHeader(view, cursor.getPosition());
    }

    public HeaderState getHeaderState(int position) {
        if (indexer == null || getCount() == 0 || position < 0)
            return HeaderState.GONE;

        // The header should get pushed up if the top item shown
        // is the last item in a section for a particular letter.
        int section = getSectionForPosition(position);
        int nextSectionPosition = getPositionForSection(section + 1);

        if (nextSectionPosition != -1 && position == nextSectionPosition - 1)
            return HeaderState.PUSHED_UP;

        return HeaderState.VISIBLE;
    }

    public void setHeaderBackgroundDrawableId(int id) {
        this.backgroundId = id;
    }

    private void bindSectionHeader(View itemView, int position) {
        final TextView headerView = (TextView) itemView.findViewById(headerId);
        final View dividerView = itemView.findViewById(dividerId);

        final int section = getSectionForPosition(position);
        if (getPositionForSection(section) == position) {
            String title = (String) indexer.getSections()[section];
            if (null != headerView)
                headerView.setText(title);
            toggleVisibility(headerView, View.VISIBLE);
            toggleVisibility(dividerView, View.GONE);
        } else {
            toggleVisibility(headerView, View.GONE);
            toggleVisibility(dividerView, View.VISIBLE);
        }

        // move the divider for the last item in a section
        if (getPositionForSection(section + 1) - 1 == position) {
            toggleVisibility(dividerView, View.GONE);
        } else {
            toggleVisibility(dividerView, View.VISIBLE);
        }
    }

    private void toggleVisibility(View view, int visible) {
        if (null != view)
            view.setVisibility(visible);
    }
}




Java Source Code List

net.comfreeze.lib.BundleBuilder.java
net.comfreeze.lib.CFZApplication.java
net.comfreeze.lib.ContentValueBuilder.java
net.comfreeze.lib.FragmentMap.java
net.comfreeze.lib.adapter.HeaderListAdapter.java
net.comfreeze.lib.adapter.IHeaderListAdapter.java
net.comfreeze.lib.adapter.SeparatedListAdapter.java
net.comfreeze.lib.api.BaseAPI.java
net.comfreeze.lib.api.RestAPI.java
net.comfreeze.lib.api.XMLAPI.java
net.comfreeze.lib.api.helper.CursorHelper.java
net.comfreeze.lib.api.helper.JSONHelper.java
net.comfreeze.lib.api.helper.ModelHelper.java
net.comfreeze.lib.api.xml.WordpressAPI.java
net.comfreeze.lib.audio.SoundManager.java
net.comfreeze.lib.db.DatabaseHelper.java
net.comfreeze.lib.db.DatabaseTable.java
net.comfreeze.lib.db.helper.HelperCursor.java
net.comfreeze.lib.db.model.CFZModel.java
net.comfreeze.lib.db.model.FieldColumnMap.java
net.comfreeze.lib.fragments.CFZListFragment.java
net.comfreeze.lib.provider.CFZSimpleProvider.java
net.comfreeze.lib.service.CFZService.java
net.comfreeze.lib.ui.SupportFragmentActivity.java
net.comfreeze.lib.ui.dialog.CFZDialogProgress.java
net.comfreeze.lib.ui.fragment.CFZFragmentBase.java
net.comfreeze.lib.views.BiScrollView.java
net.comfreeze.lib.views.CFZViewHelper.java
net.comfreeze.lib.views.FlowLayout.java
net.comfreeze.lib.views.GestureHelper.java
net.comfreeze.lib.views.HeaderListView.java
net.comfreeze.lib.views.ResizingView.java
net.comfreeze.lib.views.ViewCollection.java
net.comfreeze.lib.views.ViewUtils.java
net.comfreeze.lib.xml.XMLParser.java
net.comfreeze.lib.xml.wordpress.FeedXmlParser.java