首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

Android封存和恢复ListView的滑动后位置

2013-10-01 
Android保存和恢复ListView的滑动后位置MainActivity如下:package cc.testlistviewimport java.util.Arra

Android保存和恢复ListView的滑动后位置

MainActivity如下:

package cc.testlistview;import java.util.ArrayList;import java.util.HashMap;import cn.com.bravesoft.testlistviewloadmore.R;import android.app.Activity;import android.content.SharedPreferences;import android.os.Bundle;import android.view.View;import android.widget.AbsListView;import android.widget.AbsListView.OnScrollListener;import android.widget.ListView;import android.widget.SimpleAdapter;/** * Demo描述:  * 保存和恢复ListView的当前位置 *  * 注意事项: * onScrollStateChanged()方法中 * v.getTop()方法的文档描述: * Top position of this view relative to its parent. *  * 测试方法: * 1 运行应用,滑动ListView * 2 ListView停止滑动后,按下back退出应用 * 3 重新进入应用,ListView保持原位置 */public class MainActivity extends Activity {private int position;private int listViemItemTop;private ListView mListView;private SimpleAdapter mSimpleAdapter;private HashMap<String, Object> mHashMap;private ArrayList<HashMap<String, Object>> mArrayList;private final String LISTVIEW_PREFERENCES = "listview_preferences";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);init();}private void init() {mListView = (ListView) findViewById(R.id.listView);mArrayList = new ArrayList<HashMap<String, Object>>();addDataForListView();mSimpleAdapter = new SimpleAdapter(MainActivity.this, mArrayList,R.layout.listviewitem, new String[] { "id" },new int[] { R.id.textView });mListView.setAdapter(mSimpleAdapter);mListView.setOnScrollListener(new OnScrollListenerImple());}private void addDataForListView() {for (int i = 0; i < 20; i++) {mHashMap = new HashMap<String, Object>();mHashMap.put("id", "" + i);mArrayList.add(mHashMap);}}@Overridepublic void onPause() {super.onPause();SharedPreferences mySharedPreferences = getSharedPreferences(LISTVIEW_PREFERENCES,Activity.MODE_PRIVATE);SharedPreferences.Editor edit = mySharedPreferences.edit();edit.putInt("listViemItemTop", listViemItemTop);edit.putInt("position", position);edit.commit();}@Overridepublic void onResume() {super.onResume();SharedPreferences sharedPreferences = getSharedPreferences(LISTVIEW_PREFERENCES,Activity.MODE_PRIVATE); listViemItemTop = sharedPreferences.getInt("listViemItemTop", 0);position = sharedPreferences.getInt("position", 0);//reset ListView locationmListView.setSelectionFromTop(position, listViemItemTop);}private class OnScrollListenerImple implements OnScrollListener {@Overridepublic void onScroll(AbsListView listView, int firstVisibleItem,int visibleItemCount, int totalItemCount) {}       @Overridepublic void onScrollStateChanged(AbsListView listview, int scrollState) {if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {position = mListView.getFirstVisiblePosition();View itemView = mListView.getChildAt(0);listViemItemTop = (itemView == null) ? 0 : itemView.getTop();System.out.println("---> listViemItemTop=" + listViemItemTop);}}}}


main.xml如下:

<RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >    <ListView        android:id="@+id/listView"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:layout_centerInParent="true"     /></RelativeLayout>


listviewitem.xml如下:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent" >    <TextView        android:id="@+id/textView"        android:layout_width="fill_parent"        android:gravity="center"        android:layout_height="80dip"     /></RelativeLayout>


 

热点排行