XListView实现下拉刷新和上拉加载更多功能 +多条目展示网络数据

首先导入XListView依赖
Activity实现 XListView.IXListViewListener
XListView实现下拉刷新和上拉加载更多功能 +多条目展示网络数据_第1张图片

主页面 操作XListView 实现下拉刷新 上拉加载

package com.example.mnyk;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
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.example.xlistview.me.maxwin.view.XListView;
import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class HotFragment extends android.support.v4.app.Fragment {

    private View view;
    private XListView lv;
    private ArrayList dataItems;
    private MAdapter mAdapter;
    private String url = "http://www.xieast.com/api/news/news.php?count=10&page=";
    private int page = 1;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = View.inflate(getActivity(), R.layout.fragment_hot, null);
        //获取资源id
        lv = (XListView) view.findViewById(R.id.lv);

        dataItems = new ArrayList();
        mAdapter = new MAdapter(getActivity());

        //设置XListView监听
        lv.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {
                //下拉刷新
                page = 1;
                initData(page);
            }

            @Override
            public void onLoadMore() {
                //上拉加载
                page = page+1;
                initData(page);
            }
        });
        
        //注意 : 一定要写上这句话 否则不能实现加载更多
        //可加载
        lv.setPullLoadEnable(true);
        //初始化网络数据
        initData(page);

        lv.setAdapter(mAdapter);
        return view;
    }

    //加载网络数据
    private void initData(final int page) {
        new AsyncTask>() {
            @Override
            protected List doInBackground(String... strings) {

                DataItem dataItem = Util.ResponseBean(strings[0],DataItem.class);
                return dataItem.getData();
            }

            @Override
            protected void onPostExecute(List dataBeans) {
                if (dataBeans == null){
                    Toast.makeText(getActivity(),"请求网络数据失败",Toast.LENGTH_SHORT).show();
                    return;
                }

                if (page == 1){
                    mAdapter.setDatas(dataBeans);
                }else {
                    mAdapter.addDatas(dataBeans);
                }

                lv.stopLoadMore();
                lv.stopRefresh();

            }
        }.execute(url+page);

    }
}

封装HttpConnection工具类

package com.example.mnyk;

import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class Util {

    //请求一个URL返回Bean 数据
    public static  T ResponseBean(String urlstr, Class c) {
        //执行网络请求 获得String消息 调用封装类
        String result = Util.Response(urlstr);

        //将结果转换成bean类  gson解析
        T t = (T) new Gson().fromJson(result, c);

        return t;
    }

    public static String Response(String urlstr) {
        String result = "";
        try {
            //1.获取地址
            URL url = new URL(urlstr);
            //2.打开连接
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            //3.请求方法
            httpURLConnection.setRequestMethod("GET");
            //4.设置读取超时
            httpURLConnection.setReadTimeout(5000);
            //5.设置连接超时
            httpURLConnection.setConnectTimeout(5000);
            //6.请求结果
            int responseCode = httpURLConnection.getResponseCode();
            if (responseCode == 200) {
                result = steam2String(httpURLConnection.getInputStream());
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    private static String steam2String(InputStream is) {
        StringBuilder sb = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        try {
            for (String tmp = reader.readLine(); tmp != null; tmp = reader.readLine()) {
                sb.append(tmp);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }
}

创建适配器 MAdapter

package com.example.mnyk;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.RecyclerView;
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.ArrayList;
import java.util.List;

class MAdapter extends BaseAdapter{
    private List mdataItems;
    private Context context;

    public MAdapter(Context context) {
        this.context = context;
        mdataItems = new ArrayList<>();
    }

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

    @Override
    public DataItem.DataBean getItem(int i) {
        return mdataItems.get(i);
    }

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

    @Override
    public int getViewTypeCount() {
        return super.getViewTypeCount()+1;
    }

    @Override
    public int getItemViewType(int position) {

        DataItem.DataBean dataBean = getItem(position);
        String s1 = dataBean.getThumbnail_pic_s();
        String s2 = dataBean.getThumbnail_pic_s02();
        String s3 = dataBean.getThumbnail_pic_s03();

        if (s2 == null && s3 == null){
            return 0;
        }else {
            return 1;
        }
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder vh = null;
        if (view == null){
            vh = new ViewHolder();
            view = View.inflate(context,getItemViewType(i) == 0 ? R.layout.lv_item : R.layout.fy_item,null);
            if (getItemViewType(i) == 0){
                vh.image = (ImageView) view.findViewById(R.id.image);
                vh.title = (TextView) view.findViewById(R.id.title);
                vh.date = (TextView) view.findViewById(R.id.date);
            }else {
                vh.title_fy = (TextView) view.findViewById(R.id.title_fy);
                vh.imageLeft = (ImageView) view.findViewById(R.id.imageLeft);
                vh.imageCenter = (ImageView) view.findViewById(R.id.imageCenter);
                vh.imageRight = (ImageView) view.findViewById(R.id.imageRight);
            }

            view.setTag(vh);
        }else {
            vh = (ViewHolder) view.getTag();
        }

        if (getItemViewType(i) == 0){
            ImageLoader.getInstance().displayImage(mdataItems.get(i).getThumbnail_pic_s(),vh.image,Config.displaycle());
            vh.title.setText(mdataItems.get(i).getTitle());
            vh.date.setText(mdataItems.get(i).getDate());
        }else {
            ImageLoader.getInstance().displayImage(mdataItems.get(i).getThumbnail_pic_s(),vh.imageLeft,Config.display());
            ImageLoader.getInstance().displayImage(mdataItems.get(i).getThumbnail_pic_s02(),vh.imageCenter,Config.display());
            ImageLoader.getInstance().displayImage(mdataItems.get(i).getThumbnail_pic_s03(),vh.imageRight,Config.display());
            vh.title_fy.setText(mdataItems.get(i).getTitle());
        }

        return view;
    }

    public void setDatas(List dataBeans) {
        this.mdataItems.clear();
        if (dataBeans != null){
            this.mdataItems.addAll(dataBeans);
        }
        notifyDataSetChanged();
    }

    public void addDatas(List dataBeans) {
        if (dataBeans != null){
            this.mdataItems.addAll(dataBeans);
        }
        notifyDataSetChanged();
    }

    class ViewHolder{
        ImageView image,imageLeft,imageCenter,imageRight;
        TextView title,date,title_fy;
    }


}

布局文件




    
    


创建分页的布局 多条目展示需要用到两个xml布局文件




    

    

    

    


你可能感兴趣的:(Android)