android_108_PullToRefresh
2017-01-31 11:13
387 查看
效果:
布局:
旋转动画
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:drawable="@drawable/indicate_rotate"
android:toDegrees="360">
</rotate>
主界面:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.sg31.sgpullrefresh.MainActivity" >
<com.sg31.sgpullrefresh.SGRefreshListView
android:id="@+id/refreshListView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</com.sg31.sgpullrefresh.SGRefreshListView>
</RelativeLayout>
headerView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<RelativeLayout android:layout_width="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content">
<ImageView android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:id="@+id/iv_arrow"
android:background="@drawable/indicator_arrow"
android:layout_height="wrap_content"/>
<ProgressBar android:layout_width="30dp"
android:layout_centerInParent="true"
android:layout_height="30dp"
android:visibility="invisible"
android:id="@+id/pb_rotate"
android:indeterminateDuration="1000"
android:indeterminateDrawable="@drawable/indeterminate_drawable"
/>
</RelativeLayout>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:gravity="center"
android:layout_marginLeft="15dp"
android:orientation="vertical">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:id="@+id/tv_state"
android:textColor="#aa000000"
android:text="下拉刷新"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:id="@+id/tv_time"
android:textColor="@android:color/darker_gray"
android:text="最后刷新:"/>
</LinearLayout>
</LinearLayout>
footerView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<ProgressBar
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:indeterminate="true"
android:indeterminateDrawable="@drawable/indeterminate_drawable"
android:indeterminateDuration="1000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:text="加载更多..."
android:textColor="#aa000000"
android:textSize="20sp" />
</LinearLayout>
自定义ListView:
package com.sg31.sgpullrefresh;
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.view.animation.RotateAnimation;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
// 必须手动导入
import android.widget.AbsListView.OnScrollListener;;
public class SGRefreshListView extends ListView implements OnScrollListener {
private View headerView;// headerView
private ImageView iv_arrow;
private ProgressBar pb_rotate;
private TextView tv_state, tv_time;
private View footerView;
private int footerViewHeight;
private int headerViewHeight;// headerView高
private int downY;// 按下时y坐标
private final int PULL_REFRESH = 0;// 下拉刷新的状态
private final int RELEASE_REFRESH = 1;// 松开刷新的状态
private final int REFRESHING = 2;// 正在刷新的状态
private int currentState = PULL_REFRESH;
private RotateAnimation upAnimation, downAnimation;
private boolean isLoadingMore = false;// 当前是否正在处于加载更多
public SGRefreshListView(Context context) {
super(context);
init();
}
public SGRefreshListView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
setOnScrollListener(this);
initHeaderView();
initRotateAnimation();
initFooterView();
}
/**
* 初始化headerView
*/
private void initHeaderView() {
headerView = View.inflate(getContext(), R.layout.refresh_header, null);
iv_arrow = (ImageView) headerView.findViewById(R.id.iv_arrow);
pb_rotate = (ProgressBar) headerView.findViewById(R.id.pb_rotate);
tv_state = (TextView) headerView.findViewById(R.id.tv_state);
tv_time = (TextView) headerView.findViewById(R.id.tv_time);
headerView.measure(0, 0);// 主动通知系统去测量该view;
headerViewHeight = headerView.getMeasuredHeight();
headerView.setPadding(0, -headerViewHeight, 0, 0);
addHeaderView(headerView);
}
/**
* 初始化旋转动画
*/
private void initRotateAnimation() {
upAnimation = new RotateAnimation(0, -180,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
upAnimation.setDuration(300);
upAnimation.setFillAfter(true);
downAnimation = new RotateAnimation(-180, -360,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
downAnimation.setDuration(300);
downAnimation.setFillAfter(true);
}
private void initFooterView() {
footerView = View.inflate(getContext(), R.layout.refresh_footer, null);
footerView.measure(0, 0);// 主动通知系统去测量该view;
footerViewHeight = footerView.getMeasuredHeight();
// 隐藏
footerView.setPadding(0, -footerViewHeight, 0, 0);
addFooterView(footerView);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
downY = (int) ev.getY();
break;
case MotionEvent.ACTION_MOVE:
if (currentState == REFRESHING) {
break;
}
int deltaY = (int) (ev.getY() - downY);
int paddingTop = -headerViewHeight + deltaY;
if (paddingTop > -headerViewHeight
&& getFirstVisiblePosition() == 0) {
headerView.setPadding(0, paddingTop, 0, 0);
// Log.e("RefreshListView", "paddingTop: "+paddingTop);
if (paddingTop >= 0 && currentState == PULL_REFRESH) {
// 从下拉刷新进入松开刷新状态
currentState = RELEASE_REFRESH;
refreshHeaderView();
} else if (paddingTop < 0 && currentState == RELEASE_REFRESH) {
// 进入下拉刷新状态
currentState = PULL_REFRESH;
refreshHeaderView();
}
return true;// 拦截TouchMove,不让listview处理该次move事件,会造成listview无法滑动
}
break;
case MotionEvent.ACTION_UP:
if (currentState == PULL_REFRESH) {
// 隐藏headerView
headerView.setPadding(0, -headerViewHeight, 0, 0);
} else if (currentState == RELEASE_REFRESH) {
headerView.setPadding(0, 0, 0, 0);
currentState = REFRESHING;
refreshHeaderView();
if (listener != null) {
listener.onPullRefresh();
}
}
break;
}
return super.onTouchEvent(ev);
}
/**
* 根据currentState来更新headerView
*/
private void refreshHeaderView() {
switch (currentState) {
case PULL_REFRESH:
tv_state.setText("下拉刷新");
iv_arrow.startAnimation(downAnimation);
break;
case RELEASE_REFRESH:
tv_state.setText("松开刷新");
iv_arrow.startAnimation(upAnimation);
break;
case REFRESHING:
iv_arrow.clearAnimation();// 因为向上的旋转动画有可能没有执行完
iv_arrow.setVisibility(View.INVISIBLE);
pb_rotate.setVisibility(View.VISIBLE);
tv_state.setText("正在刷新...");
break;
}
}
/**
* 完成刷新操作,重置状态,在你获取完数据并更新完adater之后,去在UI线程中调用该方法
*/
public void completeRefresh() {
if (isLoadingMore) {
// 重置footerView状态
footerView.setPadding(0, -footerViewHeight, 0, 0);
isLoadingMore = false;
} else {
// 重置headerView状态
headerView.setPadding(0, -headerViewHeight, 0, 0);
currentState = PULL_REFRESH;
pb_rotate.setVisibility(View.INVISIBLE);
iv_arrow.setVisibility(View.VISIBLE);
tv_state.setText("下拉刷新");
tv_time.setText("最后刷新:" + getCurrentTime());
}
}
/**
* 获取当前系统时间,并格式化
*
* @return
*/
private String getCurrentTime() {
SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
return format.format(new Date());
}
private OnRefreshListener listener;
public void setOnRefreshListener(OnRefreshListener listener) {
this.listener = listener;
}
public interface OnRefreshListener {
void onPullRefresh();
void onLoadingMore();
}
/**
* SCROLL_STATE_IDLE:闲置状态,就是手指松开 SCROLL_STATE_TOUCH_SCROLL:手指触摸滑动,就是按着来滑动
* SCROLL_STATE_FLING:快速滑动后松开
*/
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE
&& getLastVisiblePosition() == (getCount() - 1)
&& !isLoadingMore) {
isLoadingMore = true;
footerView.setPadding(0, 0, 0, 0);// 显示出footerView
setSelection(getCount());// 让listview最后一条显示出来
if (listener != null) {
listener.onLoadingMore();
}
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
}
}
主控制器:
package com.sg31.sgpullrefresh;
import java.util.ArrayList;
import com.sg31.sgpullrefresh.SGRefreshListView.OnRefreshListener;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private SGRefreshListView refreshListView;
private ArrayList<String> list = new ArrayList<String>();
private MyAdapter adapter;
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
// 更新UI
adapter.notifyDataSetChanged();
refreshListView.completeRefresh();
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView();
initData();
}
private void initView() {
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
refreshListView = (SGRefreshListView) findViewById(R.id.refreshListView);
}
private void initData() {
for (int i = 0; i < 15; i++) {
list.add("listview原来的数据 - " + i);
}
// final View headerView = View.inflate(this, R.layout.layout_header,
// null);
// 第一种方法
// headerView.getViewTreeObserver().addOnGlobalLayoutListener(new
// OnGlobalLayoutListener() {
// @Override
// public void onGlobalLayout() {
// headerView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
// int headerViewHeight = headerView.getHeight();
//
//
// Log.e("MainActivity", "headerViewHeight: "+headerViewHeight);
// headerView.setPadding(0, -headerViewHeight, 0, 0);
// refreshListView.addHeaderView(headerView);//
// }
// });
// 第二种方法
// headerView.measure(0, 0);//主动通知系统去测量
// int headerViewHeight = headerView.getMeasuredHeight();
// Log.e("MainActivity", "headerViewHeight: "+headerViewHeight);
// headerView.setPadding(0, -headerViewHeight, 0, 0);
// refreshListView.addHeaderView(headerView);//
adapter = new MyAdapter();
refreshListView.setAdapter(adapter);
refreshListView.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onPullRefresh() {
// 需要联网请求服务器的数据,然后更新UI
requestDataFromServer(false);
}
@Override
public void onLoadingMore() {
requestDataFromServer(true);
}
});
}
/**
* 模拟向服务器请求数据
*/
private void requestDataFromServer(final boolean isLoadingMore) {
new Thread() {
public void run() {
SystemClock.sleep(3000);// 模拟请求服务器的一个时间长度
if (isLoadingMore) {
list.add("加载更多的数据-1");
list.add("加载更多的数据-2");
list.add("加载更多的数据-3");
} else {
list.add(0, "下拉刷新的数据");
}
// 在UI线程更新UI
handler.sendEmptyMessage(0);
};
}.start();
}
class MyAdapter extends BaseAdapter {
public int getCount() {
return list.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = new TextView(MainActivity.this);
textView.setPadding(20, 20, 20, 20);
textView.setTextSize(18);
textView.setText(list.get(position));
return textView;
}
}
}
布局:
旋转动画
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:drawable="@drawable/indicate_rotate"
android:toDegrees="360">
</rotate>
主界面:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.sg31.sgpullrefresh.MainActivity" >
<com.sg31.sgpullrefresh.SGRefreshListView
android:id="@+id/refreshListView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</com.sg31.sgpullrefresh.SGRefreshListView>
</RelativeLayout>
headerView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<RelativeLayout android:layout_width="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content">
<ImageView android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:id="@+id/iv_arrow"
android:background="@drawable/indicator_arrow"
android:layout_height="wrap_content"/>
<ProgressBar android:layout_width="30dp"
android:layout_centerInParent="true"
android:layout_height="30dp"
android:visibility="invisible"
android:id="@+id/pb_rotate"
android:indeterminateDuration="1000"
android:indeterminateDrawable="@drawable/indeterminate_drawable"
/>
</RelativeLayout>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:gravity="center"
android:layout_marginLeft="15dp"
android:orientation="vertical">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:id="@+id/tv_state"
android:textColor="#aa000000"
android:text="下拉刷新"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:id="@+id/tv_time"
android:textColor="@android:color/darker_gray"
android:text="最后刷新:"/>
</LinearLayout>
</LinearLayout>
footerView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<ProgressBar
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:indeterminate="true"
android:indeterminateDrawable="@drawable/indeterminate_drawable"
android:indeterminateDuration="1000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:text="加载更多..."
android:textColor="#aa000000"
android:textSize="20sp" />
</LinearLayout>
自定义ListView:
package com.sg31.sgpullrefresh;
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.view.animation.RotateAnimation;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
// 必须手动导入
import android.widget.AbsListView.OnScrollListener;;
public class SGRefreshListView extends ListView implements OnScrollListener {
private View headerView;// headerView
private ImageView iv_arrow;
private ProgressBar pb_rotate;
private TextView tv_state, tv_time;
private View footerView;
private int footerViewHeight;
private int headerViewHeight;// headerView高
private int downY;// 按下时y坐标
private final int PULL_REFRESH = 0;// 下拉刷新的状态
private final int RELEASE_REFRESH = 1;// 松开刷新的状态
private final int REFRESHING = 2;// 正在刷新的状态
private int currentState = PULL_REFRESH;
private RotateAnimation upAnimation, downAnimation;
private boolean isLoadingMore = false;// 当前是否正在处于加载更多
public SGRefreshListView(Context context) {
super(context);
init();
}
public SGRefreshListView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
setOnScrollListener(this);
initHeaderView();
initRotateAnimation();
initFooterView();
}
/**
* 初始化headerView
*/
private void initHeaderView() {
headerView = View.inflate(getContext(), R.layout.refresh_header, null);
iv_arrow = (ImageView) headerView.findViewById(R.id.iv_arrow);
pb_rotate = (ProgressBar) headerView.findViewById(R.id.pb_rotate);
tv_state = (TextView) headerView.findViewById(R.id.tv_state);
tv_time = (TextView) headerView.findViewById(R.id.tv_time);
headerView.measure(0, 0);// 主动通知系统去测量该view;
headerViewHeight = headerView.getMeasuredHeight();
headerView.setPadding(0, -headerViewHeight, 0, 0);
addHeaderView(headerView);
}
/**
* 初始化旋转动画
*/
private void initRotateAnimation() {
upAnimation = new RotateAnimation(0, -180,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
upAnimation.setDuration(300);
upAnimation.setFillAfter(true);
downAnimation = new RotateAnimation(-180, -360,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
downAnimation.setDuration(300);
downAnimation.setFillAfter(true);
}
private void initFooterView() {
footerView = View.inflate(getContext(), R.layout.refresh_footer, null);
footerView.measure(0, 0);// 主动通知系统去测量该view;
footerViewHeight = footerView.getMeasuredHeight();
// 隐藏
footerView.setPadding(0, -footerViewHeight, 0, 0);
addFooterView(footerView);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
downY = (int) ev.getY();
break;
case MotionEvent.ACTION_MOVE:
if (currentState == REFRESHING) {
break;
}
int deltaY = (int) (ev.getY() - downY);
int paddingTop = -headerViewHeight + deltaY;
if (paddingTop > -headerViewHeight
&& getFirstVisiblePosition() == 0) {
headerView.setPadding(0, paddingTop, 0, 0);
// Log.e("RefreshListView", "paddingTop: "+paddingTop);
if (paddingTop >= 0 && currentState == PULL_REFRESH) {
// 从下拉刷新进入松开刷新状态
currentState = RELEASE_REFRESH;
refreshHeaderView();
} else if (paddingTop < 0 && currentState == RELEASE_REFRESH) {
// 进入下拉刷新状态
currentState = PULL_REFRESH;
refreshHeaderView();
}
return true;// 拦截TouchMove,不让listview处理该次move事件,会造成listview无法滑动
}
break;
case MotionEvent.ACTION_UP:
if (currentState == PULL_REFRESH) {
// 隐藏headerView
headerView.setPadding(0, -headerViewHeight, 0, 0);
} else if (currentState == RELEASE_REFRESH) {
headerView.setPadding(0, 0, 0, 0);
currentState = REFRESHING;
refreshHeaderView();
if (listener != null) {
listener.onPullRefresh();
}
}
break;
}
return super.onTouchEvent(ev);
}
/**
* 根据currentState来更新headerView
*/
private void refreshHeaderView() {
switch (currentState) {
case PULL_REFRESH:
tv_state.setText("下拉刷新");
iv_arrow.startAnimation(downAnimation);
break;
case RELEASE_REFRESH:
tv_state.setText("松开刷新");
iv_arrow.startAnimation(upAnimation);
break;
case REFRESHING:
iv_arrow.clearAnimation();// 因为向上的旋转动画有可能没有执行完
iv_arrow.setVisibility(View.INVISIBLE);
pb_rotate.setVisibility(View.VISIBLE);
tv_state.setText("正在刷新...");
break;
}
}
/**
* 完成刷新操作,重置状态,在你获取完数据并更新完adater之后,去在UI线程中调用该方法
*/
public void completeRefresh() {
if (isLoadingMore) {
// 重置footerView状态
footerView.setPadding(0, -footerViewHeight, 0, 0);
isLoadingMore = false;
} else {
// 重置headerView状态
headerView.setPadding(0, -headerViewHeight, 0, 0);
currentState = PULL_REFRESH;
pb_rotate.setVisibility(View.INVISIBLE);
iv_arrow.setVisibility(View.VISIBLE);
tv_state.setText("下拉刷新");
tv_time.setText("最后刷新:" + getCurrentTime());
}
}
/**
* 获取当前系统时间,并格式化
*
* @return
*/
private String getCurrentTime() {
SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
return format.format(new Date());
}
private OnRefreshListener listener;
public void setOnRefreshListener(OnRefreshListener listener) {
this.listener = listener;
}
public interface OnRefreshListener {
void onPullRefresh();
void onLoadingMore();
}
/**
* SCROLL_STATE_IDLE:闲置状态,就是手指松开 SCROLL_STATE_TOUCH_SCROLL:手指触摸滑动,就是按着来滑动
* SCROLL_STATE_FLING:快速滑动后松开
*/
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE
&& getLastVisiblePosition() == (getCount() - 1)
&& !isLoadingMore) {
isLoadingMore = true;
footerView.setPadding(0, 0, 0, 0);// 显示出footerView
setSelection(getCount());// 让listview最后一条显示出来
if (listener != null) {
listener.onLoadingMore();
}
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
}
}
主控制器:
package com.sg31.sgpullrefresh;
import java.util.ArrayList;
import com.sg31.sgpullrefresh.SGRefreshListView.OnRefreshListener;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private SGRefreshListView refreshListView;
private ArrayList<String> list = new ArrayList<String>();
private MyAdapter adapter;
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
// 更新UI
adapter.notifyDataSetChanged();
refreshListView.completeRefresh();
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView();
initData();
}
private void initView() {
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
refreshListView = (SGRefreshListView) findViewById(R.id.refreshListView);
}
private void initData() {
for (int i = 0; i < 15; i++) {
list.add("listview原来的数据 - " + i);
}
// final View headerView = View.inflate(this, R.layout.layout_header,
// null);
// 第一种方法
// headerView.getViewTreeObserver().addOnGlobalLayoutListener(new
// OnGlobalLayoutListener() {
// @Override
// public void onGlobalLayout() {
// headerView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
// int headerViewHeight = headerView.getHeight();
//
//
// Log.e("MainActivity", "headerViewHeight: "+headerViewHeight);
// headerView.setPadding(0, -headerViewHeight, 0, 0);
// refreshListView.addHeaderView(headerView);//
// }
// });
// 第二种方法
// headerView.measure(0, 0);//主动通知系统去测量
// int headerViewHeight = headerView.getMeasuredHeight();
// Log.e("MainActivity", "headerViewHeight: "+headerViewHeight);
// headerView.setPadding(0, -headerViewHeight, 0, 0);
// refreshListView.addHeaderView(headerView);//
adapter = new MyAdapter();
refreshListView.setAdapter(adapter);
refreshListView.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onPullRefresh() {
// 需要联网请求服务器的数据,然后更新UI
requestDataFromServer(false);
}
@Override
public void onLoadingMore() {
requestDataFromServer(true);
}
});
}
/**
* 模拟向服务器请求数据
*/
private void requestDataFromServer(final boolean isLoadingMore) {
new Thread() {
public void run() {
SystemClock.sleep(3000);// 模拟请求服务器的一个时间长度
if (isLoadingMore) {
list.add("加载更多的数据-1");
list.add("加载更多的数据-2");
list.add("加载更多的数据-3");
} else {
list.add(0, "下拉刷新的数据");
}
// 在UI线程更新UI
handler.sendEmptyMessage(0);
};
}.start();
}
class MyAdapter extends BaseAdapter {
public int getCount() {
return list.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = new TextView(MainActivity.this);
textView.setPadding(20, 20, 20, 20);
textView.setTextSize(18);
textView.setText(list.get(position));
return textView;
}
}
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件