Java ScrollTo()不';如果它通过onBackPressed()从MainActivity运行,则无法正常工作
如果我使用onBackPressed()的后退按钮从MainActivity启动table.scroll到(0,0),它并不总是有效(几乎从不)。通常需要再次按下后退按钮Java ScrollTo()不';如果它通过onBackPressed()从MainActivity运行,则无法正常工作,java,android,Java,Android,如果我使用onBackPressed()的后退按钮从MainActivity启动table.scroll到(0,0),它并不总是有效(几乎从不)。通常需要再次按下后退按钮 @Override public void onBackPressed() { table.scrollTo(0,0); } 下面的代码也不能正常工作 @Override public void onBackPressed() { 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事件))在表中运行此方法,则scrollTo(0,0)始终可以正常工作
@Override
public boolean onDoubleTap(MotionEvent event) {
scrollTo(0,0);
return true;
}
我不运行新线程。我通过onCreate()创建了一个表
将表格布局放置在水平滚动视图中,并将水平滚动视图放置在另一个滚动视图中,如下面的代码所示
<?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>
这些都是工作在我的情况下,希望这将帮助你
<?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>
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);
}
}