新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > Android常用控件之下拉刷新Wifi列表

Android常用控件之下拉刷新Wifi列表

作者: 時(shí)間:2016-09-12 來(lái)源:網(wǎng)絡(luò) 收藏

isRefreshable = false;

}

@Override

public void onScroll(AbsListView arg0, int firstVisiableItem, int arg2,

int arg3) {

// TODO Auto-generated method stub

firstItemIndex = firstVisiableItem;

}

@Override

public void onScrollStateChanged(AbsListView view, int scrollState) {

// TODO Auto-generated method stub

}

public boolean onTouchEvent(MotionEvent event) {

if (isRefreshable) {

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:

if (firstItemIndex == 0 !isRecored) {

isRecored = true;

startY = (int) event.getY();

Log.v(TAG, 在down時(shí)候記錄當(dāng)前位置‘);

}

break;

case MotionEvent.ACTION_UP:

if (state != REFRESHING state != LOADING) {

if (state == DONE) {

// 什么都不做

}

if (state == PULL_To_REFRESH) {

state = DONE;

changeHeaderViewByState();

Log.v(TAG, 由下拉刷新?tīng)顟B(tài),到done狀態(tài));

}

if (state == RELEASE_To_REFRESH) {

state = REFRESHING;

changeHeaderViewByState();

onRefresh();

Log.v(TAG, 由松開(kāi)刷新?tīng)顟B(tài),到done狀態(tài));

}

}

isRecored = false;

isBack = false;

break;

case MotionEvent.ACTION_MOVE:

int tempY = (int) event.getY();

if (!isRecored firstItemIndex == 0) {

Log.v(TAG, 在move時(shí)候記錄下位置);

isRecored = true;

startY = tempY;

}

if (state != REFRESHING isRecored state != LOADING) {

// 保證在設(shè)置padding的過(guò)程中,當(dāng)前的位置一直是在head,否則如果當(dāng)列表超出屏幕的話,當(dāng)在上推的時(shí)候,列表會(huì)同時(shí)進(jìn)行滾動(dòng)

// 可以松手去刷新了

if (state == RELEASE_To_REFRESH) {

setSelection(0);

// 往上推了,推到了屏幕足夠掩蓋head的程度,但是還沒(méi)有推到全部掩蓋的地步

if (((tempY - startY) / RATIO headContentHeight)

(tempY - startY) > 0) {

state = PULL_To_REFRESH;

changeHeaderViewByState();

Log.v(TAG, 由松開(kāi)刷新?tīng)顟B(tài)轉(zhuǎn)變到下拉刷新?tīng)顟B(tài));

}

// 一下子推到頂了

else if (tempY - startY = 0) {

state = DONE;

changeHeaderViewByState();

Log.v(TAG, 由松開(kāi)刷新?tīng)顟B(tài)轉(zhuǎn)變到done狀態(tài));

}

// 往下拉了,或者還沒(méi)有上推到屏幕頂部掩蓋head的地步

else {

// 不用進(jìn)行特別的操作,只用更新paddingTop的值就行了

}

}

// 還沒(méi)有到達(dá)顯示松開(kāi)刷新的時(shí)候,DONE或者是PULL_To_REFRESH狀態(tài)

if (state == PULL_To_REFRESH) {

setSelection(0);

// 下拉到可以進(jìn)入RELEASE_TO_REFRESH的狀態(tài)

if ((tempY - startY) / RATIO >= headContentHeight) {

state = RELEASE_To_REFRESH;

isBack = true;

changeHeaderViewByState();

Log.v(TAG, 由done或者下拉刷新?tīng)顟B(tài)轉(zhuǎn)變到松開(kāi)刷新);

}

// 上推到頂了

else if (tempY - startY = 0) {

state = DONE;

changeHeaderViewByState();

Log.v(TAG, 由DOne或者下拉刷新?tīng)顟B(tài)轉(zhuǎn)變到done狀態(tài));

}

}

// done狀態(tài)下

if (state == DONE) {

if (tempY - startY > 0) {

state = PULL_To_REFRESH;

changeHeaderViewByState();

}

}

// 更新headView的size

if (state == PULL_To_REFRESH) {

headView.setPadding(0, -1 * headContentHeight

+ (tempY - startY) / RATIO, 0, 0);

}

// 更新headView的paddingTop

if (state == RELEASE_To_REFRESH) {

headView.setPadding(0, (tempY - startY) / RATIO

- headContentHeight, 0, 0);

}

}

break;

}

}

return super.onTouchEvent(event);

}

/**

* 當(dāng)狀態(tài)改變時(shí)候,調(diào)用該方法,以更新界面

*/

private void changeHeaderViewByState() {

switch (state) {

case RELEASE_To_REFRESH:

arrowImageView.setVisibility(View.VISIBLE);

progressBar.setVisibility(View.GONE);

tipsTextview.setVisibility(View.VISIBLE);

lastUpdatedTextView.setVisibility(View.VISIBLE);

arrowImageView.clearAnimation();

arrowImageView.startAnimation(animation);

tipsTextview.setText(松開(kāi)刷新);

Log.v(TAG, 當(dāng)前狀態(tài),松開(kāi)刷新);

break;

case PULL_To_REFRESH:

progressBar.setVisibility(View.GONE);

tipsTextview.setVisibility(View.VISIBLE);

lastUpdatedTextView.setVisibility(View.VISIBLE);

arrowImageView.clearAnimation();

arrowImageView.setVisibility(View.VISIBLE);

// 是由RELEASE_To_REFRESH狀態(tài)轉(zhuǎn)變來(lái)的

if (isBack) {

isBack = false;

arrowImageView.clearAnimation();

arrowImageView.startAnimation(reverseAnimation);

tipsTextview.setText(下拉刷新);

} else {

tipsTextview.setText(下拉刷新);

}

Log.v(TAG, 當(dāng)前狀態(tài),下拉刷新);

break;

case REFRESHING:



關(guān)鍵詞:

評(píng)論


相關(guān)推薦

技術(shù)專(zhuān)區(qū)

關(guān)閉