ListView: implement setSelection() and scrollTo()

This commit is contained in:
Julian Winkler
2024-09-03 17:51:25 +02:00
parent 3d38ec9755
commit 14eae15178
5 changed files with 48 additions and 0 deletions

View File

@@ -215,6 +215,14 @@ JNIEXPORT jlong JNICALL Java_android_widget_AbsListView_native_1constructor
JNIEXPORT void JNICALL Java_android_widget_AbsListView_native_1setAdapter
(JNIEnv *, jobject, jlong, jobject);
/*
* Class: android_widget_AbsListView
* Method: native_scrollTo
* Signature: (JI)V
*/
JNIEXPORT void JNICALL Java_android_widget_AbsListView_native_1scrollTo
(JNIEnv *, jobject, jlong, jint);
/*
* Class: android_widget_AbsListView
* Method: setItemChecked

View File

@@ -171,3 +171,12 @@ JNIEXPORT jint JNICALL Java_android_widget_AbsListView_getCheckedItemPosition(JN
return gtk_single_selection_get_selected(single_selection);
}
JNIEXPORT void JNICALL Java_android_widget_AbsListView_native_1scrollTo(JNIEnv *env, jobject this, jlong widget_ptr, jint position)
{
GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW(_PTR(widget_ptr));
GtkListView *list_view = GTK_LIST_VIEW(gtk_scrolled_window_get_child(scrolled_window));
#if GTK_CHECK_VERSION(4, 12, 0)
gtk_list_view_scroll_to(list_view, position, GTK_LIST_SCROLL_NONE, NULL);
#endif
}

View File

@@ -25,6 +25,7 @@ public abstract class AbsListView extends AdapterView {
@Override
protected native long native_constructor(Context context, AttributeSet attrs);
protected native void native_setAdapter(long widget, ListAdapter adapter);
protected native void native_scrollTo(long widget, int position);
public void setChoiceMode(int choiceMode) {}
@@ -73,6 +74,30 @@ public abstract class AbsListView extends AdapterView {
public void smoothScrollBy(int position, int duration) {}
public void smoothScrollToPositionFromTop(int position, int offset) {
native_scrollTo(this.widget, position);
}
private int pendingSelection = -1;
@Override
public void setSelection(int position, boolean animate) {
super.setSelection(position, animate);
native_scrollTo(this.widget, position);
if (getWidth() > 0 && getHeight() > 0)
native_scrollTo(AbsListView.this.widget, position);
else
pendingSelection = position;
}
@Override
public void layout(int l, int t, int r, int b) {
super.layout(l, t, r, b);
if (pendingSelection != -1) {
native_scrollTo(AbsListView.this.widget, pendingSelection);
pendingSelection = -1;
}
}
@Override
public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
return new LayoutParams(getContext(), attrs);

View File

@@ -36,6 +36,7 @@ public abstract class AdapterView extends ViewGroup {
}
public void setSelection(int i) {
setSelection(i, false);
}
public void setOnItemSelectedListener(AdapterView.OnItemSelectedListener listener) {}

View File

@@ -99,4 +99,9 @@ public class ListView extends AbsListView {
}
public void setDividerHeight(int height) {}
@Override
public void setSelection(int position, boolean animate) {
super.setSelection(position + getHeaderViewsCount(), animate);
}
}