期间滚动视图滚动位置我有如下实现的书架:管理取向变化
我有一个滚动型,其包含嵌套TableLayout它充当动态生成TableRows的容器。
<com.test.bookshelf.CustomScrollView
android:id="@+id/scrollview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/newHeader"
android:overScrollMode="never"
android:fadingEdge="none" >
<TableLayout
android:id="@+id/tblLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="0dp" >
</TableLayout>
</com.test.bookshelf.CustomScrollView>
定制滚动型代码:
public class CustomScrollView extends ScrollView {
private boolean enableScrolling = true;
private ScrollViewListener scrollViewListener = null;
public interface OnEndScrollListener {
public void onEndScroll();
}
private OnEndScrollListener mOnEndScrollListener;
public boolean isEnableScrolling() {
return enableScrolling;
}
public void setEnableScrolling(boolean enableScrolling) {
this.enableScrolling = enableScrolling;
}
public CustomScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomScrollView(Context context) {
super(context);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (isEnableScrolling()) {
return super.onInterceptTouchEvent(ev);
} else {
return false;
}
}
public void setScrollViewListener(ScrollViewListener scrollViewListener) {
this.scrollViewListener = scrollViewListener;
}
@Override
protected void onScrollChanged(int x, int y, int oldx, int oldy) {
super.onScrollChanged(x, y, oldx, oldy);
if(scrollViewListener != null) {
scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
}
}
public OnEndScrollListener getOnEndScrollListener() {
return mOnEndScrollListener;
}
public void setOnEndScrollListener(OnEndScrollListener mOnEndScrollListener) {
this.mOnEndScrollListener = mOnEndScrollListener;
}
}
每个表行由固定的列数,其动态计算基于的书籍总数。在方向改变时,整个布局被重新创建。
正在生成书架时,我将行数总数存储在一个静态变量中。
当用户点击任何一本书时,它会将其带到详细信息页面。 我的Click事件过程中存储当前滚动位置:
public static currentScrollPosition = 0;
imageviewobj.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
currentScrollPosition = scrollviewObj.getScrollY();
....
当用户返回到这个画面我恢复使用此值滚动位置。
scrollviewObj.post(new Runnable() {
@Override
public void run() {
scrollviewObj.scrollTo(0, currentScrollPosition);
}
});
但我怎么计算的方向改变的过程中相当于滚动位置? 纵向模式下书籍的垂直偏移(滚动位置)与横向模式不同,因为行数和列数有所不同。
对此有何看法?
谢谢!一小段代码片段将有助于 – Sourav 2014-09-09 10:34:29
如何获取“正在看到”的行的索引。 ListView有一个getFirstVisiblePosition()方法。 ScrollView有类似的东西吗? – Sourav 2014-09-09 10:40:56
您可以使用getScrollY:http://stackoverflow.com/q/3327087/693752 – Snicolas 2014-09-09 11:25:11