android listview 下拉更新的简单例子
package com.river.app.dropdownlistviewdemo;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import java.util.Arrays;
import java.util.LinkedList;
public class DropDownListViewDemo extends Activity {
private LinkedList<String> listItems = null;
private DropDownListView listView = null;
private ArrayAdapter<String> adapter;
private String[] mStrings = {"11111", "2222", "3333",
"4444", "5555", "6666", "77777", "8888", "99999",
"aaaaa", "bbbb", "cccc", "dddd", "eeee","11111", "2222", "3333",
"4444", "5555", "6666", "77777", "8888", "99999",
"aaaaa", "bbbb", "cccc", "dddd", "eeee"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drop_down_list_view_demo);
listView = (DropDownListView)findViewById(R.id.list_view);
listView.setOnDropDownListener(new DropDownListView.OnDropDownListener() {
@Override
public void onDropDown() {
new GetDataTask(true).execute();
}
});
listItems = new LinkedList<String>();
listItems.addAll(Arrays.asList(mStrings));
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
listView.setAdapter(adapter);
}
private class GetDataTask extends AsyncTask<Void, Void, String[]> {
private boolean isDropDown;
public GetDataTask(boolean isDropDown) {
this.isDropDown = isDropDown;
}
@Override
protected String[] doInBackground(Void... params) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
return mStrings;
}
@Override
protected void onPostExecute(String[] result) {
if (isDropDown) {
listItems.addFirst("add a item");
adapter.notifyDataSetChanged();
listView.onDropDownComplete();
}
super.onPostExecute(result);
}
}
}
package com.river.app.dropdownlistviewdemo;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class DropDownListView extends ListView {
private Context context;
private RelativeLayout headerLayout;
private ProgressBar headerProgressBar;
private TextView tv1;
private OnDropDownListener onDropDownListener;
float myx;
public DropDownListView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
this.context = context;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
headerLayout = (RelativeLayout) inflater.inflate(R.layout.drop_down_list_header, this, false);
headerProgressBar = (ProgressBar) headerLayout.findViewById(R.id.drop_down_list_header_progress_bar);
tv1=(TextView) headerLayout.findViewById(R.id.drop_down_list_header_default_text);
}
public void setOnDropDownListener(OnDropDownListener onDropDownListener) {
this.onDropDownListener = onDropDownListener;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
myx = event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
float y = event.getRawY();
if (getFirstVisiblePosition() == 0 && y - myx > 500) {
addHeaderView(headerLayout);
onDropDown();
}
break;
}
return super.onTouchEvent(event);
}
public void onDropDown() {
if (onDropDownListener != null) {
headerProgressBar.setVisibility(View.VISIBLE);
tv1.setVisibility(View.VISIBLE);
setSelection(0);
onDropDownListener.onDropDown();
}
}
public void onDropDownComplete() {
headerProgressBar.setVisibility(View.GONE);
tv1.setVisibility(View.GONE);
}
public interface OnDropDownListener {
public void onDropDown();
}
}
源码:https://download.csdn.net/download/szwyxriver/15156044