SwipeRefreshLayout的使用,下拉刷新
1.
<?xml version="1.0" encoding="utf-8"?> <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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="myapplication.com.myswiperefreshlayou.MainActivity"> <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/id_swipe_ly" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@+id/id_listview" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> </android.support.v4.widget.SwipeRefreshLayout> </RelativeLayout>
2,
package myapplication.com.myswiperefreshlayou; import android.annotation.SuppressLint; import android.os.Handler; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.AbsListView; import android.widget.ArrayAdapter; import android.widget.ListView; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener{ private static final int REFRESH_COMPLETE = 0X110; 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("Lucene", "Canvas", "Bitmap")); mAdapter.notifyDataSetChanged(); mSwipeLayout.setRefreshing(false); break; } }; }; @SuppressLint("InlinedApi") protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mListView = (ListView) findViewById(R.id.id_listview); mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.id_swipe_ly); 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); mListView.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { switch (scrollState) { // 当不滚动时 case AbsListView.OnScrollListener.SCROLL_STATE_IDLE: // 判断滚动到底部 if (view.getLastVisiblePosition() == (view.getCount() - 1)) { // 滑动到底时的操作 mDatas.addAll(Arrays.asList("1", "1", "1")); mAdapter.notifyDataSetChanged(); mSwipeLayout.setRefreshing(false); } break; } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { } }); } public void onRefresh() { // Log.e("xxx", Thread.currentThread().getName()); // UI Thread mHandler.sendEmptyMessageDelayed(REFRESH_COMPLETE, 2000); } }
今天多一点积累,明天少一分烦恼