Xlistview

package com.example.xlistviewdemo;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.example.adapter.MyBaseAdapter;
import com.example.vo.Bean;
import com.example.vo.Bean.DataEntity;
import com.example.xlistviews.XListView;
import com.example.xlistviews.XListView.IXListViewListener;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;


public class MainActivity extends Activity implements IXListViewListener{
    private XListView my_xlist;
    private int pageIndex = 10;
    private MyBaseAdapter adapter;
    private Handler mhandler;
    private List<DataEntity> data;
    private List<DataEntity> datas;
    private String URL = "http://api.expoon.com/AppNews/getNewsList/type/1/p/"+pageIndex;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        findViewById();
        my_xlist.setPullLoadEnable(true);
        my_xlist.setPullRefreshEnable(true);
        my_xlist.setXListViewListener(this);
        mhandler=new Handler();
        XUtils(URL);
    }

    private void XUtils(String uRL) {
        // TODO Auto-generated method stub
        HttpUtils httpUtils=new HttpUtils();
        httpUtils.send(HttpMethod.GET, uRL, new RequestCallBack<String>() {

            @Override
            public void onFailure(HttpException arg0, String arg1) {
                // TODO Auto-generated method stub
                
            }

            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                // TODO Auto-generated method stub
                Gson gson=new Gson();
                Bean bean = gson.fromJson(arg0.result, Bean.class);
                
                data = bean.getData();
                datas=data;
                adapter=new MyBaseAdapter(MainActivity.this,datas);
                my_xlist.setAdapter(adapter);
            }
        });
    }

    private void findViewById() {
        // TODO Auto-generated method stub
        my_xlist = (XListView)findViewById(R.id.my_xlist);
        my_xlist.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                //此处跳转传值
            }
        });
    }

    //刷新
    @Override
    public void onRefresh() {
        // TODO Auto-generated method stub
        
        mhandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                URL+=1;
                XUtils(URL);
                onLoad();
            }
        }, 2000);

    }

    private void onLoad() {
        // TODO Auto-generated method stub
        my_xlist.stopRefresh();
        my_xlist.stopLoadMore();
        // 设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 获取当前系统时间
        String nowTime = df.format(new Date(System.currentTimeMillis()));
        // 释放时提示正在刷新时的当前时间
        my_xlist.setRefreshTime(nowTime);
    }

    //加载
    @Override
    public void onLoadMore() {
        // TODO Auto-generated method stub
        mhandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                getdataflush();
                adapter.notifyDataSetChanged();
                onLoad();
            }
        }, 2000);
        
    }

    private void getdataflush() {
        // TODO Auto-generated method stub
        pageIndex = pageIndex+1;
        String URL1 = "http://api.expoon.com/AppNews/getNewsList/type/1/p/"+pageIndex;
        HttpUtils httpUtils=new HttpUtils();
        httpUtils.send(HttpMethod.GET, URL1, new RequestCallBack<String>() {

            @Override
            public void onFailure(HttpException arg0, String arg1) {
                // TODO Auto-generated method stub
                
            }

            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                // TODO Auto-generated method stub
                Gson gson=new Gson();
                Bean bean = gson.fromJson(arg0.result, Bean.class);
                
                List<DataEntity>    d = bean.getData();
                datas.addAll(d);
            }
        });
        
    }


}

   <com.example.xlistviews.XListView
        android:id="@+id/my_xlist"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        ></com.example.xlistviews.XListView>

<string name="xlistview_header_hint_normal">下拉刷新</string>
    <string name="xlistview_header_hint_ready">松开刷新数据</string>
    <string name="xlistview_header_hint_loading">正在加载...</string>
    <string name="xlistview_header_last_time">上次更新时间:</string>
    <string name="xlistview_footer_hint_normal">查看更多</string>
    <string name="xlistview_footer_hint_ready">松开载入更多</string>

原文地址:https://www.cnblogs.com/Zb759260513/p/5381149.html