AmazingListView+AmazingAdapter

package com.example.XCar;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;

/**
 * Created with IntelliJ IDEA.
 * User: S7
 * Date: 13-7-22
 * Time: 下午8:46
 * To change this template use File | Settings | File Templates.
 */
public class AmazingListView extends ListView
        implements AmazingAdapter.HasMorePagesListener
{
    public static final String TAG = AmazingListView.class.getSimpleName();
    private AmazingAdapter adapter;
    boolean footerViewAttached = false;
    public boolean isScrolling = false;
    View listFooter;
    private View mHeaderView;
    private int mHeaderViewHeight;
    private boolean mHeaderViewVisible;
    private int mHeaderViewWidth;
    public AmazingListViewScrollListener my_scrolling_listener;

    public AmazingListView(Context paramContext)
    {
        super(paramContext);
    }

    public AmazingListView(Context paramContext, AttributeSet paramAttributeSet)
    {
        super(paramContext, paramAttributeSet);
    }

    public AmazingListView(Context paramContext, AttributeSet paramAttributeSet, int paramInt)
    {
        super(paramContext, paramAttributeSet, paramInt);
    }

    public void configureHeaderView(int position)
    {
        if(this.mHeaderView == null){
            return;
        }

        int state = adapter.getPinnedHeaderState(position);

        switch (state){
            case AmazingAdapter.PINNED_HEADER_GONE:

                mHeaderViewVisible = false;
                break;
            case AmazingAdapter.PINNED_HEADER_VISIBLE:
                adapter.configurePinnedHeader(mHeaderView, position, 255);
                if (mHeaderView.getTop() != 0) {
                    mHeaderView.layout(0, 0, mHeaderViewWidth, mHeaderViewHeight);
                }
                mHeaderViewVisible = true;
                break;


            case AmazingAdapter.PINNED_HEADER_PUSHED_UP:
                View firstView = getChildAt(0);
                if (firstView != null) {
                    int bottom = firstView.getBottom();
                    int headerHeight = mHeaderView.getHeight();
                    int y;
                    int alpha;
                    if (bottom < headerHeight) {
                        y = (bottom - headerHeight);
                        alpha = 255 * (headerHeight + y) / headerHeight;
                    } else {
                        y = 0;
                        alpha = 255;
                    }
                    adapter.configurePinnedHeader(mHeaderView, position, alpha);
                    if (mHeaderView.getTop() != y) {
                        mHeaderView.layout(0, y, mHeaderViewWidth, mHeaderViewHeight + y);
                    }
                    mHeaderViewVisible = true;
                }
                break;

        }
    }

    protected void dispatchDraw(Canvas canvas)
    {
        super.dispatchDraw(canvas);
        if (mHeaderViewVisible)
            drawChild(canvas, mHeaderView, getDrawingTime());
    }

    public AmazingAdapter getAdapter()
    {
        return this.adapter;
    }

    public View getLoadingView()
    {
        return this.listFooter;
    }

    public boolean isLoadingViewVisible()
    {
        return this.footerViewAttached;
    }

    public void mayHaveMorePages()
    {
        if (! footerViewAttached && listFooter != null) {
            this.addFooterView(listFooter);
            footerViewAttached = true;
        }
    }

    public void noMorePages()
    {
        if (this.listFooter != null)
            removeFooterView(this.listFooter);
        this.footerViewAttached = false;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (mHeaderView != null) {
            mHeaderView.layout(0, 0, mHeaderViewWidth, mHeaderViewHeight);
            configureHeaderView(getFirstVisiblePosition());
        }
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (mHeaderView != null) {
            measureChild(mHeaderView, widthMeasureSpec, heightMeasureSpec);
            mHeaderViewWidth = mHeaderView.getMeasuredWidth();
            mHeaderViewHeight = mHeaderView.getMeasuredHeight();
        }
    }

    public void setAdapter(ListAdapter paramListAdapter)
    {
        if (!(paramListAdapter instanceof AmazingAdapter))
            throw new IllegalArgumentException(AmazingListView.class.getSimpleName() + " must use adapter of type " + AmazingAdapter.class.getSimpleName());
        if (this.adapter != null)
        {
            this.adapter.setHasMorePagesListener(null);
            setOnScrollListener(null);
        }
        this.adapter = ((AmazingAdapter)paramListAdapter);
        ((AmazingAdapter)paramListAdapter).setHasMorePagesListener(this);
        setOnScrollListener((AmazingAdapter)paramListAdapter);
        View localView = new View(getContext());
        super.addFooterView(localView);
        super.setAdapter(paramListAdapter);
        super.removeFooterView(localView);
        requestLayout();
    }

    public void setLoadingView(View paramView)
    {
        this.listFooter = paramView;
    }

    public void setPinnedHeaderView(View paramView)
    {
        this.mHeaderView = paramView;
        if (this.mHeaderView != null)
            setFadingEdgeLength(0);
        requestLayout();
    }

    public void setPinnedHeaderViewHeight(int paramInt)
    {
        this.mHeaderViewHeight = paramInt;
    }

    public void setScrollingListener(AmazingListViewScrollListener paramAmazingListViewScrollListener)
    {
        this.my_scrolling_listener = paramAmazingListViewScrollListener;
    }
}
package com.example.XCar;

import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.SectionIndexer;

/**
 * Created with IntelliJ IDEA.
 * User: S7
 * Date: 13-7-22
 * Time: 下午8:47
 * To change this template use File | Settings | File Templates.
 */
public abstract class AmazingAdapter extends BaseAdapter
        implements SectionIndexer, AbsListView.OnScrollListener
{
    public static final int PINNED_HEADER_GONE = 0;
    public static final int PINNED_HEADER_PUSHED_UP = 2;
    public static final int PINNED_HEADER_VISIBLE = 1;
    public static final String TAG = AmazingAdapter.class.getSimpleName();
    boolean automaticNextPageLoading = false;
    HasMorePagesListener hasMorePagesListener;
    int initialPage = 1;
    int page = 1;

    protected abstract void bindSectionHeader(View paramView, int paramInt, boolean paramBoolean);

    public abstract void configurePinnedHeader(View paramView, int paramInt1, int paramInt2);

    public abstract View getAmazingView(int paramInt, View paramView, ViewGroup paramViewGroup);

    /**
     * Computes the desired state of the pinned header for the given
     * position of the first visible list item. Allowed return values are
     * {@link #PINNED_HEADER_GONE}, {@link #PINNED_HEADER_VISIBLE} or
     * {@link #PINNED_HEADER_PUSHED_UP}.
     */
    public int getPinnedHeaderState(int position) {
        if (position < 0 || getCount() == 0) {
            return PINNED_HEADER_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 PINNED_HEADER_PUSHED_UP;
        }

        return PINNED_HEADER_VISIBLE;
    }

    public abstract int getPositionForSection(int paramInt);

    public abstract int getSectionForPosition(int paramInt);

    public abstract Object[] getSections();

    public final View getView(int position, View convertView, ViewGroup parent)
    {
       /* View localView = getAmazingView(paramInt, paramView, paramViewGroup);
        if ((paramInt == -1 + getCount()) && (this.automaticNextPageLoading))
            onNextPageRequested(1 + this.page);
        if (getPositionForSection(getSectionForPosition(paramInt)) == paramInt);
        for (boolean bool = true; ; bool = false)
        {
            bindSectionHeader(localView, paramInt, bool);
            return localView;
        }*/

        View res = getAmazingView(position, convertView, parent);

        if (position == getCount() - 1 && automaticNextPageLoading) {
            onNextPageRequested(page + 1);
        }

        final int section = getSectionForPosition(position);
        boolean displaySectionHeaders = (getPositionForSection(section) == position);

        bindSectionHeader(res, position, displaySectionHeaders);

        return res;
    }

    public void nextPage()
    {
        this.page = (1 + this.page);
    }

    public void notifyMayHaveMorePages()
    {
        this.automaticNextPageLoading = true;
        if (this.hasMorePagesListener != null)
         this.hasMorePagesListener.mayHaveMorePages();
    }

    public void notifyNoMorePages()
    {
        this.automaticNextPageLoading = false;
        if (this.hasMorePagesListener == null)
            return;
        this.hasMorePagesListener.noMorePages();
    }

    protected abstract void onNextPageRequested(int paramInt);

    public void onScroll(AbsListView paramAbsListView, int paramInt1, int paramInt2, int paramInt3)
    {
        if (paramAbsListView instanceof AmazingListView)
             ((AmazingListView)paramAbsListView).configureHeaderView(paramInt1);
    }

    public void onScrollStateChanged(AbsListView view, int scrollState)
    {
        if(view instanceof AmazingListView){
            if(scrollState != PINNED_HEADER_GONE){
                ((AmazingListView)view).isScrolling = true;
            }else{
                ((AmazingListView)view).isScrolling = false;
            }
        }

        if(((AmazingListView)view).my_scrolling_listener != null)
            ((AmazingListView)view).my_scrolling_listener.isScrolling();


       /* if (paramAbsListView instanceof AmazingListView)
        {
            if (paramInt != 0)
                break label20;
            ((AmazingListView)paramAbsListView).isScrolling = false;
        }
        do
        {
            return;
            label20: ((AmazingListView)paramAbsListView).isScrolling = true;
        }
        while (((AmazingListView)paramAbsListView).my_scrolling_listener == null);
        ((AmazingListView)paramAbsListView).my_scrolling_listener.isScrolling();*/
    }

    public void resetPage()
    {
        this.page = this.initialPage;
    }

    void setHasMorePagesListener(HasMorePagesListener paramHasMorePagesListener)
    {
        this.hasMorePagesListener = paramHasMorePagesListener;
    }

    public void setInitialPage(int paramInt)
    {
        this.initialPage = paramInt;
    }

    public static abstract interface HasMorePagesListener
    {
        public abstract void mayHaveMorePages();

        public abstract void noMorePages();
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值