如何使用数据绑定滚动到 ScrollView 上的位置?
How to scroll to position on ScrollView using databinding?
我在我的 android 应用程序中使用了数据绑定,但有一个问题没有人能回答我...
如果我的 ScrollView 包含一些包含 InputditText 元素的 TextInputLayout,我如何使用数据绑定滚动到特定的 textinputlayout?
现在我使用 Butterknife,绑定滚动视图,并且必须通过手动滚动到该视图的这个位置,但我想知道是否没有其他方法可以做到这一点。
有人有想法吗?
我写了一个小自定义 BindingAdapter,它滚动到由包含视图 ID 的变量定义的位置。
我知道它包含一个 findViewById 但对我来说没关系。
@BindingAdapter("scrollTo")
public static void scrollTo(ScrollView view, int viewId) {
if (viewId == 0) {
view.scrollTo(0, 0);
return;
}
View v = view.findViewById(viewId);
view.scrollTo(0, v.getTop());
v.requestFocus();
}
因为使用数据绑定,我可以很容易地在我的xml中添加这个适配器:
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
<variable
name="viewModel"
type="com.grumpyshoe.myapp.features.dashboard.viewmodel.DashboardViewmodel"/>
</data>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/root_dashboard"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<ScrollView
android:id="@+id/bank_account_scroll_wrapper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:scrollTo="@{viewModel.focusedViewId}">
[...]
</LinearLayout>
</layout>
我在我的 android 应用程序中使用了数据绑定,但有一个问题没有人能回答我...
如果我的 ScrollView 包含一些包含 InputditText 元素的 TextInputLayout,我如何使用数据绑定滚动到特定的 textinputlayout? 现在我使用 Butterknife,绑定滚动视图,并且必须通过手动滚动到该视图的这个位置,但我想知道是否没有其他方法可以做到这一点。
有人有想法吗?
我写了一个小自定义 BindingAdapter,它滚动到由包含视图 ID 的变量定义的位置。 我知道它包含一个 findViewById 但对我来说没关系。
@BindingAdapter("scrollTo")
public static void scrollTo(ScrollView view, int viewId) {
if (viewId == 0) {
view.scrollTo(0, 0);
return;
}
View v = view.findViewById(viewId);
view.scrollTo(0, v.getTop());
v.requestFocus();
}
因为使用数据绑定,我可以很容易地在我的xml中添加这个适配器:
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
<variable
name="viewModel"
type="com.grumpyshoe.myapp.features.dashboard.viewmodel.DashboardViewmodel"/>
</data>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/root_dashboard"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<ScrollView
android:id="@+id/bank_account_scroll_wrapper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:scrollTo="@{viewModel.focusedViewId}">
[...]
</LinearLayout>
</layout>