package com.example.myapplication;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

import java.util.List;

public class NewsAdapter extends BaseAdapter implements AbsListView.OnScrollListener {

    private List<NewBean> mList;
    private LayoutInflater mInflater;
    private ImageLoader mImageLoader;
    private int mstart,mend;
    public static String[] URLS;
    //是否第一次启动
    private boolean mFirst;

    public NewsAdapter(Context context, List<NewBean> data, ListView listView){
        //构造方法
        mList = data;
        mInflater = LayoutInflater.from(context);
        mImageLoader = new ImageLoader(listView);
        URLS = new String[data.size()];
        for (int i = 0;i < data.size();i++){
            URLS[i] = data.get(i).getPicUrl();
        }
        //绑定监听事件
        listView.setOnScrollListener(this);
        //初始化值
        mFirst = true;
    }

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

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if(convertView == null){
            viewHolder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_layout, null);
            viewHolder.newPic = convertView.findViewById(R.id.new_pic);
            viewHolder.newTitle = convertView.findViewById(R.id.new_title);
            viewHolder.newTime = convertView.findViewById(R.id.new_time);
            viewHolder.newDesc = convertView.findViewById(R.id.new_desc);
            viewHolder.newContent = convertView.findViewById(R.id.new_content);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.newPic.setImageResource(R.mipmap.ic_launcher);
        String url = mList.get(position).getPicUrl();
        viewHolder.newPic.setTag(url);
        //new ImageLoader().showImagerByThread(viewHolder.newPic, mList.get(position).getPicUrl());
        //new ImageLoader().showImageByAsyncTask(viewHolder.newPic, mList.get(position).getPicUrl());
        mImageLoader.showImageByAsyncTask(viewHolder.newPic, mList.get(position).getPicUrl());
        viewHolder.newTitle.setText(mList.get(position).getTitle());
        viewHolder.newTime.setText(mList.get(position).getTime());
        viewHolder.newDesc.setText(mList.get(position).getDesc());
        viewHolder.newContent.setText(mList.get(position).getContentUrl());
        return convertView;
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        //滑动状态的时候调用
        if(scrollState == SCROLL_STATE_IDLE){
            //停止滚动的时候
            mImageLoader.loadImages(mstart,mend);
        } else{
            //停止任务
            mImageLoader.cancelAllTasks();
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        //滑不滑动都会调用
        mstart = firstVisibleItem;
        mend = firstVisibleItem + visibleItemCount;
        //第一次显示调用
        if(mFirst == true && visibleItemCount > 0){
            mImageLoader.loadImages(mstart,mend);
            mFirst = false;
        }
    }

    class ViewHolder{
        public TextView newTitle, newTime, newDesc, newContent;
        public ImageView newPic;
    }
}
