Android自帶API ,V4包下面的下拉刷新控件android
android.support.v4.widget.SwipeRefreshLayout
SwipeRefreshLayout只能包含一個控件dom
佈局例子:ide
<android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swipeRefreshLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> </android.support.v4.widget.SwipeRefreshLayout>
應用例子:佈局
/** * SwipeRefreshLayout控件學習 */ public class RefreshActicity extends AppCompatActivity { SwipeRefreshLayout swipeRefreshLayout; ListView listView; List<Product> list = new ArrayList<>(); ProductAdapter adapter; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_refresh); swipeRefreshLayout = findViewById(R.id.swipeRefreshLayout); listView = findViewById(R.id.listView); //獲取數據 initData(); //設置適配器 adapter = new ProductAdapter(this, list); listView.setAdapter(adapter); //設置下拉進度的背景顏色,默認是白色 swipeRefreshLayout.setProgressBackgroundColorSchemeResource(android.R.color.white); //設置下拉進度的肢體顏色 swipeRefreshLayout.setColorSchemeResources(R.color.colorAccent, R.color.colorPrimary, R.color.colorPrimaryDark); //下拉時觸發下拉動畫,動畫完畢後回調這個方法 swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { //開始刷新,設置當前爲刷新狀態(刷新狀態會屏蔽掉下拉事件) swipeRefreshLayout.setRefreshing(true); //耗時操做,好比聯網獲取數據 new Handler().postDelayed(new Runnable() { @Override public void run() { list.clear(); initData(); adapter.notifyDataSetChanged(); //加載完數據,設置爲不刷新狀態,將下拉進度收起來 swipeRefreshLayout.setRefreshing(false); } }, 2000); } }); } /** * 生成10條List數據 */ private void initData() { for (int i = 1; i <= 10; i++) { Random random = new Random(); Product product = new Product("100" + i, "testdata", random.nextInt(100)); list.add(product); } } }