XListView实现上拉加载下拉刷新,注意事项

使用XListview注意事项:
首先将下载好的开源github代码中的
view包里面的三个类,xlistview,xlistviewFooter,xlistviewHeader
layout里面的xlistview_header,xlistview_footer
values下面的strings.xml里面的代码复制进来
drawable下面的一张图片
还要改那些类的R文件
public class Fragmentzhu extends Fragment implements XListView.IXListViewListener {
    @Nullable
    private List<DBean.DataBean> list = new ArrayList<>();
    int zhi = 1;
    int num = 1;//页数
    private MyAdapter myAdapter;
    private XListView xListView;

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragmentzhubeijing, container, false);
        xListView = view.findViewById(R.id.xlist_view);

        xListView.setPullLoadEnable(true);
        xListView.setPullRefreshEnable(true);
        xListView.setXListViewListener(this);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        Bundle bundle = getArguments();
        zhi = bundle.getInt("key");

        Log.i("zhi",zhi+"");

        getDataFromNet();
    }


    private void getDataFromNet() {
        AsyncTask<Void, Void, String> asyncTask = new AsyncTask<Void, Void, String>() {
            @Override
            protected String doInBackground(Void... voids) {

                String path = "http://api.expoon.com/AppNews/getNewsList/type/1/p"+num;

                try {
                    URL url = new URL(path);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

                    //设置请求
                    connection.setRequestMethod("GET");
                    connection.setReadTimeout(5000);
                    connection.setConnectTimeout(5000);

                    //获取
                    int responseCode = connection.getResponseCode();
                    if (responseCode == 200){
                        InputStream inputStream = connection.getInputStream();

                        String json = streamToString(inputStream,"utf-8");

                        return json;
                    }


                } catch (Exception e) {
                    e.printStackTrace();
                }

                return "";
            }

            @Override
            protected void onPostExecute(String json) {
                Log.i("--",json);

                Gson gson = new Gson();
                DBean dBean = gson.fromJson(json, DBean.class);

                list.addAll(dBean.getData());

                setAdapter();

                xListView.stopLoadMore();

            }
        };

        asyncTask.execute();

    }

    private void setAdapter() {
        if (myAdapter==null) {
            myAdapter = new MyAdapter(list, getActivity());
             xListView.setAdapter(myAdapter);
        }else{
            myAdapter.notifyDataSetChanged();
        }
    }

    private String streamToString(InputStream inputStream, String charset) {
        try {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream,charset);

            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String s = null;
            StringBuilder builder = new StringBuilder();
            while ((s = bufferedReader.readLine()) != null){
                builder.append(s);
            }

            bufferedReader.close();
            return builder.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public void onRefresh() {
        num--;
        if (num>0){
           refreshData();
        }else{
            Toast.makeText(getActivity(),"已经是第一页了",Toast.LENGTH_SHORT).show();
            xListView.stopRefresh();
        }
    }

    @Override
    public void onLoadMore() {
            num++;
            getDataFromNet();

 }
    private void refreshData() {


            AsyncTask<Void, Void, String> asyncTask = new AsyncTask<Void, Void, String>() {
                @Override
                protected String doInBackground(Void... voids) {

                    String path = "http://api.expoon.com/AppNews/getNewsList/type/1/p"+num;

                    try {
                        URL url = new URL(path);
                        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

                        //设置请求
                        connection.setRequestMethod("GET");
                        connection.setReadTimeout(5000);
                        connection.setConnectTimeout(5000);

                        //获取
                        int responseCode = connection.getResponseCode();
                        if (responseCode == 200){
                            InputStream inputStream = connection.getInputStream();

                            String json = streamToString(inputStream,"utf-8");

                            return json;
                        }


                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    return "";
                }

                @Override
                protected void onPostExecute(String json) {
                    Log.i("--",json);

                    Gson gson = new Gson();
                    DBean dBean = gson.fromJson(json, DBean.class);

                    list.addAll(0,dBean.getData());

                    setAdapter();

                    xListView.stopRefresh();

                    //设置刷新时间
                    long currentTimeMillis = System.currentTimeMillis();

                    Date date = new Date(System.currentTimeMillis());
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
                    String time = simpleDateFormat.format(date);


                    xListView.setRefreshTime(time);


                }
            };

            asyncTask.execute();
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值