gpt4 book ai didi

android - 在方向更改期间管理 ScrollView 中的滚动位置

转载 作者:行者123 更新时间:2023-11-29 21:02:22 25 4
gpt4 key购买 nike

我有一个 Bookshelf,其实现如下:

我有一个 ScrollView,它包含一个嵌套的 TableLayout,它充当动态生成的 TableRow 的容器。

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

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

对此有什么想法吗?

最佳答案

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

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

为此,您需要将全局树布局观察器添加到您的列表单元格以获取它们的高度(如果它不是固定的)。然后你可以要求滚动。

此方法允许您通过跟踪列表中的索引来独立于您的 View 。然后它将在纵向到横向以及从横向到纵向工作。

关于android - 在方向更改期间管理 ScrollView 中的滚动位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25648526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com