下拉刷新控件SwipeRefreshLayout

来源:互联网 发布:csgo声音优化 编辑:程序博客网 时间:2024/04/28 15:48

近期在学习android开发过程中,遇到了PullToRefresh控件,但是貌似很有没维护了吧,导入as中会出现问题。没有找到解决办法,只能用官方控件了。

Google提供了一个官方的下拉刷新控件SwipeRefreshLayout
API
几个常用的函数也很简单。

下面是我的一个例子:
XML:

<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"android:orientation="vertical">   <android.support.v4.widget.SwipeRefreshLayout   android:layout_width="fill_parent"   android:layout_height="fill_parent"   android:id="@+id/swiperefresh">    <ListView      android:id="@+id/lv"      android:layout_width="match_parent"      android:layout_height="match_parent">    </ListView>     </android.support.v4.widget.SwipeRefreshLayout></LinearLayout>

MainActivity:

package com.example.administrator.usingswiperefreshlayout;

import android.os.AsyncTask;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;

import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

private SwipeRefreshLayout swiperefresh;private ArrayAdapter<String> adapter;private ListView lv;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    //建立一个List    List<String> arr = new ArrayList();    arr.add("Hello");    arr.add("西瓜");    arr.add("Refresh");    swiperefresh = (SwipeRefreshLayout) findViewById(R.id.swiperefresh);    swiperefresh.setColorSchemeResources(android.R.color.holo_blue_light,            android.R.color.holo_red_light, android.R.color.holo_orange_light,            android.R.color.holo_green_light);//设置刷新动画的颜色    lv = (ListView) findViewById(R.id.lv);    //在listview中添加内容    adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arr);    lv.setAdapter(adapter);    //刷新监听器    swiperefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {        @Override        public void onRefresh() {            swiperefresh.setRefreshing(true);//开始刷新            //执行刷新后需要完成的操作            new Handler().postDelayed(new Runnable() {                @Override                public void run() {                    adapter.addAll("加入","成功");//添加两个条目                    //刷新完成                    Toast.makeText(MainActivity.this,"刷新完成",Toast.LENGTH_SHORT).show();                    swiperefresh.setRefreshing(false);//结束刷新                }            },4000);//刷新动画持续4秒        }    });}}

如果遇到报错 Android Error inflating class SwipeRefreshLayout

那么
You need to use a full package name for SwipeRefreshLayout:

android.support.v4.widget.SwipeRefreshLayout

0 1