效果图:

Android  ListView下拉与上拉刷新加载更多(一)_xml                 Android  ListView下拉与上拉刷新加载更多(一)_xml_02


java代码中的实现:



public class MainActivity extends ActionBarActivity implements WaterDropListView.IWaterDropListViewListener  {

private WaterDropListView waterDropListView;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 1:
waterDropListView.stopRefresh();
break;
case 2:
waterDropListView.stopLoadMore();
break;
}

}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
waterDropListView = (WaterDropListView) findViewById(R.id.waterdrop_listview);
waterDropListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, getData()));
waterDropListView.setWaterDropListViewListener(this);
waterDropListView.setPullLoadEnable(true);
}

private List<String> getData(){
List<String> data = new ArrayList<String>();
for (int i = 0; i < 30; i++) {
data.add("this is a test");
}
return data;
}
@Override
public void onRefresh() {
ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.execute(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
handler.sendEmptyMessage(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});

}

@Override
public void onLoadMore() {
ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.execute(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
handler.sendEmptyMessage(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
}


activity_activity.xml中


<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.example.listviewreflesdemo.MainActivity" >
<com.example.listviewreflesdemo.view.WaterDropListView
android:id="@+id/waterdrop_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.example.listviewreflesdemo.view.WaterDropListView>

</RelativeLayout>