解析一個(gè)Github上的項(xiàng)目ListViewFilter之三

代碼解析

IndexBarView.java

該接口的設(shè)計(jì)目的主要是建立一個(gè)自定義View-屏幕左邊的檢索條。代碼的解析如下:

public class IndexBarView extends View {

// index bar margin
//索引條周邊留白的寬度
float mIndexbarMargin;

// user touched Y axis coordinate value
//用戶觸摸索引條的位置
float mSideIndexY;

// flag used in touch events manipulations
//是否正在索引的標(biāo)志
boolean mIsIndexing = false;

// holds current section position selected by user

int mCurrentSectionPosition = -1;

// array list to store section positions

public ArrayList<Integer> mListSections;

// array list to store listView data

ArrayList<String> mListItems;

// paint object

Paint mIndexPaint;

// context object

Context mContext;

// interface object used as bridge between list view and index bar view for

// filtering list view content on touch event

IIndexBarFilter mIndexBarFilter;

public IndexBarView(Context context) {

super(context);

this.mContext = context;

}

public IndexBarView(Context context, AttributeSet attrs) {

super(context, attrs);

this.mContext = context;

}

public IndexBarView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

this.mContext = context;

}

public void setData(PinnedHeaderListView listView, ArrayList<String> listItems,ArrayList<Integer> listSections) {

this.mListItems = listItems;

this.mListSections = listSections;

// list view implements mIndexBarFilter interface

mIndexBarFilter = listView;

// set index bar margin from resources

mIndexbarMargin = mContext.getResources().getDimension(R.dimen.index_bar_view_margin);

// index bar item color and text size

mIndexPaint = new Paint();

mIndexPaint.setColor(mContext.getResources().getColor(R.color.color_black));

mIndexPaint.setAntiAlias(true);

mIndexPaint.setTextSize(mContext.getResources().getDimension(R.dimen.index_bar_view_text_size));

}

//重寫View的onDraw方法
// draw view content on canvas using paint

@Override

protected void onDraw(Canvas canvas) {

if (mListSections != null && mListSections.size() > 1) {

//索引條每個(gè)字母的占據(jù)的高度
float sectionHeight = (getMeasuredHeight() - 2 * mIndexbarMargin)/ mListSections.size();

//索引條字母離頂部邊框的距離
float paddingTop = (sectionHeight - (mIndexPaint.descent() - mIndexPaint.ascent())) / 2;

for (int i = 0; i < mListSections.size(); i++) {

float paddingLeft = (getMeasuredWidth() - mIndexPaint.measureText(getSectionText(mListSections.get(i)))) / 2;

canvas.drawText(getSectionText(mListSections.get(i)),

paddingLeft,

mIndexbarMargin + (sectionHeight * i) + paddingTop + mIndexPaint.descent(),

mIndexPaint);

}

}

super.onDraw(canvas);

}

public String getSectionText(int sectionPosition) {

return mListItems.get(sectionPosition);

}

boolean contains(float x, float y) {

// Determine if the point is in index bar region, which includes the

// right margin of the bar

return (x >= getLeft() && y >= getTop() && y <= getTop() + getMeasuredHeight());

}

void filterListItem(float sideIndexY) {

mSideIndexY = sideIndexY;

// filter list items and get touched section position with in index bar

mCurrentSectionPosition = (int) (((mSideIndexY) - getTop() - mIndexbarMargin) /

((getMeasuredHeight() - (2 * mIndexbarMargin)) / mListSections.size()));

if (mCurrentSectionPosition >= 0 && mCurrentSectionPosition < mListSections.size()) {

int position = mListSections.get(mCurrentSectionPosition);

String previewText = mListItems.get(position);

mIndexBarFilter.filterList(mSideIndexY, position, previewText);

}

}

public boolean onTouchEvent(MotionEvent ev) {

switch (ev.getAction()) {

case MotionEvent.ACTION_DOWN:

// If down event occurs inside index bar region, start indexing

if (contains(ev.getX(), ev.getY())) {

// It demonstrates that the motion event started from index

// bar

mIsIndexing = true;

// Determine which section the point is in, and move the

// list to

// that section

filterListItem(ev.getY());

return true;

}

else {

mCurrentSectionPosition = -1;

return false;

}

case MotionEvent.ACTION_MOVE:

if (mIsIndexing) {

// If this event moves inside index bar

if (contains(ev.getX(), ev.getY())) {

// Determine which section the point is in, and move the

// list to that section

filterListItem(ev.getY());

return true;

}

else {

mCurrentSectionPosition = -1;

return false;

}

}

break;

case MotionEvent.ACTION_UP:

if (mIsIndexing) {

mIsIndexing = false;

mCurrentSectionPosition = -1;

}

break;

}

return false;

}

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,466評(píng)論 25 708
  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程,因...
    小菜c閱讀 6,547評(píng)論 0 17
  • 小倩向我哭訴著交往了三年的男朋友因?yàn)槌鰢?guó)要和她分手。他說他沒時(shí)間,竟然連最后一面都不肯見我就走了。哪怕只見十分八分...
    半島同學(xué)閱讀 1,214評(píng)論 9 9
  • 女兒放下書包就出去了,說跟兩個(gè)好友約好了去趟書城,七點(diǎn)左右就會(huì)回來。雖然知道她們可不是僅僅是去書城,她卻并不會(huì)說什...
    安之若零閱讀 682評(píng)論 11 5
  • 建造一所房子 就在我的竹林 讓竹林的清風(fēng)吹散過往的塵囂 讓竹林的雨露洗滌莫需的煩躁 將一顆寧靜的心 放在屬于我的這...
    一念牽心閱讀 129評(píng)論 0 0