您的位置:首页 > 其它

PullreFreshLibrary(PullToRefreshListView)上拉刷新,下拉加载(—)

2016-05-31 14:48 405 查看
MainActivity.xml

第三方架包下载地址:https://github.com/chrisbanes/Android-PullToRefresh

解压后使用其中的library

<com.handmark.pulltorefresh.library.PullToRefreshListView
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="@+id/expand_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ptr:ptrDrawable="@drawable/indicator_bg_top"
ptr:ptrAnimationStyle="flip"
ptr:ptrHeaderBackground="#383838"
ptr:ptrHeaderTextColor="#FFFFFF"
/>


MainActivity.class

import java.util.ArrayList;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity  {

private PullToRefreshListView mExpandList;
private ArrayList<String> list;
private ArrayAdapter<String> adapter;
Handler hand=new Handler(){
public void handleMessage(android.os.Message msg) {
if(msg.what==0){
adapter.notifyDataSetChanged();
mExpandList.onRefreshComplete();
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState)  {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = new ArrayList<String>();
getData();
mExpandList = (PullToRefreshListView)  findViewById(R.id.expand_list);
mExpandList.setMode(Mode.BOTH);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, list);
mExpandList.setAdapter(adapter);
mExpandList.setOnRefreshListener(new OnRefreshListener2<ListView>()  {

@Override
public void onPullDownToRefresh(PullToRefreshBase refreshView) {
// TODO Auto-generated method stub
list.clear();
getData();
hand.sendEmptyMessageDelayed(0, 2000);
}

@Override
public void onPullUpToRefresh(PullToRefreshBase refreshView) {
// TODO Auto-generated method stub
getData();
hand.sendEmptyMessageDelayed(0, 2000);
}
});
}

private void getData() {
for(int i=0;i<10;i++){
list.add(i+"");
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息