Controlling the scroll bar in scroll mode while changing orientation

I have a bookshelf that is implemented like this:

I have a ScrollView that contains a nested TableLayout that acts as a container for dynamically generated tables.

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

      

Custom scroll 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 row of the table consists of a fixed number of columns, which is dynamically calculated based on the total number of books. When you change the orientation, the entire layout is re-created.

When the bookshelf is created, I store the total number of rows in a static variable.

When the user clicks on any book, he takes them to the details page. I keep 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 restored the scroll position using this value.

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

      

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

Any thoughts on this?

+1


source to share


1 answer


You should just store the index of the string that will be visible (or clicked) in your case, and store that information thanks to onSaveInstanceState

/ onCreate

.

After that, when the rotation occurs, inside your new instance onCreate

, calculate the vertical offset at which you have to scroll.



To do this, you will need to add the tree layout global observer to the list cells to get the height, if 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. Then he will work with portrait before landscape and landscape before portrait.

+2


source







All Articles