安卓高级主题——PullToRefresh

集成PullToRefresh

下载地址:https://github.com/open-android/PullToRefresh

 

//在项目下的build.gradle
allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}

//在app模块下的build.gradle
dependencies {
        compile 'com.github.open-android:PullToRefresh:v1.0'
}

使用PullToRefresh实现列表刷新

布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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">

    <com.itheima.pulltorefreshlib.PullToRefreshListView
        android:id="@+id/pull_to_refresh_list_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</RelativeLayout>
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.itheima.pulltorefreshlib.PullToRefreshBase;
import com.itheima.pulltorefreshlib.PullToRefreshListView;

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

public class MainActivity extends AppCompatActivity {

    private ArrayAdapter mArrayAdapter;
    private PullToRefreshListView mPullToRefreshListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mPullToRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_to_refresh_list_view);
        mArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, mockList(0, 30));
        mPullToRefreshListView.setAdapter(mArrayAdapter);

        mPullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);

        mPullToRefreshListView.setOnRefreshListener(mListViewOnRefreshListener2);


    }

    private ArrayList<String> mItems = new ArrayList<String>();
    /**
     * 创建或动态添加测试用的数据列表
     * @param start 数据的起始位置
     * @param count 数据的个数
     * @return 当前的数据列表
     */
    private List<String> mockList(int start, int count) {
        for (int i = start; i < start + count; i++) {
            mItems.add("黑马程序员:" + i);
        }
        return mItems;
    }

    private PullToRefreshBase.OnRefreshListener2<ListView> mListViewOnRefreshListener2 = new PullToRefreshBase.OnRefreshListener2<ListView>() {

        /**
         * 下拉刷新回调
         * @param refreshView
         */
        @Override
        public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
            //模拟延时三秒刷新
            mPullToRefreshListView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mItems.clear();
                    mockList(0, 20);
                    mArrayAdapter.notifyDataSetChanged();
                    mPullToRefreshListView.onRefreshComplete();//下拉刷新结束,下拉刷新头复位

                }
            }, 3000);
        }

        /**
         * 上拉加载更多回调
         * @param refreshView
         */
        @Override
        public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
            //模拟延时三秒加载更多数据
            mPullToRefreshListView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mockList(mItems.size(), 20);
                    mArrayAdapter.notifyDataSetChanged();
                    mPullToRefreshListView.onRefreshComplete();//上拉加载更多结束,上拉加载头复位
                }
            }, 3000);
        }
    };
}

以上是官方代码

自定义

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;
    }
}
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.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.itheima.pulltorefreshlib.ILoadingLayout;
import com.itheima.pulltorefreshlib.PullToRefreshBase;
import com.itheima.pulltorefreshlib.PullToRefreshListView;

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

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 = (PullToRefreshListView)findViewById(R.id.pull_to_refresh_list_view);

        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);

        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();
            }
        });
    }

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

    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();//表示刷新完成
            }
        }
    }

    static class DataAdapter extends BaseAdapter{

        private Context context;
        private ArrayList<Music> musics;
        public DataAdapter(Context context,ArrayList<Music> musics){
            this.context = 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(context).inflate(R.layout.list_item,null);
                vh = new ViewHolder();
                vh.tv_title = (TextView)convertView.findViewById(R.id.tv_title);
                vh.tv_singer = (TextView)convertView.findViewById(R.id.tv_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;
        }
    }
}

布局文件

list_item.xml

<?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"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_title"
        android:text="new Text"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:textSize="30sp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_singer"
        android:text="new Text"
        android:layout_marginLeft="30dp"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/tv_title"
        android:layout_toEndOf="@+id/tv_title"
        android:textSize="30sp"/>

</RelativeLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity">

    <com.itheima.pulltorefreshlib.PullToRefreshListView
        android:id="@+id/pull_to_refresh_list_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</android.support.constraint.ConstraintLayout>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值