1. 程式人生 > >prlibrary上拉重新整理、下拉載入

prlibrary上拉重新整理、下拉載入


import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;

import com.example.myapplication.adapter.Myadapter;
import com.example.myapplication.bean.Bean1;
import com.example.myapplication.net.Helper;
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;

public class MainActivity extends AppCompatActivity {
    private String picurl="http://172.17.8.100/movieApi/movie/v1/findHotMovieList?count=3&page=";
    private int page = 1;
    private PullToRefreshListView list_view;
    private List<Bean1.ResultBean> list=new ArrayList<>();
    private   Myadapter myadapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list_view=findViewById(R.id.list_view);
        myadapter = new Myadapter(list, MainActivity.this);
        list_view.setAdapter(myadapter);
        doHttp();
        list_view.setMode(PullToRefreshBase.Mode.BOTH);
        list_view.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page = 1;
                doHttp();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                doHttp();
            }
        });
    }

    private void doHttp() {
        new Helper().get(picurl+page).result(new Helper.HttpLister() {
            @Override
            public void success(String data) {
               // Toast.makeText(MainActivity.this,data,Toast.LENGTH_LONG).show();
                Gson gson = new Gson();
                List<Bean1.ResultBean> result = gson.fromJson(data, Bean1.class).getResult();
                if (page == 1){
                    list.clear();
                }
                list.addAll(result);
                myadapter.notifyDataSetChanged();

                list_view.onRefreshComplete();
            }
        });
    }
}