使用pullToRefresh实现下拉刷新,上拉加载

首先导入pullToRefresh的依赖

下载地址  https://github.com/chrisbanes/Android-PullToRefresh

导入demo之后需要更改pullToRefresh的Gradle文件版本,和当前程序的版本对应

也可以直接导入依赖到Gradle 中(目前只支持Android Studio)

compile 'com.jwenfeng.pulltorefresh:library:1.0.3'

布局文件


pullToRefresh提供的布局非常灵活,可以根据需要进行选择。

Activity配置

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

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

import jiangguxu.bawei.com.myapplication.adapter.PullAdapter;
import jiangguxu.bawei.com.myapplication.bean.MovieBean;
import jiangguxu.bawei.com.myapplication.net.NetUtils;

public class PullActivity extends AppCompatActivity implements NetUtils.NetCallback {

    private PullToRefreshListView pull_list;
    private String url="http://172.17.8.100/movieApi/movie/v1/findReleaseMovieList?userId=1&sessionId=15320748258726&page=1&count=";
    private int count=5;
    private List resultBeanList = new ArrayList<>();
    private PullAdapter adapter;
    private NetUtils instance;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pull);
        pull_list = findViewById(R.id.pull_list);
        instance = NetUtils.getInstance();
        instance.getDataFromServer(url+count,this);
        adapter = new PullAdapter(resultBeanList,this);
        pull_list.setAdapter(adapter);
    }

    @Override
    public void onSuccess(String result) {
        Gson gson = new Gson();
        MovieBean movieBean = gson.fromJson(result, MovieBean.class);
        if (count == 5){
            resultBeanList.clear();
        }
        resultBeanList.addAll(movieBean.getResult());
        adapter.notifyDataSetChanged();
        //设置刷新模式,Both表示头部和底部都可以刷新
        pull_list.setMode(PullToRefreshListView.Mode.BOTH);
        pull_list.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase refreshView) {
                count =5;
                instance.getDataFromServer(url+count,PullActivity.this);
                pull_list.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        pull_list.onRefreshComplete();
                    }
                },1000);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase refreshView) {
                count ++;
                instance.getDataFromServer(url+count,PullActivity.this);
                pull_list.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        pull_list.onRefreshComplete();
                    }
                },1000);
            }

        });
    }

    @Override
    public void onError(String errorMsg) {

    }
}

 

你可能感兴趣的:(Android)