Xlistview的应用1(上拉刷新,下拉加载)

在主activity中,设置一些属性

 

 

package com.example.listviewshangxia;

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

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

import com.example.listviewshangxia.XListView.IXListViewListener;
public class MainActivity extends Activity implements IXListViewListener {
    private XListView mListView;
    private SimpleAdapter mAdapter1;
    private Handler mHandler;
    private ArrayList<HashMap<String, Object>> dlist;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /** 下拉刷新,上拉加载 */
        dlist = new ArrayList<HashMap<String, Object>>();
        
        mListView = (XListView) findViewById(R.id.techan_xListView);// 这个listview是在这个layout里面
        mListView.setPullLoadEnable(true);// 设置让它上拉,FALSE为不让上拉,便不加载更多数据
        mAdapter1 = new SimpleAdapter(MainActivity.this, getData(),
                R.layout.scenic_item_list, new String[] { "name", "img",
                        "content" }, new int[] { R.id.title, R.id.img,
                        R.id.content });
        mListView.setAdapter(mAdapter1);
        mListView.setXListViewListener(this);
        mHandler = new Handler();
        mListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Toast.makeText(getApplicationContext(), "您点击了"+data[position-1], Toast.LENGTH_LONG).show();
            }
        });
    }
    
    /** 初始化本地数据 */
    String data[] = new String[] { "三块石国家森林公园", "关山湖国家水利风景区", "小鹿沟青龙寺景区",
            "天女山风景区", "后安腰堡采摘园" };
    String data1[] = new String[] { "抚顺县救兵乡王木村", "抚顺县救兵乡王木村", "抚顺县救兵乡王木村",
            "抚顺县救兵乡王木村", "抚顺县救兵乡王木村" };

    private ArrayList<HashMap<String, Object>> getData() {

        for (int i = 0; i < data.length; i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("name", data[i]);
            map.put("content", data1[i]);
            map.put("img", R.drawable.ic_launcher);
            dlist.add(map);
        }
        return dlist;
    }

    /** 停止刷新, */
    private void onLoad() {
        mListView.stopRefresh();
        mListView.stopLoadMore();
        mListView.setRefreshTime("刚刚");
    }

    // 刷新
    @Override
    public void onRefresh() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                getData();
                mListView.setAdapter(mAdapter1);
                onLoad();
            }
        }, 2000);
    }

    // 加载更多
    @Override
    public void onLoadMore() {
        mHandler.postDelayed(new Runnable() {

            @Override
            public void run() {
                getData();
                mAdapter1.notifyDataSetChanged();
                onLoad();
            }
        }, 2000);
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
         if(keyCode==KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){
            this.finish();
         }
         return false;
    }
    
}

 

 

设置xml

 

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#f0f0f0"
        android:orientation="vertical" >

        <com.example.listviewshangxia.XListView
            android:id="@+id/techan_xListView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:cacheColorHint="#00000000" >
        </com.example.listviewshangxia.XListView>
    </LinearLayout>

</LinearLayout>

posted on 2016-03-23 20:19  天空很大,我们很小  阅读(159)  评论(0编辑  收藏  举报