Android studio 原生自帶下拉載入控制元件SwipeRefreshLayout使用介紹
阿新 • • 發佈:2019-01-31
SwipeRefreshLayout是官方出的一款下拉載入的控制元件放在擴充套件包V4包中用法相當簡單下面我們就一步一步的來實現它吧
首先是佈局如下
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent"android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.administrator.mydemo.MainActivity"> <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swiplayout" android:layout_width="match_parent" android:layout_height="match_parent"> <ListViewandroid:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> </android.support.v4.widget.SwipeRefreshLayout> </LinearLayout>
接著是Activity 中負責邏輯程式碼實現如下
public class MainActivity extends AppCompatActivity { private SwipeRefreshLayout mSwiplayout; private ListView mListview; private ArrayAdapter<String> adapter; private List<String> list=new ArrayList<>(Arrays.asList("one","two","three","four")); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); intoView(); } private void intoView(){ mSwiplayout = (SwipeRefreshLayout) findViewById(R.id.swiplayout); mSwiplayout.setColorSchemeResources(R.color.one1,R.color.one,R.color.one5);//設定進度條的顏色最多設定四種 mSwiplayout.setProgressBackgroundColorSchemeResource(R.color.one5);//下拉進度條背景色預設為白色 // mSwiplayout.setRefreshing(true);//進到這個頁面就開始載入這裡可以加判斷如果資料加載出來就講他關掉 //手勢監聽 mSwiplayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { //下拉重新整理監聽資料更新在這操作 if (mSwiplayout.isRefreshing()){//判斷是否重新整理 // mSwiplayout.setRefreshing(false);//關掉重新整理 } } }); mListview = (ListView) findViewById(R.id.listview); adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list); mListview.setAdapter(adapter); } }
註釋寫得很清楚是不是非常的簡單有什麼問題直接評論