gpt4 book ai didi

java - 如果 ScrollTo() 通过 onBackPressed() 从 MainActivity 运行,则无法正常工作

转载 作者:行者123 更新时间:2023-12-02 01:06:30 25 4
gpt4 key购买 nike

如果我使用 onBackPressed () 的后退按钮从 MainActivity 启动 table.scrollTo(0,0),它并不总是有效(几乎从不)。通常需要再次按后退按钮。

@Override
public void onBackPressed() {
table.scrollTo(0,0);
}

下面的代码也无法正常工作。

@Override
public void onBackPressed() {
table.scrollTo(0,0);
table.scrollTo(0,0);
}

...还有这段代码。

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

如果我通过双击(onDoubleTap(MotionEvent event))在表格内运行此方法,则scrollTo (0,0) 始终可以正常工作。

@Override
public boolean onDoubleTap(MotionEvent event) {
scrollTo(0,0);
return true;
}

我不运行新线程。我通过 onCreate() 创建一个表

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
table = new TableView(this);
setContentView(table);
}

最佳答案

将表格布局放置在 Horizo​​ntalScrollView 中,并将 Horizo​​ntalScrollView 放置在另一个 ScrollView 中,如下面的代码所示

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:isScrollContainer="true"
tools:context=".MainActivity">
<HorizontalScrollView
android:id="@+id/hScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!--row and column-->
</TableLayout>
</HorizontalScrollView>
</ScrollView>

并在 Activity 的java文件中调用ScrollView和Horizo​​ntalScrollView的scrollTo方法,如下面的代码所示

public class MainActivity extends AppCompatActivity {

ScrollView scrollView;
HorizontalScrollView hScrollView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrollView = findViewById(R.id.scrollView);
hScrollView= findViewById(R.id.hScrollView);
}

@Override
public void onBackPressed() {
scrollView.scrollTo(0,0);
hScrollView.scrollTo(0,0);
}
}

这些对我的情况有效,希望对您有帮助

关于java - 如果 ScrollTo() 通过 onBackPressed() 从 MainActivity 运行,则无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57725081/

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