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"> <ListView android: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); } }
注释写得很清楚是不是非常的简单有什么问题直接评论