SwipeRefreshLayout
生活随笔
收集整理的這篇文章主要介紹了
SwipeRefreshLayout
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
參考官方文章:https://developer.android.com/training/swipe/index.html
通過手勢刷新與Overflow Menu中的菜單刷新,用菜單刷新是為了防止默寫客戶的機型不滿足觸屏要求,故而只能使用物理按鍵
布局
menu.xml
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:id="@+id/action_bar_menu"android:layout_width="match_parent"android:layout_height="match_parent"><item android:id="@+id/refresh"android:title="@string/refresh"app:showAsAction="never" /></menu>Toolbar.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/tool_bar"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"android:background="@color/colorPrimary"android:theme="@style/ThemeOverlay.AppCompat.ActionBar"android:popupTheme="@style/ThemeOverlay.AppCompat.Light"></android.support.v7.widget.Toolbar>
main.xml
主程序
package com.app.bt;import android.os.Bundle; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.widget.ArrayAdapter; import android.widget.ListView;public class MyActivity extends AppCompatActivity{private SwipeRefreshLayout mSwipeRefreshLayout;private Toolbar mToolbar;private ListView mListView;private ArrayAdapter mAdapter;private String[] src = {"蘋果","菠蘿","香蕉"};private String[] dst = {"百度","騰訊","阿里"};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);mToolbar = (Toolbar) findViewById(R.id.tool_bar);mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh);mListView = (ListView) findViewById(R.id.list_view);mAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,src);mToolbar.setTitle("SwipeRefreshLayout Test");setSupportActionBar(mToolbar);mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {@Overridepublic void onRefresh() {refreshListView();}});mListView.setAdapter(mAdapter);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.menu,menu);return super.onCreateOptionsMenu(menu);}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {switch (item.getItemId()){case R.id.refresh:mSwipeRefreshLayout.setRefreshing(true); //需要調用setRefreshing( )來展示進度條指示器,SwipeRefreshLayout里面的相應時間已經包好了,所以無需添加refreshListView();return true;}return super.onOptionsItemSelected(item);}private void refreshListView() {mAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,dst);mListView.setAdapter(mAdapter);mSwipeRefreshLayout.setRefreshing(false);} }總結
以上是生活随笔為你收集整理的SwipeRefreshLayout的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Toolbar+DrawerLayout
- 下一篇: Android socket 编程 实现