import android.app.Activity; 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.Toast; import java.util.ArrayList; import java.util.HashMap; public class MainActivity extends Activity implements XListView.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 AdapterView.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.mipmap.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 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="@color/colorPrimary" android:orientation="vertical" > <hq.bwie.com.xlistview.XListView android:id="@+id/techan_xListView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:cacheColorHint="#00000000" > </hq.bwie.com.xlistview.XListView> </LinearLayout> </LinearLayout> //权限<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="hq.bwie.com.xlistview"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
xListView下拉刷新,上拉加载
最新推荐文章于 2018-01-17 20:56:22 发布