美文网首页
解析一个Github上的项目ListViewFilter之二

解析一个Github上的项目ListViewFilter之二

作者: 二七头头 | 来源:发表于2016-03-21 00:45 被阅读56次

    代码解析

    IPinnedHeader.java

    该接口的设计目的主要是建立一个显示在顶部的标头并添加其状态属性。该状态属性主要用于辅助屏幕滚动时设置相关行为。代码的解析如下:

    import android.view.View;
    
    /**
    
     * Adapter interface. The list adapter must implement this interface.
    
     */
    
    public interface IPinnedHeader {
    
    /**
    
     * Pinned header state: don't show the header.
    
     */
    //顶部已经显示该标头时或者当ListView中item的数量为0时,不动态载入标头布局。此时顶部标头的状态为@PINNED_HEADER_GONE
    public static final int PINNED_HEADER_GONE = 0;
    
    /**
    
     * Pinned header state: show the header at the top of the list.
    
     */
    //标头的状态为可见
    public static final int PINNED_HEADER_VISIBLE = 1;
    
    /**
    
     * Pinned header state: show the header. If the header extends beyond
    
     * the bottom of the first shown element, push it up and clip.
    
     */
    //顶部的标头的下一行是另一个标头时,顶部标头的状态为@PINNED_HEADER_PUSHED_UP
    public static final int PINNED_HEADER_PUSHED_UP = 2;
    
    /**
    
     * 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}.
    
     */
    //获取顶部标头状态
    int getPinnedHeaderState(int position);
    
    /**
    
     * Configures the pinned header view to match the first visible list item.
    
     *
    
     * @param header pinned header view.
    
     * @param position position of the first visible list item.
    
     * @param alpha fading of the header view, between 0 and 255.
    
     */
    //配置顶部标头
    void configurePinnedHeader(View header, int position);
    
    }
    

    代码解析

    IIndexBarFilter.java

    该接口的设计用于辅助左边索引条的检索,按下索引条时,显示相应位置的预览字母。手指离开时,Listview显示相应位置的内容。

    public interface IIndexBarFilter {
    
    void filterList(float sideIndexY,int position,String previewText);
    
    }
    

    代码解析

    PinnedHeaderListView .java

    该类用于建立ListView,并实现顶部实现分组标头的功能。

    /*
    
     * A ListView that maintains a header pinned at the top of the list. The
    
     * pinned header can be pushed up and dissolved as needed.
    
     */
    
    public class PinnedHeaderListView extends ListView implements IIndexBarFilter {
    
    // interface object that configure pinned header view position in list view
    
    IPinnedHeader mAdapter;
    
    // view objects
    
    View mHeaderView,mIndexBarView,mPreviewTextView;
    
    // flags that decide view visibility
    
    boolean mHeaderVisibility=false;
    
    boolean mPreviewVisibility=false;
    
    // initially show index bar view with it's content
    
    boolean mIndexBarVisibility=true;
    
    // context object
    
    Context mContext;
    
    // view height and width
    
    int mHeaderViewWidth,
    
    mHeaderViewHeight,
    
    mIndexBarViewWidth,
    
    mIndexBarViewHeight,
    
    mIndexBarViewMargin,
    
    mPreviewTextViewWidth,
    
    mPreviewTextViewHeight;
    
    // touched index bar Y axis position used to decide preview text view position
    
    float mIndexBarY;
    
    public PinnedHeaderListView(Context context) {
    
    super(context);
    
    this.mContext = context;
    
    }
    
    public PinnedHeaderListView(Context context, AttributeSet attrs) {
    
    super(context, attrs);
    
    this.mContext = context;
    
    }
    
    public PinnedHeaderListView(Context context, AttributeSet attrs, int defStyle) {
    
    super(context, attrs, defStyle);
    
    this.mContext = context;
    
    }
    
    //为ListView配置适配器
    @Override
    
    public void setAdapter(ListAdapter adapter) {
    
    this.mAdapter = (PinnedHeaderAdapter)adapter;
    
    super.setAdapter(adapter);
    
    }
    
    public void setPinnedHeaderView(View headerView) {
    
    this.mHeaderView = headerView;
    
    // Disable vertical fading when the pinned header is present
    
    // TODO change ListView to allow separate measures for top and bottom fading edge;
    
    // in this particular case we would like to disable the top, but not the bottom edge.
    //FadingEdge用于暗示ListView上方或者下方还有更多内容。因为这里顶部标头滚定,所以将其长度设置为0
    if (mHeaderView != null) {
    
    setFadingEdgeLength(0);
    
    }
    
    }
    
    public void setIndexBarView(View indexBarView) {
    
    mIndexBarViewMargin = (int)mContext.getResources().getDimension(R.dimen.index_bar_view_margin);
    
    this.mIndexBarView = indexBarView;
    
    }
    
    public void setPreviewView(View previewTextView) {
    
    this.mPreviewTextView=previewTextView;
    
    }
    //重写@onMeasure方法,测量ListView中各组件的大小。
    @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();
    
    }
    
    if (mIndexBarView != null && mIndexBarVisibility) {
    
    measureChild(mIndexBarView, widthMeasureSpec, heightMeasureSpec);
    
    mIndexBarViewWidth = mIndexBarView.getMeasuredWidth();
    
    mIndexBarViewHeight = mIndexBarView.getMeasuredHeight();
    
    }
    
    if (mPreviewTextView != null && mPreviewVisibility) {
    
    measureChild(mPreviewTextView, widthMeasureSpec, heightMeasureSpec);
    
    mPreviewTextViewWidth = mPreviewTextView.getMeasuredWidth();
    
    mPreviewTextViewHeight = mPreviewTextView.getMeasuredHeight();
    
    }
    
    }
    //重写@onLayout方法,将测量后的组件放置在指定位置。
    @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());
    
    }
    
    if (mIndexBarView != null && mIndexBarVisibility) {
    
    mIndexBarView.layout(getMeasuredWidth()- mIndexBarViewMargin - mIndexBarViewWidth, mIndexBarViewMargin
    
    , getMeasuredWidth()- mIndexBarViewMargin, getMeasuredHeight()- mIndexBarViewMargin);
    
    }
    
    if (mPreviewTextView != null && mPreviewVisibility) {
    
    mPreviewTextView.layout(mIndexBarView.getLeft()-mPreviewTextViewWidth, (int)mIndexBarY-(mPreviewTextViewHeight/2)
    
    , mIndexBarView.getLeft(), (int)(mIndexBarY-(mPreviewTextViewHeight/2))+mPreviewTextViewHeight);
    
    }
    
    }
    
    public void setIndexBarVisibility(Boolean isVisible) {
    
    if(isVisible) {
    
    mIndexBarVisibility=true;
    
    }
    
    else {
    
    mIndexBarVisibility=false;
    
    }
    
    }
    
    private void setPreviewTextVisibility(Boolean isVisible) {
    
    if(isVisible) {
    
    mPreviewVisibility=true;
    
    }
    
    else {
    
    mPreviewVisibility=false;
    
    }
    
    }
    
    public void configureHeaderView(int position) {
    
    if (mHeaderView == null) {
    
    return;
    
    }
    
    int state = mAdapter.getPinnedHeaderState(position);
    
    switch (state) {
    
    case IPinnedHeader.PINNED_HEADER_GONE: 
    
    mHeaderVisibility = false;
    
    break;
    
    case IPinnedHeader.PINNED_HEADER_VISIBLE: 
    
    if (mHeaderView.getTop() != 0) {
    
    mHeaderView.layout(0, 0, mHeaderViewWidth, mHeaderViewHeight);
    
    }
    
    mAdapter.configurePinnedHeader(mHeaderView, position);
    
    mHeaderVisibility = true;
    
    break;
    
    case IPinnedHeader.PINNED_HEADER_PUSHED_UP: 
    
    View firstView = getChildAt(0);
    
    int bottom = firstView.getBottom();
    
    // int itemHeight = firstView.getHeight();
    
    int headerHeight = mHeaderView.getHeight();
    
    int y;
    
    if (bottom < headerHeight) {
    
    y = (bottom - headerHeight);
    
    }
    
    else {
    
    y = 0;
    
    }
    
    if (mHeaderView.getTop() != y) {
    
    mHeaderView.layout(0, y, mHeaderViewWidth, mHeaderViewHeight + y);
    
    }
    
    mAdapter.configurePinnedHeader(mHeaderView, position);
    
    mHeaderVisibility = true;
    
    break;
    
    }
    
    }
    
    //由于ListView是ViewGroup的子类,须重写dispathDraw方法
    @Override
    
    protected void dispatchDraw(Canvas canvas) {
    
    super.dispatchDraw(canvas);// draw list view elements (zIndex == 1)
    
    if (mHeaderView != null && mHeaderVisibility) {
    
    drawChild(canvas, mHeaderView, getDrawingTime()); // draw pinned header view (zIndex == 2)
    
    }
    
    if (mIndexBarView != null && mIndexBarVisibility) {
    
    drawChild(canvas, mIndexBarView, getDrawingTime()); // draw index bar view (zIndex == 3)
    
    }
    
    if (mPreviewTextView != null && mPreviewVisibility) {
    
    drawChild(canvas, mPreviewTextView, getDrawingTime()); // draw preview text view (zIndex == 4)
    
    }
    
    }
    
    //重写onTouchEvent方法
    @Override
    
    public boolean onTouchEvent(MotionEvent ev) {
    
    if (mIndexBarView != null && ((IndexBarView)mIndexBarView).onTouchEvent(ev)) {
    
    setPreviewTextVisibility(true);
    
    return true;
    
    }
    
    else {
    
    setPreviewTextVisibility(false);
    
    return super.onTouchEvent(ev);
    
    }
    
    }
    
    @Override
    
    public void filterList(float indexBarY, int position,String previewText) {
    
    this.mIndexBarY=indexBarY;
    
    if(mPreviewTextView instanceof TextView)
    
    ((TextView)mPreviewTextView).setText(previewText);
    
    setSelection(position);
    
    }
    
    }
    

    相关文章

      网友评论

          本文标题:解析一个Github上的项目ListViewFilter之二

          本文链接:https://www.haomeiwen.com/subject/drdklttx.html