您的位置:首页 > 其它

自定义ListView实现下拉刷新

2016-02-04 11:43 295 查看
第一次刷新:



第二次刷新:



ListView上面的header文件布局:

<?xml version="1.0" encoding="utf-8"?>

<!-- ListView的头部 -->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:background="#00000000" >

    <!-- 内容 -->

    <RelativeLayout

        android:id="@+id/head_contentLayout"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:paddingLeft="30dp" >

        <!-- 箭头图像、进度条 -->

        <FrameLayout

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_alignParentLeft="true"

            android:layout_centerVertical="true" >

            <!-- 箭头 -->

            <ImageView

                android:id="@+id/iv_arrow"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_gravity="center"

                android:src="@drawable/arrow" />

            <!-- 进度条 -->

            <ProgressBar

                android:id="@+id/progressBar"

                style="?android:attr/progressBarStyleSmall"

                android:layout_width="50dp"

                android:layout_height="50dp"

                android:layout_gravity="center"

                android:visibility="gone" />

        </FrameLayout>

        <!-- 提示、最近更新 -->

        <LinearLayout

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_centerHorizontal="true"

            android:gravity="center_horizontal"

            android:orientation="vertical" >

            <!-- 提示 -->

            <TextView

                android:id="@+id/tv_state"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="下拉刷新"

                android:textColor="#FF000000"

                android:textSize="20sp" />

            <!-- 最近更新 -->

            <TextView

                android:id="@+id/tv_updateTime"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text=""

                android:textColor="#FFFF0000"

                android:textSize="10sp" />

        </LinearLayout>

    </RelativeLayout>

</LinearLayout>

自定义ListView:

package com.example.customlistview;

import java.text.SimpleDateFormat;

import java.util.Date;

import android.content.Context;

import android.util.AttributeSet;

import android.view.MotionEvent;

import android.view.View;

import android.widget.ImageView;

import android.widget.ListView;

import android.widget.ProgressBar;

import android.widget.TextView;

public class CustomListView extends ListView{
View header;
TextView tvState;
TextView upDateTime;
ProgressBar progressBar;
int viewHeight;
ImageView ivArror;
int downY;
byte currentState;
public static final byte STATE_DONE=0;
public static final byte STATE_PULL=1;
public static final byte STATE_RELEASE=2;
public static final byte STATE_REFRESHING=3;

    String time="";
OnRefreshListener onRefreshListener;

public void setOnRefershListener(OnRefreshListener onRefreshListener){
this.onRefreshListener=onRefreshListener;
}
public CustomListView(Context context, AttributeSet attrs) {
super(context, attrs);
currentState=STATE_DONE;
header=View.inflate(context, R.layout.list_header,null);
tvState=(TextView) header.findViewById(R.id.tv_state);
progressBar=(ProgressBar) header.findViewById(R.id.progressBar);
ivArror=(ImageView) header.findViewById(R.id.iv_arrow);
upDateTime=(TextView) header.findViewById(R.id.tv_updateTime);
this.addHeaderView(header);
header.measure(0, 0);
viewHeight=header.getMeasuredHeight();
//header.setPadding(left, top, right, bottom);
header.setPadding(0, -viewHeight, 0, 0);
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
int action=ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:

             if(this.currentState==STATE_DONE){

            this.currentState=STATE_PULL;

            this.downY=(int) ev.getY();

             }
break;

case MotionEvent.ACTION_MOVE:

             if(currentState==STATE_PULL){

            int moveY=(int) ev.getY();

            int top=-viewHeight+moveY-downY;

            header.setPadding(0, top, 0, 0);

            if(moveY-downY>viewHeight){

            this.currentState=STATE_RELEASE;

            tvState.setText("松开刷新");

            if("".equals(time)){

            upDateTime.setText(time); 

            }else{

            upDateTime.setText("上次刷新:"+time);

            }

            }

             }
break;
case MotionEvent.ACTION_UP:

            if(currentState==STATE_RELEASE){

            currentState=STATE_REFRESHING;

            tvState.setText("刷新中");

            ivArror.setVisibility(View.GONE);

            progressBar.setVisibility(View.VISIBLE);

            if (onRefreshListener != null) {
onRefreshListener.onRefresh(this);
}

            }
break;
}
return super.onTouchEvent(ev);
}

   public String getDate(){
  SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
  String date=sdf.format(new Date());
  return date;

   }

   public void refreshComplete(){
  progressBar.setVisibility(View.GONE);
  ivArror.setVisibility(View.VISIBLE);
  tvState.setText("下拉刷新");
  time=getDate();
  upDateTime.setText("本次刷新:"+time);
  header.setPadding(0 , -viewHeight, 0, 0);

       currentState=STATE_DONE;

       

   }

   interface OnRefreshListener{
  public void onRefresh(CustomListView customListView);

   }

}

MainActivity:

public class MainActivity extends Activity {
CustomListView customListView;
ArrayList<String> list=new ArrayList<String>();
MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpView();
}
private void setUpView() {
customListView=(CustomListView) findViewById(R.id.customView);
list.add("dadwa");list.add("dadfewwa");
list.add("dfwadwa");list.add("dafewedwa");
list.add("datrdwa");list.add("da4t3dwa");
adapter=new MyAdapter(this, list);
customListView.setAdapter(adapter);
customListView.setOnRefershListener(new OnRefreshListener() {
@Override
public void onRefresh(final CustomListView customListView) {
//模拟联网刷新
new Thread(){
public void run() {
try {
this.currentThread().sleep(5000);
for(int i=0;i<10;i++){
list.add(""+i);
}
runOnUiThread(new Runnable() {
public void run() {
adapter.notifyDataSetChanged();
customListView.refreshComplete();
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
};
}.start();
}
});
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: