方式一、Google 官方控件 SwipeRefreshLayout
-
xml 布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <!--使用SwipeRefreshLayout包裹ListView控件--> <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/srl_main" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/lv_main" android:layout_width="match_parent" android:layout_height="match_parent"></ListView> </android.support.v4.widget.SwipeRefreshLayout> </LinearLayout>
-
Activity 中使用
boolean isRefreshing()
:刷新控件状态(是否刷新中)public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener { private SwipeRefreshLayout srl; private ListView lv; private ArrayAdapter<String> adapter; private List<String> datas = new ArrayList<>(Arrays.asList("aa", "bb", "cc")); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); srl = (SwipeRefreshLayout) findViewById(R.id.srl_main); lv = (ListView) findViewById(R.id.lv_main); srl.setColorSchemeColors(Color.RED); // 刷新控件的颜色 srl.setOnRefreshListener(this); adapter = new ArrayAdapter<>(this, android.R.layout.simple_expandable_list_item_1, datas); lv.setAdapter(adapter); } /** * 下拉回调 */ @Override public void onRefresh() { datas.addAll(Arrays.asList("dd", "ee", "ff")); adapter.notifyDataSetChanged(); // 刷新adapter srl.setRefreshing(false); } }
-
缺陷:只能包含一个子 View
以上。如有错误和疑问,欢迎指正提出。 catface.wyh@gmail.com