新的XListView主页面

package zjj.bwie.com.week3_01.fragment;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;

import com.bawei.xlistviewlib.view.XListView;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;

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

import zjj.bwie.com.week3_01.R;
import zjj.bwie.com.week3_01.adapter.MyPagerAdapter;
import zjj.bwie.com.week3_01.adapter.XAdapter;
import zjj.bwie.com.week3_01.bean.Bean;

/**
 * A simple {@link Fragment} subclass.
 */
public class OneFragment extends Fragment {


    private View headView;
    private XListView xlist_view;
    private String http = "http://www.xieast.com/api/news/news.php?page=";
    private int path=2;
    private ViewPager view_pager_hear;

    private XAdapter xAdapter;
    private List<ImageView> imageViews = new ArrayList<>();
    private List<Bean.DataBean> data = new ArrayList<>();

    private MyPagerAdapter myPagerAdapter;
    private Handler handler=new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==0){
                String s= (String) msg.obj;
                Gson gson = new Gson();
                Bean bean = gson.fromJson(s, Bean.class);
                Log.i("AAAA",bean.getData().toString());
                for (int i = 0; i < bean.getData().size(); i++) {
                    ImageView imageView = new ImageView(getActivity());
                    ImageLoader.getInstance().displayImage(bean.getData().get(i).getThumbnail_pic_s(),imageView);
                    imageViews.add(imageView);
                }

                //Log.d(TAG, "onActivityCreated: "+bean.getData());
                myPagerAdapter = new MyPagerAdapter(getActivity(), imageViews);
                view_pager_hear.setAdapter(myPagerAdapter);
                handler.sendEmptyMessageDelayed(1,2000);
                myPagerAdapter.notifyDataSetChanged();

                if(path==1){
                    data.clear();
                }
                data.addAll(bean.getData());
                xAdapter.notifyDataSetChanged();
                xlist_view.stopRefresh(true);
                xlist_view.stopLoadMore();
            }else if(msg.what==1){
                int currentItem = view_pager_hear.getCurrentItem();
                currentItem++;
                view_pager_hear.setCurrentItem(currentItem);
                handler.sendEmptyMessageDelayed(1,2000);
            }

        }
    };

    public OneFragment() {
        // Required empty public constructor
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_one, container, false);
        xlist_view = view.findViewById(R.id.xlist_view);
        headView = View.inflate(getActivity(), R.layout.hear_layout, null);
        view_pager_hear = headView.findViewById(R.id.view_pager_hear);
        return view;
    }

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

        //轮播图
		//xlistview添加viewpager头
        AbsListView.LayoutParams params = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, 600);
        headView.setLayoutParams(params);

        xlist_view.addHeaderView(headView);


        xAdapter = new XAdapter(getActivity(), data);
        xlist_view.setAdapter(xAdapter);
        init();

        xlist_view.setPullRefreshEnable(true);
        xlist_view.setPullLoadEnable(true);
        xlist_view.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {
                path=1;
                init();
            }
            @Override
            public void onLoadMore() {
                path++;
                init();
            }
        });
    }

    private void init() {
        new Thread(){
            @Override
            public void run() {
                try {
                    URL url = new URL(http+path);
                    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                    urlConnection.setConnectTimeout(5000);
                    urlConnection.setReadTimeout(5000);
                    if(urlConnection.getResponseCode()==200){
                        InputStream inputStream = urlConnection.getInputStream();
                        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                        int len=-1;
                        byte[] bytes = new byte[1024];
                        while ((len=inputStream.read(bytes))!=-1){
                            byteArrayOutputStream.write(bytes,0,len);
                        }

                        String s = new String(byteArrayOutputStream.toByteArray());
                        Message message = new Message();
                        message.what=0;
                        message.obj=s;
                        handler.sendMessage(message);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值