PullToRefreshListView

package lmm.bw.com.mythird;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

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 FragmentA extends Fragment {

    String urlstring = "http://172.17.8.100/movieApi/movie/v1/findReleaseMovieList?page=1&count=10";
    int page;
    private PullToRefreshListView pull;

    public List<PerBean.ResultBean> list = new ArrayList<>();
    private MyAdapter myAdapter;
    private Dao dao;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getActivity(),R.layout.fragment11,null);
        pull = view.findViewById(R.id.plistview);

        inData(page);
        myAdapter = new MyAdapter(getActivity(), list);
        pull.setAdapter(myAdapter);
        pull.setMode(PullToRefreshBase.Mode.BOTH);
        dao = new Dao(getActivity());

        pull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
                list.clear();
                page = 0;
                inData(page);
            }

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

        return view;
    }

    private void inData(int page) {
        Boolean iswork = NetUtils.iswork(getActivity());
        if (iswork){
            String myurl = urlstring + page;
            new MyAsycnTask().execute(myurl);
        }else {
            Toast.makeText(getActivity(),"没网",Toast.LENGTH_SHORT).show();
            list = dao.select();
            myAdapter = new MyAdapter(getActivity(),list);
            pull.setAdapter(myAdapter);

        }
    }

    private class MyAsycnTask extends AsyncTask<String,Void,String> {
        @Override
        protected String doInBackground(String... strings) {
            String gson = NetUtils.getGson(strings[0]);
            return gson;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Gson gson = new Gson();
            PerBean perBean = gson.fromJson(s, PerBean.class);
            List<PerBean.ResultBean> result = perBean.getResult();
            list.addAll(result);

            List<PerBean.ResultBean> select = dao.select();
            if (select.size() <=100){
                for (int i = 0; i<list.size();i++){
                    String tee = list.get(i).getName();
                    String tes = list.get(i).getSummary();
                    String ivs = list.get(i).getImageUrl();
                    dao.add(ivs,tes,tee);
                }
            }

            myAdapter.notifyDataSetChanged();
            pull.onRefreshComplete();
        }
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值