主要方法:
设置刷新简体器: setOnRefreshListener() --> SwipeRefreshLayout.OnRefreshListener
1. isRefreshing()
2. setColorSchema()
3. onRefresh()
1. Layout - activity_main.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.mark.swiperefreshlayoutdemo.MainActivity" > <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> </RelativeLayout>
2. Java - MainActivity.xml
public class MainActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener { private static final int REFRESH_COMPLETE = 0X01; private SwipeRefreshLayout mSwipeLayout; private ListView mListView; private ArrayAdapter<String> mAdapter; private List<String> mDatas = new ArrayList<String>(Arrays.asList("Java", "Javascript", "C++", "Ruby", "Json", "HTML")); private Handler mHandler = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case REFRESH_COMPLETE: mDatas.addAll(Arrays.asList("Refresh-1", "Refresh-2", "Refresh-3")); mAdapter.notifyDataSetChanged(); mSwipeLayout.setRefreshing(false); break; } }; }; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mListView = (ListView) findViewById(R.id.listView); mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout); mSwipeLayout.setOnRefreshListener(this); mSwipeLayout.setColorScheme( android.R.color.holo_blue_bright, android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_red_light); mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mDatas); mListView.setAdapter(mAdapter); } public void onRefresh() { mHandler.sendEmptyMessageDelayed(REFRESH_COMPLETE, 2000); } }
时间: 2024-10-29 19:11:25