自定義ExpandableListView下拉重新整理功能簡單實現(這裡主要說自定義可下拉的功能)
ExpandableListView 筆記 ExpandableListVivew 是 ListView 的子類,它在普通 ListView 的基礎上進行了擴充套件, 它把應用中的列表項分為幾組,每組裡又可包含多個列表項。 ExpandableListVivew 的用法 與普通 ListView 的用法非常相似,只是 ExpandableListVivew 顯示的列表項應該由 ExpandableAdapter 提供。
自定義的類如下:
package com.xing.wifichat.view;
import android.R.integer;
import android.content.Context;
import android.content.DialogInterface;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.xing.wifichat.R;
public class MainExpandableListView extends ExpandableListView implements
OnScrollListener {
@Override
public void setAdapter(ListAdapter adapter) {
// TODO Auto-generated method stub
super.setAdapter(adapter);
}
//鬆開重新整理
private final static int RELEASE_TO_REFRESH=0;//
private final static int PULL_TO_REFRESH=1;//下拉重新整理
private final static int REFRESHING =2;//正在重新整理
private final static int DONE = 3;
private final static int RATIO=3;//實際的padding的距離與介面 上偏移距離 的比例
private LayoutInflater inflater;
private LinearLayout headLayout;//頭linearlayout
private TextView tipsTextview;
private TextView lastUpdatedTextView;
private ImageView arrowImageView;//箭頭的圖示
private ProgressBar progressBar;
private RotateAnimation animation;
// 反轉動畫
private RotateAnimation reverseAnimation;
private int headContentWidth;//頭部的寬度
private LinearLayout headView;
private int headContentHeight;
/** 手勢按下的起點位置 */
private int startY;
private int firstItemIndex;
private int state;
private boolean isBack;
//private OnRefreshListener refreshListener;
private boolean isRefreshable;
public MainExpandableListView(Context context) {
super(context);
// TODO Auto-generated constructor stub
init(context);
}
public MainExpandableListView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
init(context);
}
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
public void init(Context context)
{
inflater =LayoutInflater.from(context);
headView =(LinearLayout) inflater.inflate(R.layout.refresh_head, null);
arrowImageView = (ImageView) headView.findViewById(R.id.head_arrowImageView);//箭頭
arrowImageView.setMinimumWidth(70);
arrowImageView.setMinimumHeight(50);
progressBar = (ProgressBar) headView.findViewById(R.id.head_progressBar);
tipsTextview = (TextView) headView.findViewById(R.id.head_tipsTextView);
lastUpdatedTextView = (TextView) headView.findViewById(R.id.head_lastUpdatedTextView);
headView.measure(0, 0);
headContentHeight=headView.getMeasuredHeight();
headContentWidth=headView.getMeasuredWidth();
headView.setPadding(0, -headContentHeight, 0, 0);//把headview隱藏到頂部
headView.invalidate();//重新整理介面
addHeaderView(headView,null,false);
setOnScrollListener(this);//滾動監聽
animation = new RotateAnimation(0, -180, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
animation.setInterpolator(new LinearInterpolator());
animation.setDuration(250);
animation.setFillAfter(true);
reverseAnimation = new RotateAnimation(-180, 0, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
reverseAnimation.setInterpolator(new LinearInterpolator());
reverseAnimation.setDuration(200);
reverseAnimation.setFillAfter(true);
state = DONE;
isRefreshable = false;
}
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
firstVisibleItem=firstVisibleItem;
}
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
/**
* 設定觸控事件 總的思路就是
*
* 1 ACTION_DOWN:記錄起始位置
*
* 2 ACTION_MOVE:計算當前位置與起始位置的距離,來設定state的狀態
*
* 3 ACTION_UP:根據state的狀態來判斷是否下載
*/
public boolean onTouchEvent(MotionEvent event)
{
isRefreshable = true;
if (isRefreshable) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN://按下螢幕
System.out.println("按下屏");
if (firstItemIndex==0) {
startY=(int)event.getY();
System.out.println("記錄down下當前的位置");
}
break;
case MotionEvent.ACTION_MOVE: //移動螢幕
System.out.println("移動下屏");
int tempY=(int)event.getY();
if (state ==PULL_TO_REFRESH) {
setSelection(0);//很重要
//下拉到可以release_to_refresh的狀態
if ((tempY-startY)/RATIO>=headContentHeight) {
state=RELEASE_TO_REFRESH;
isBack=true;
changeHeaderViewByState();
}
//上推到頂了
else if(tempY-startY<=0){
state =DONE;
changeHeaderViewByState();
}
headView.setPadding(0, -headContentHeight + (tempY - startY) / RATIO, 0, 0);
}
if (state == RELEASE_TO_REFRESH) {
setSelection(0);
// 往上推了,推到了螢幕足夠掩蓋head的程度,但是還沒有推到全部掩蓋的地步
if (((tempY - startY) / RATIO < headContentHeight) && (tempY - startY) > 0) {
state = PULL_TO_REFRESH;
changeHeaderViewByState();
// Log.v(TAG, "由鬆開重新整理狀態轉變到下拉重新整理狀態");
}
headView.setPadding(0, -headContentHeight + (tempY - startY) / RATIO, 0, 0);
}
// done狀態下
if (state == DONE) {
if (tempY - startY > 0) {
state = PULL_TO_REFRESH;
changeHeaderViewByState();
}
}
break;
case MotionEvent.ACTION_UP:
System.out.println("ACTION_UP");
if (state != REFRESHING) {
// 不在重新整理狀態
if (state == PULL_TO_REFRESH) {
state = DONE;
changeHeaderViewByState();
// Log.v(TAG, "下拉重新整理狀態,到done狀態");
}
if (state == RELEASE_TO_REFRESH) {
state = REFRESHING;
changeHeaderViewByState();
isRefreshable = true;
// Log.v(TAG, "鬆開重新整理狀態,到done狀態");
}
}
isBack = false;
break;
}
}
return super.onTouchEvent(event);
}
//當狀態改變時候,呼叫 該方法,以更新介面
private void changeHeaderViewByState()
{
switch (state) {
case RELEASE_TO_REFRESH:
arrowImageView.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE);
tipsTextview.setVisibility(View.VISIBLE);
arrowImageView.clearAnimation();
arrowImageView.startAnimation(animation);
tipsTextview.setText("鬆開重新整理");
// Log.v(TAG, "當前狀態,鬆開重新整理");
break;
case PULL_TO_REFRESH:
progressBar.setVisibility(View.GONE);
tipsTextview.setVisibility(View.VISIBLE);
arrowImageView.clearAnimation();
arrowImageView.setVisibility(View.VISIBLE);
tipsTextview.setText("下拉重新整理");
// 是RELEASE_To_REFRESH狀態轉變來的
if (isBack) {
isBack = false;
arrowImageView.startAnimation(reverseAnimation);
}
// Log.v(TAG, "當前狀態,下拉重新整理");
break;
case REFRESHING:
headView.setPadding(0, 0, 0, 0);
progressBar.setVisibility(View.VISIBLE);
arrowImageView.clearAnimation();
arrowImageView.setVisibility(View.GONE);
tipsTextview.setText("正在重新整理...");
break;
case DONE:
headView.setPadding(0, -headContentHeight, 0, 0);
progressBar.setVisibility(View.GONE);
arrowImageView.clearAnimation();
arrowImageView.setImageResource(R.drawable.arrow);
tipsTextview.setText("下拉重新整理");
break;
}
}
}
原始碼可以執行!!!!!!!!!!!!!!!!!