Android_XListView上拉加载,下拉刷新

package activity.example.com.xlistview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;

import activity.example.com.xlistview.view.XListView;
/**
 * 用xListView实现下拉刷新, 上拉加载
 *  xListView是ListView的一个子类;里面写好了又刷新和加载的方法;
 */
public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener{

    Handler handler = new Handler();
    String data[] = new String[]{"三块石国家森林公园", "关山湖国家水利风景区", "小鹿沟青龙寺景区",
            "天女山风景区", "后安腰堡采摘园"};
    String data1[] = new String[]{"抚顺县救兵乡王木村", "抚顺县救兵乡王木村", "抚顺县救兵乡王木村",
            "抚顺县救兵乡王木村", "抚顺县救兵乡王木村"};
    private XListView xlv;
    private ArrayList> list = new ArrayList<>();
    private SimpleAdapter simpleAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        xlv = (XListView)findViewById(R.id.xlv);
        xlv.setPullLoadEnable(true);// 设置让它上拉,FALSE为不让上拉,便不加载更多数据

        for(int i=0;i<data.length; i++){
            HashMap map = new HashMap<>();
            map.put("name",data[i]);
            list.add(map);
        }
        simpleAdapter = new SimpleAdapter(MainActivity.this, list,R.layout.scenic_item_list, new String[]{"name"}, new int[]{R.id.title1});
        xlv.setAdapter(simpleAdapter);


        /**
         *         传进去接口实例,我们传的MainActivity,
         *         因为我们的Mactivity实现了XListView里的这个接口(IXListViewListener)
         *         接口回调的的是实现类里的已经实现的方法;
         */
        xlv.setXListViewListener(this);
        xlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view,
                                    int position, long id) {
                Toast.makeText(getApplicationContext(), "您点击了" + data[position - 1], Toast.LENGTH_LONG).show();
            }
        });
    }


    /**
     * 下拉刷新
     * 企业直接清空集合,重新请请求接口的第一页数据;
     */
    @Override
    public void onRefresh() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                list.clear();
                for(int i=0;i<data.length; i++){
                    HashMap map = new HashMap<>();
                    map.put("name",data1[i]);
                    list.add(map);
                }
                simpleAdapter.notifyDataSetChanged();
                completeRefresh();
            }
        },2000);
    }
    /**
     * 上拉加载
     */
    @Override
    public void onLoadMore() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {

                for(int i=0;i<data.length; i++){
                    HashMap map = new HashMap<>();
                    map.put("name",data[i]);
                    list.add(map);
                }
                simpleAdapter.notifyDataSetChanged();
                completeRefresh();
            }
        },2000);
    }

    /**
            * 完成刷新或加载的ui效果,让头部或底部隐藏;
     */
    public void completeRefresh(){
        xlv.stopRefresh();
        xlv.stopLoadMore();
        xlv.setRefreshTime("刚刚");

    }
}

你可能感兴趣的:(Android_XListView上拉加载,下拉刷新)