package com.weidingqiang.customlistview; import android.os.Handler; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; import java.util.ArrayList; import java.util.List; public class SwipeActivity extends AppCompatActivity { private SwipeRefreshLayout swipeRefreshLayout; private ListView listView; List<String> data; private ArrayAdapter arrayAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_swipe); listView = (ListView) this.findViewById(R.id.listview); swipeRefreshLayout = (SwipeRefreshLayout) this.findViewById(R.id.swipeRefreshLayout); initData(); //设置刷新时动画的颜色,可以设置4个 swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light); swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { //tv.setText("正在刷新"); // TODO Auto-generated method stub new Handler().postDelayed(new Runnable() { @Override public void run() { // TODO Auto-generated method stub //tv.setText("刷新完成"); swipeRefreshLayout.setRefreshing(false); } }, 2000); } }); } private void initData(){ data = new ArrayList<String>(); data.add("测试数据1"); data.add("测试数据2"); data.add("测试数据3"); data.add("测试数据1"); // data.add("测试数据2"); // data.add("测试数据3"); // data.add("测试数据1"); // data.add("测试数据2"); // data.add("测试数据3"); // data.add("测试数据1"); // data.add("测试数据2"); // data.add("测试数据3"); arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,data); listView.setAdapter(arrayAdapter); } }
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/swipeRefreshLayout" > <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent"></ListView> </android.support.v4.widget.SwipeRefreshLayout>
时间: 2024-11-02 23:24:34