xlistview上拉刷新,下拉加載

package com.qianqian.xlistview;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.qianqian.xlistview.bean.Data;
import com.qianqian.xlistview.view.xlist.XListView;

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 MainActivity extends AppCompatActivity {
    //聲明變量
    XListView xlv;
    List list=new ArrayList();;
    int current=1;
     MyAdapter adapter;
    boolean flag=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //找控件
        xlv= (XListView) findViewById(R.id.xlv);
        //初始化數據
        initData();
        //設置適配器
        adapter = new MyAdapter();
        //綁定
        xlv.setAdapter(adapter);
        //刷新
        xlv.setPullRefreshEnable(true);
        //加載
        xlv.setPullLoadEnable(true);
        //時間
        xlv.setRefreshTime("2017-10-18");
        //監聽
        xlv.setXListViewListener(new XListView.IXListViewListener() {
            //刷新
            @Override
            public void onRefresh() {
                flag=false;
                current=1;
                initData();
                xlv.stopRefresh();
            }
            //加載
            @Override
            public void onLoadMore() {
                flag=true;
                current++;
                initData();
                xlv.stopLoadMore();

            }
        });
    }
//初始化數據
    private void initData() {
        new AsyncTask() {
            @Override
            protected String doInBackground(String... parmas) {
                //解析
                getData();
                return null;
            }


            @Override
            protected void onPostExecute(String result) {
                //刷新
                adapter.notifyDataSetChanged();
                super.onPostExecute(result);
            }
        }.execute();
    }
    //解析的方法
    private void getData() {
        //路徑
        String path="https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page="+current;
        try {
            URL url=new URL(path);
            HttpURLConnection conn=(HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(5000);
            int code = conn.getResponseCode();
            if (code==200) {
                //gson解析
                Gson gson=new Gson();
                Data data = gson.fromJson(new InputStreamReader(conn.getInputStream()), Data.class);
//				list=data.getNewslist();
                if (flag) {
                    list.addAll(data.getNewslist());
                } else {
                    list=data.getNewslist();
                }
               // list.addAll(data.getNewslist());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
//適配器的方法
    class  MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            if (list!=null){
                return  list.size();
            }
            return 0;
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }
        //優化
        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder holder;
            if (view==null) {
                //加載佈局
                view=View.inflate(MainActivity.this, R.layout.item, null);
                holder=new ViewHolder();
                //紮控件
                holder.tv_title =(TextView) view.findViewById(R.id.tv_title);
                holder.tv_desc =(TextView) view.findViewById(R.id.tv_desc);
                holder.tv_time =(TextView) view.findViewById(R.id.tv_time);
                holder.iv =(ImageView) view.findViewById(R.id.iv);
                view.setTag(holder);
            }else {
                holder=(ViewHolder) view.getTag();
            }
            //賦值
            holder.tv_title.setText(list.get(i).getTitle());
            holder.tv_desc.setText(list.get(i).getDescription());
            holder.tv_time.setText(list.get(i).getCtime());
            ImageLoader.getInstance().displayImage(list.get(i).getPicUrl(), holder.iv);

            return view;
        }
    }
static  class  ViewHolder{
TextView tv_title,tv_desc,tv_time;
    ImageView iv;
}
}
//圖片
package com.qianqian.xlistview.bean; import android.app.Application; import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer; import com.qianqian.xlistview.R; public class Myapction extends Application{ @Override public void onCreate() { //初始化一個默認圖片的配置 DisplayImageOptions options=new DisplayImageOptions.Builder() .showImageOnLoading(R.mipmap.ic_launcher) //漸顯 .displayer(new FadeInBitmapDisplayer(3000)).cacheOnDisk(true) .cacheInMemory(true) .build(); ImageLoaderConfiguration config=new ImageLoaderConfiguration.Builder(this) .threadPoolSize(4) .defaultDisplayImageOptions(options) .build(); //初始化 ImageLoader.getInstance().init(config); super.onCreate(); } } 


//xml

       

You May Also Like