Github의 항목ListViewFilter 2 확인

8274 단어

코드 해석


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);

}

}

좋은 웹페이지 즐겨찾기