仿今日头条Fragment

package com.example.newsinfo.fragment;


import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
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.AdapterView;

import com.example.newsinfo.R;
import com.example.newsinfo.activity.WebViewActivity;
import com.example.newsinfo.adapter.MyBaseAdapter;
import com.example.newsinfo.bean.Bean;
import com.example.newsinfo.sqilte.MyManager;
import com.google.gson.Gson;
import com.limxing.xlistview.view.XListView;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

/**
 * Created by la on 2017/9/7.
 */

public class Fragment1 extends Fragment implements XListView.IXListViewListener{

    private boolean flag;
    private XListView xlv1;
    private MyBaseAdapter adapter;
    private int pageIndex = 1;
    private SQLiteDatabase db;
    private String text="";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment1, container, false);

        xlv1 = view.findViewById(R.id.xlv1);
        xlv1.setPullLoadEnable(true);
        xlv1.setXListViewListener(this);

        getContent("http://v.juhe.cn/toutiao/index?type=guoji&key=54e3d5f4ee64f51bef570ce8505d37b5");

        xlv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                //获取当前的条目
                Bean.ResultBean.DataBean dataBean = (Bean.ResultBean.DataBean) adapterView.getItemAtPosition(i);

                Intent intent = new Intent(getActivity(), WebViewActivity.class);
                intent.putExtra("url",dataBean.getUrl());
                startActivity(intent);
            }
        });
        return view;
    }

    private void getContent(String path) {

        new AsyncTask<String, Void, String>() {
            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);

                Gson gson = new Gson();
                Bean bean = gson.fromJson(s, Bean.class);
                List<Bean.ResultBean.DataBean> list = bean.getResult().getData();

                MyManager manager = new MyManager(getActivity());
                manager.add(list);
                List<Bean.ResultBean.DataBean> list1 = manager.cha();

                if(adapter == null){
                    adapter = new MyBaseAdapter(list1,getActivity());
                    xlv1.setAdapter(adapter);
                }else{
                    adapter.loadMore(list,flag);
                }

            }

            @Override
            protected String doInBackground(String... strings) {
                try {
                    String s = strings[0];
                    URL url = new URL(s);

                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setReadTimeout(5000);
                    connection.setConnectTimeout(5000);

                    int code = connection.getResponseCode();
                    if(code == 200){
                        InputStream is = connection.getInputStream();

                        String json = getJson(is);
                        return json;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        }.execute(path);
    }

    @Override
    public void onRefresh() {
        flag = false;
        ++pageIndex;
        getContent("http://v.juhe.cn/toutiao/index?type=shehui&key=54e3d5f4ee64f51bef570ce8505d37b5");
        xlv1.stopRefresh(true);
    }

    @Override
    public void onLoadMore() {
        flag = true;
        ++pageIndex;
        getContent("http://v.juhe.cn/toutiao/index?type=shehui&key=54e3d5f4ee64f51bef570ce8505d37b5");
        xlv1.stopLoadMore();
    }

    //创建将字节流转换为字符流的方法
    private String getJson(InputStream is){
        try {
            //创建字节数组输出流
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            //创建字节数组
            byte[] buffer = new byte[1024];
            //定义读取长度
            int len = 0;
            //读取数据
            while((len = is.read(buffer)) != -1){
                stream.write(buffer,0,len);
            }
            //关闭流
            stream.close();
            is.close();
            //返回结果
            return stream.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值