方向变化时管理在滚动视图滚动位置 [英] Manage scroll position in scrollview during orientation change

查看:114
本文介绍了方向变化时管理在滚动视图滚动位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有执行如下一个书架:

I have a Bookshelf which is implemented as follows:

我有一个滚动型,其中包含一个嵌套TableLayout它充当动态生成TableRows容器

I have a ScrollView which contains a nested TableLayout which acts as a container for dynamically generated 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>

自定义滚动型code:

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

每个表格行由固定的列数是动态计算的基础上的书籍总数。在方向改变这整个布局被重新创建。

Each table row consist of a fixed number of columns which is dynamically calculated based on the total number of books. On orientation change this entire layout is re created.

当正在生成的书架我存储在静态变量的行的总数。

When the bookshelf is being generated I'm storing the total number of rows in a static variable.

当用户点击任何一本书的需要他们的详细信息页面。
我存储的Click事件过程中当前滚动位置:

When the user clicks on any of book it takes them to the details page. I am storing the current scroll position during the click event:

public static currentScrollPosition = 0;

imageviewobj.setOnClickListener(new View.OnClickListener() 
{
   @Override
   public void onClick(View v) 
   {
      currentScrollPosition = scrollviewObj.getScrollY();

...

当用户返回到这个画面我恢复使用此值滚动位置。

When the user returns to this screen I restore the scroll position by using this value.

scrollviewObj.post(new Runnable() {
        @Override
        public void run() {
            scrollviewObj.scrollTo(0, currentScrollPosition);
        } 
    });

但我怎么计算的相当于滚动的方向变化时位置
垂直偏移一个预定的在肖像模式(滚动位置)是从横向模式不同,因为行数和列数不同。

But how do I calculate the equivalent scroll position during orientation change? The vertical offset (scroll position) of a Book in the portrait mode is different from landscape mode because the number of rows and columns differ.

在这个有什么想法?

推荐答案

您应该简单地记住这是你的情况被看到(或点击)该行的索引,并将此信息感谢坚持到的onSaveInstanceState / 的onCreate

You should simply keep the index of the row that is being seen (or clicked) in your case, and persist this information thanks to onSaveInstanceState/onCreate.

之后,当发生旋转,你的新实例的onCreate 里面,计算垂直偏移量,你将不得不滚动。

After that when the rotation will occur, inside your new instance onCreate, compute the vertical offset at which you will have to scroll.

对于这一点,你需要一个全球性的树布局观察者添加到列表中的细胞得到他们的高度,如果是不固定的。然后你可以问滚动。

For this, you will need to add a global tree layout observer to your list cells to get their height, if it is not fixed. And then you can ask to scroll.

此方法允许您通过跟踪指数的列表中是独立的看法。然后,将纵向到横向,而且景观工作,为纵向。

This method allows you to be independent of your view by keeping track of the index in the list. It will then work in portrait to landscape but also landscape to portrait.

这篇关于方向变化时管理在滚动视图滚动位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆