2014.03.07 ——— android GridView 记录和恢复位置
关于listview的记录和恢复数据 参见
http://trylovecatch.iteye.com/blog/2002254
listview 里面 我们主要用了 setSelectionFromTop方法 来实现精确的恢复数据
但是 gridview里面没有这个方法 所以要实现这个方法就很费劲,下面是我的大概实现过程
1、setSelection
这个是可以的 但是就是不够精确
mGridView.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
mPosition = mGridView.getFirstVisiblePosition();
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
});
mGridView.setSelection(mPosition);
2、用gridview里面提供的api
smoothScrollBy
smoothScrollByOffset
smoothScrollToPositionFromTop
前两个我试了,都没有反应 第三个是可以的,但是如果GridView处于底部 好像就不管用了
mGridView.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
mPosition = mGridView.getFirstVisiblePosition();
View v = mGridView.getChildAt(0);
mPositionTop = (v == null) ? 0 : v.getTop();
if(mItemHeight <= 0){
mItemHeight = v.getHeight();
}
System.out.println("position: " + mPosition + ", top: " + mPositionTop + ", mItemHeight: " + mItemHeight);
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
});
}
mGridView.smoothScrollToPositionFromTop(mPosition , mPositionTop);
3、使用scrollTo
这个也是可以实现滚动的,但是每次都会调用 onScrollChanged(int,int ,int,int)方法,视图被视为无效,然后位置是恢复了,但是gridview下面是空白的 只显示position的那一两行
mGridView.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
mPosition = mGridView.getFirstVisiblePosition() / 3;
View v = mGridView.getChildAt(0);
mPositionTop = (v == null) ? 0 : v.getTop();
if(mItemHeight <= 0){
mItemHeight = v.getHeight();
}
System.out.println("position: " + mPosition + ", top: " + mPositionTop + ", mItemHeight: " + mItemHeight);
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
});
mGridView.scrollTo(0, mPosition * mItemHeight + Math.abs(mPositionTop) + mPosition * 10 * 2);
4、嵌套ScrollView
通过3证明了scrollto是可以实现恢复position,所以我想可以再Gridview 外面套一层ScrollView,用它来控制GridView的显示,关于嵌套 参考
http://trylovecatch.iteye.com/blog/2002254
4.1、自定义GridView
public class MyGridView extends GridView {
public MyGridView (Context context) {
super(context);
}
public MyGridView(Context context, android.util.AttributeSet attrs) {
super(context, attrs);
}
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
4.2、布局文件
<ScrollView
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:id="@+id/local_scroll_view"
>
<com.test.MyGridView
android:id="@+id/local_grid_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:cacheColorHint="#ffffff"
android:clipToPadding="false"
android:columnWidth="100dip"
android:divider="@android:color/transparent"
android:dividerHeight="10dp"
android:fadingEdge="none"
android:listSelector="@android:color/transparent"
android:numColumns="3"
android:paddingLeft="@dimen/download_layout_horizontal_margin"
android:paddingRight="@dimen/download_layout_horizontal_margin"
android:scrollbars="vertical"
android:scrollbarStyle="outsideOverlay" />
</ScrollView>
4.3、记录和恢复
@Override
public void onPause() {
super.onPause();
mScrollY = mScroll.getScrollY();
System.out.println("mScrollY: " + mScrollY);
}
mScroll.postDelayed(new Runnable() {
@Override
public void run() {
mScroll.scrollTo(0, mScrollY);
}
}, 200);
大功告成,^_^