使用的是[Android(http://lib.csdn.net/base/android).support.v4.widget.SwipeRefreshLayout控件
布局文件:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
/>
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_ly"
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>
代码如下:
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("A", "A", "A", "A", "A"));
private Handler mHandler = new Handler()
{
public void handleMessage(android.os.Message msg)
{
switch (msg.what)
{
case REFRESH_COMPLETE:
mDatas.addAll(Arrays.asList("C", "C", "C"));
mAdapter.notifyDataSetChanged();
mSwipeLayout.setRefreshing(false);
break;
}
};
};
private Toolbar toolbar;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//继承AppCompatActivity必须用这个隐藏标题
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
//toolbar
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("下拉刷新");//标题
toolbar.setTitleTextColor(Color.WHITE);
mListView = (ListView) findViewById(R.id.listview);
mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_ly);
//设置监听
mSwipeLayout.setOnRefreshListener(this);
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mDatas);
mListView.setAdapter(mAdapter);
}
public void onRefresh()
{
mHandler.sendEmptyMessageDelayed(REFRESH_COMPLETE, 2000);
}
}
网友评论