Android开发之XListview分页刷新,加载数据

首先我们把Xlistview的类包倒入工程

然后创建两个布局,头部和底部的布局:

头部布局:xlistview_footer.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <RelativeLayout
        android:id="@+id/xlistview_footer_content"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dp" >

        <ProgressBar
            android:id="@+id/xlistview_footer_progressbar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:visibility="invisible" />

        <TextView
            android:id="@+id/xlistview_footer_hint_textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="@string/xlistview_footer_hint_normal" />
    </RelativeLayout>

</LinearLayout>

底部布局:xlistview_header.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="bottom" >

    <RelativeLayout
        android:id="@+id/xlistview_header_content"
        android:layout_width="fill_parent"
        android:layout_height="60dp" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:orientation="vertical" android:id="@+id/xlistview_header_text">

            <TextView
                android:id="@+id/xlistview_header_hint_textview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/xlistview_header_hint_normal" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="3dp" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/xlistview_header_last_time"
                    android:textSize="12sp" />

                <TextView
                    android:id="@+id/xlistview_header_time"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="12sp" />
            </LinearLayout>
        </LinearLayout>

        <ImageView
            android:id="@+id/xlistview_header_arrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@id/xlistview_header_text"
            android:layout_centerVertical="true"
            android:layout_marginLeft="-35dp"
            android:src="@drawable/xlistview_arrow" />

        <ProgressBar
            android:id="@+id/xlistview_header_progressbar"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignLeft="@id/xlistview_header_text"
            android:layout_centerVertical="true"
            android:layout_marginLeft="-40dp"
            android:visibility="invisible" />
    </RelativeLayout>

</LinearLayout>

values文件下的strings.xml

   <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>

 MainActivity的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.lisen.main.MainActivity" >
  <!--引入Xlistview包名+类名--!>
    <com.bwie.xlistview.XListView
        android:id="@+id/xlv_main"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ccc" >
    </com.bwie.xlistview.XListView>

</RelativeLayout>

这是主代码引用Xlistveiw的方法:

public class MainActivity extends Activity implements IXListViewListener {
    int catalog = 1;
    int pageIndex = 1;
    int pageSize = 20;

    private XListView xlv;
    private List<XmlNews> listnews;
    private List<XmlNews> alllistnews = new ArrayList<XmlNews>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        xlv = (XListView) findViewById(R.id.xlv_main);
        xlv.setPullLoadEnable(true);
        xlv.setPullRefreshEnable(true);
        xlv.setXListViewListener(this);
        gethttp();
    }

    private void gethttp() {
        // TODO Auto-generated method stub
        String url = "http://www.oschina.net/action/api/news_list?catalog=1&pageIndex="
                + pageIndex + "&pageSize=20";
        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
                Log.i("TAG", "----------请求失败了---------------");
            }

            @Override
            public void onSuccess(ResponseInfo<String> arg0) {
                // TODO Auto-generated method stub
                // Log.i("TAG", result.toString());
                // 解析xml数据
                XStream stream = new XStream();
                stream.processAnnotations(XmlData.class);
                XmlData xdata = (XmlData) stream.fromXML(arg0.result);
                listnews = xdata.getNewslist().getNews();
                // Log.i("TAG", xdata.toString());
                alllistnews.addAll(listnews);
                xlv.setAdapter(new Myadapter(getApplicationContext(),
                        alllistnews));
            }
        });
    }

    @Override
    public void onRefresh() {
        // TODO Auto-generated method stub
        pageIndex++;
        listnews.clear();
        alllistnews.clear();
        gethttp();
        onLoad();
    }

    @Override
    public void onLoadMore() {
        // TODO Auto-generated method stub
        pageIndex++;
        gethttp();
        onLoad();
    }

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

}
原文地址:https://www.cnblogs.com/bokeyuan007/p/5356741.html