pulltorefresh上拉刷新下拉加载

mainactivity主代码

package bwie.com.yuekao_1.Fragment;

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.util.Log;
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.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import bwie.com.yuekao_1.Adapter.MovieAdapter;
import bwie.com.yuekao_1.Bean.Bean;
import bwie.com.yuekao_1.R;

public class Fragment1 extends Fragment
{

    private PullToRefreshListView pull;

    private int type=1;
    private int page=1;
    private MovieAdapter movieAdapter;
    private List<Bean.ResultBean> data;

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

        pull.setMode(PullToRefreshBase.Mode.BOTH);
        pull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                Toast.makeText(getActivity(), "下拉刷新", Toast.LENGTH_SHORT).show();

                pull.postDelayed(new Runnable()
                {
                    public void run() { //停止刷新

                        type =1;
                        page=1;
                        new MyAsy().execute();
                        movieAdapter.notifyDataSetChanged();
                        pull.onRefreshComplete();
                    }
                }, 1000);
            }

        });

        pull.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {
            @Override
            public void onLastItemVisible() {
                type=2;
                page++;
                new MyAsy().execute("http://172.17.8.100/movieApi/movie/v1/findReleaseMovieList?count=10&page="+page);
                movieAdapter.notifyDataSetChanged();
                pull.onRefreshComplete();

            }
        });

        new MyAsy().execute();
        return view ;
    }
    class MyAsy extends AsyncTask<String,String,String>
    {
        @Override
        protected String doInBackground(String... strings) {
            String path="http://172.17.8.100/movieApi/movie/v1/findReleaseMovieList?count=10&page=1";
            String message ="";
            try{
                URL url = new URL(path);
                HttpURLConnection connection =(HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.setConnectTimeout(5000);
                connection.setReadTimeout(4000);
                connection.connect();
                InputStream inputStream = connection.getInputStream();
                byte[] b = new byte[1024];
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                int len =0;
                while ((len = inputStream.read(b))!=-1)
                {
                    outputStream.write(b,0,len);
                }
                message = outputStream.toString();
                inputStream.close();
                connection.disconnect();
            }catch (Exception e)
            {

            }
            return message;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Log.i("onPostExecute",s+"");
            if(s!=null)
            {
                Gson gson = new Gson();
                Bean bean = gson.fromJson(s, Bean.class);
                data = bean.getResult();
                if(data !=null)
                {

                        movieAdapter = new MovieAdapter(getActivity(), data);
                        pull.setAdapter(movieAdapter);

                }
            }
        }
    }
}



适配器

package bwie.com.yuekao_1.Adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

import bwie.com.yuekao_1.Bean.Bean;
import bwie.com.yuekao_1.R;

public class MovieAdapter extends BaseAdapter
{
    Context context;
    List<Bean.ResultBean> data;
    public MovieAdapter(Context context, List<Bean.ResultBean> data) {
        this.context =context;
        this.data =data;
    }

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

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = View.inflate(context, R.layout.list,null);
       ImageView img = convertView.findViewById(R.id.img);
        TextView name = convertView.findViewById(R.id.name);
        TextView con = convertView.findViewById(R.id.con);
        ImageLoader.getInstance().displayImage(data.get(position).getImageUrl(),img);
        name.setText(data.get(position).getName());
        con.setText(data.get(position).getSummary());

        return convertView;
    }
}

xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <com.handmark.pulltorefresh.library.PullToRefreshListView
        android:id="@+id/pull"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</LinearLayout>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值