Je compte que le code suivant fasse défiler le textview
de sorte que la ligne avec le jeton trouvé devienne la ligne supérieure. Je basé mon code sur cette question SO ... Scrolling a TextView to a specific lineFaire défiler une ligne spécifique de textview vers le haut ne fonctionne pas pour le dernier écran plein
Il fonctionne, sauf lorsque le dernier écran complet est affiché, qui est, quand le défilement manuel par l'utilisateur ne soit défiler le texte. Je m'attends à ce que ce comportement a à voir avec la façon dont getLineForOffset
, getLineTop
et surtout scrollTo
fonctionnent ensemble, mais je ne comprends pas cela assez bien pour déterminer ce qu'il faut faire à ce sujet. Il y a quelques autres questions SO sur ce même sujet de défilement à une ligne spécifique, mais je ne vois pas ce problème particulier abordé. IFind est initialisé à 0 pour la première recherche.
case R.id.findnext:
tokenString = token.getText().toString();
iFind = containsIgnoreCase(tokenString, html.getText().toString(), ++iFind);
if (iFind == -1) toast("Not Found: " + tokenString);
else {
final Layout lay = html.getLayout();
final int lineNumb = lay.getLineForOffset(iFind);
mScrollView.post(new Runnable() {
@Override
public void run() {
int y = lay.getLineTop(lineNumb);
mScrollView.scrollTo(0, y);
}
});
}
public int containsIgnoreCase(String needle, String hayStack, int start) {
final int lengthNeedle = needle.length();
final char firstLo = Character.toLowerCase(needle.charAt(0));
final char firstUp = Character.toUpperCase(needle.charAt(0));
int i;
for (i = start; i < hayStack.length() - lengthNeedle; i++) {
final char ch = hayStack.charAt(i);
if (ch != firstLo && ch != firstUp)
continue;
if (hayStack.regionMatches(true, i, needle, 0, lengthNeedle))
return i;
}
return -1;
}
<ScrollView
android:id="@+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" >
<TextView
android:id="@+id/codeHTML"
android:visibility="visible"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/background_material_light"
android:scrollbars="vertical"
android:fadeScrollbars="false"
android:typeface="monospace"
android:textColor="#000000"
android:textSize="15sp"
android:textIsSelectable="true" />
</ScrollView>