在方向更改期间管理滚动视图中的滚动位置

时间:2014-09-03 15:43:17

标签: android scrollview android-tablelayout android-scrollview android-orientation

我有一个书架,其实现如下:

我有一个ScrollView,它包含一个嵌套的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>

自定义ScrollView代码:

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;
    }
}

每个表行由固定数量的列组成,这些列是根据书籍总数动态计算的。在方向更改时,将重新创建整个布局。

当生成书架时,我将总行数存储在一个静态变量中。

当用户点击任何一本书时,它会将他们带到详细信息页面。 我在点击事件期间存储当前滚动位置:

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);
        } 
    });

但如何在方向更改期间计算等效滚动位置? 纵向模式下书本的垂直偏移(滚动位置)与横向模式不同,因为行数和列数不同。

对此有何想法?

1 个答案:

答案 0 :(得分:2)

您应该只保留案例中正在查看(或点击)的行的索引,并通过onSaveInstanceState / onCreate保留此信息。

在旋转发生之后,在新实例onCreate内,计算您必须滚动的垂直偏移量。

为此,如果未修复,则需要将全局树布局观察器添加到列表单元格以获取其高度。然后你可以要求滚动。

此方法允许您通过跟踪列表中的索引来独立于您的视图。然后,它将以纵向和横向工作,但也可以从纵向到纵向工作。

相关问题