Android开源库——PullToRefresh

类似SlidingMenu也是一个开源组件,集成的方式也类似,不赘述,可以参考

https://blog.csdn.net/nishigesb123/article/details/90261849


PullToRefresh

概述

使用 PullToRefresh 可以实现对多种控件进行下拉刷新、上拉加载操作,还可以实现自定义刷新和加载部分视图等操作。

项目地址:https://github.com/chrisbanes/Android-PullToRefresh

Pull To Refresh Views for Android

Screenshot

This project aims to provide a reusable Pull to Refresh widget for Android. It was originally based on Johan Nilsson's library(mainly for graphics, strings and animations), but these have been replaced since.

Features

  • Supports both Pulling Down from the top, and Pulling Up from the bottom (or even both).
  • Animated Scrolling for all devices.
  • Over Scroll supports for devices on Android v2.3+.
  • Currently works with:
    • ListView
    • ExpandableListView
    • GridView
    • WebView
    • ScrollView
    • HorizontalScrollView
    • ViewPager
  • Integrated End of List Listener for use of detecting when the user has scrolled to the bottom.
  • Maven Support.
  • Indicators to show the user when a Pull-to-Refresh is available.
  • Support for ListFragment!
  • Lots of Customisation options!

Repository at https://github.com/chrisbanes/Android-PullToRefresh.


使用PullToRefresh实现列表刷新

添加布局文件

首先有一条添加“命名空间”的语句需要设置一下,图示处找个位置加进去就好~

xmlns:ptr="http://schemas.android.com/apk/res-auto"

然后是组件

  • ptrDrawable表示图片资源(代码中使用了library里的一个默认资源,大概是一个下拉箭头的样子)
  • ptrAnimationStyle为动画效果,有两种,flip表示上下,还有一个是rotate表示旋转
  • 剩下两组ptr开头的参数分别是背景和文本颜色
    <com.handmark.pulltorefresh.library.PullToRefreshListView
        android:id="@+id/pull_to_refresh_listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        ptr:ptrAnimationStyle="flip"
        ptr:ptrDrawable="@drawable/default_ptr_flip"
        ptr:ptrHeaderBackground="@android:color/transparent"
        ptr:ptrHeaderTextColor="#919191" />

列表

一般情况下,下拉刷新的操作肯定是对列表而言的,我们这里也不例外。所以既然是对列表而言,所以我们还得先准备一个列表。

首先是列表数据对象

package com.example.a5_16pulltorefresh;

public class Music {
    //标题
    private String title;
    //歌手
    private String singer;

    public Music() {
    }

    public Music(String title, String singer) {
        this.title = title;
        this.singer = singer;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getSinger() {
        return singer;
    }

    public void setSinger(String singer) {
        this.singer = singer;
    }
}


列表项布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="New Text" />

    <TextView
        android:id="@+id/singer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/title"
        android:layout_toRightOf="@+id/title"
        android:paddingLeft="16dp"
        android:text="New Text" />
</RelativeLayout>

完整代码

package com.example.a5_16pulltorefresh;

import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private PullToRefreshListView lv;
    //音乐列表
    private ArrayList<Music> musics = new ArrayList<>();
    private DataAdapter dataAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = findViewById(R.id.pull_to_refresh_listView);

        //不带2的只用实现一个刷新方法
        //        lv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
        //            @Override
        //            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
        //
        //            }
        //        });
        //带2的是上下刷新可以单独实现方法
        lv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                new LoadDataAsyncTask(MainActivity.this).execute();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                new LoadDataAsyncTask(MainActivity.this).execute();
            }
        });
        //设置模式——BOTH表示上下都可以
        lv.setMode(PullToRefreshBase.Mode.BOTH);
        //设置刷新时显示的文本
        //下拉
        ILoadingLayout startLayout = lv.getLoadingLayoutProxy(true, false);
        startLayout.setPullLabel("正在下拉刷新...");
        startLayout.setRefreshingLabel("正在玩命加载中...");
        startLayout.setReleaseLabel("放开刷新...");
        //上拉
        ILoadingLayout endLayout = lv.getLoadingLayoutProxy(false, true);
        endLayout.setPullLabel("正在上拉刷新...");
        endLayout.setRefreshingLabel("正在玩命加载中...");
        endLayout.setReleaseLabel("放开刷新...");

        //调用一次
        loadData();
        dataAdapter = new DataAdapter(this, musics);
        lv.setAdapter(dataAdapter);
    }

    //异步类
    static class LoadDataAsyncTask extends AsyncTask<Void, Void, String> {
        private MainActivity mainActivity;

        //静态方法,需要传...
        public LoadDataAsyncTask(MainActivity mainActivity) {
            this.mainActivity = mainActivity;
        }

        @Override
        protected String doInBackground(Void... voids) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            mainActivity.loadData();
            return "success";
        }

        //完成数据的话...
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if ("success".equals(s)) {
                mainActivity.dataAdapter.notifyDataSetChanged();//通知数据集发生改变
                mainActivity.lv.onRefreshComplete();//表示刷新完成
            }
        }
    }

    //模拟一组数据
    private int count = 1;

    private void loadData() {
        for (int i = 0; i < 10; i++) {
            musics.add(new Music("歌曲-" + count, "歌手-" + count));
            count++;
        }
    }

    static class DataAdapter extends BaseAdapter {
        private Context ctx;
        private ArrayList<Music> musics;

        //构造器传上下文
        public DataAdapter(Context context, ArrayList<Music> musics) {
            this.ctx = context;
            this.musics = musics;
        }

        @Override
        public int getCount() {
            return musics.size();
        }

        @Override
        public Object getItem(int position) {
            return musics.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder vh;
            //码一下
            if (convertView == null) {
                convertView = LayoutInflater.from(ctx).inflate(R.layout.list_item, null);
                vh = new ViewHolder();
                vh.tv_title = convertView.findViewById(R.id.title);
                vh.tv_singer = convertView.findViewById(R.id.singer);
                convertView.setTag(vh);
            }
            vh = (ViewHolder) convertView.getTag();
            Music m = musics.get(position);
            vh.tv_title.setText(m.getTitle());
            vh.tv_singer.setText(m.getSinger());
            return convertView;
        }

        static class ViewHolder {
            TextView tv_title;
            TextView tv_singer;
        }
    }

}

测试效果

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云无心鸟知还

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值