github: https://github.com/liaohuqiu/android-Ultra-Pull-To-Refresh
<in.srain.cube.views.ptr.PtrFrameLayout android:id="@+id/ptr" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent"></ListView> </in.srain.cube.views.ptr.PtrFrameLayout>
public class MainActivity extends AppCompatActivity { private List<String> list; private ArrayAdapter<String> adapter; private PtrFrameLayout ptrFrameLayout; private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { //设置刷新完毕,取消刷新动画 ptrFrameLayout.refreshComplete(); adapter.notifyDataSetChanged(); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ptrFrameLayout = (PtrFrameLayout) findViewById(R.id.ptr); ListView listView = (ListView) findViewById(R.id.lv); initData(); //1.默认经典头布局 // PtrClassicDefaultHeader defaultHeader = new PtrClassicDefaultHeader(this); // 给Ptr添加头布局 // ptrFrameLayout.setHeaderView(defaultHeader); // 使头布局的状态和刷新状态同步 // ptrFrameLayout.addPtrUIHandler(defaultHeader); //2.模仿MD风格的头布局 // MaterialHeader materialHeader = new MaterialHeader(this); //设置MD风格动画的颜色 // materialHeader.setColorSchemeColors(new int[]{Color.RED, Color.GREEN, Color.BLUE}); // ptrFrameLayout.setHeaderView(materialHeader); // ptrFrameLayout.addPtrUIHandler(materialHeader); //3.闪动文字的头布局 StoreHouseHeader storeHouseHeader = new StoreHouseHeader(this); //设置加载数据时显示的字符串,文本只支持0-9,英文字符 storeHouseHeader.initWithString("999Loading..."); //设置头布局的背景颜色 storeHouseHeader.setBackgroundColor(Color.BLACK); //设置文本的颜色 storeHouseHeader.setTextColor(Color.WHITE); // storeHouseHeader.setDropHeight(300); ptrFrameLayout.setHeaderView(storeHouseHeader); ptrFrameLayout.addPtrUIHandler(storeHouseHeader); adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list); listView.setAdapter(adapter); //设置下拉刷新监听 ptrFrameLayout.setPtrHandler(new PtrDefaultHandler() { //在这里进行数据加载 @Override public void onRefreshBegin(PtrFrameLayout frame) { new Thread(new Runnable() { @Override public void run() { list.add(0, "王五"); SystemClock.sleep(3000); mHandler.sendEmptyMessage(0); } }).start(); } }); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity.this, "position:" + position, Toast.LENGTH_SHORT).show(); } }); // ptrFrameLayout.setPtrHandler(new PtrHandler() { //返回控件是否可以执行下拉刷新操作 // @Override // public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) { // return true; // } // // @Override // public void onRefreshBegin(PtrFrameLayout frame) { // // } // }); } private void initData() { list = new ArrayList<>(); for (int i = 0; i < 30; i++) { list.add("lisi:" + i); } } }