Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使用数据绑定滚动到ScrollView上的位置?_Android_Data Binding_Scrollview - Fatal编程技术网

Android 如何使用数据绑定滚动到ScrollView上的位置?

Android 如何使用数据绑定滚动到ScrollView上的位置?,android,data-binding,scrollview,Android,Data Binding,Scrollview,我在android应用程序中使用数据绑定,但有一个问题没有人能回答我 如果我的ScrollView包含一些TextInputLayout包含InputditText元素,我如何使用数据绑定滚动到特定的TextInputLayout? 现在我使用Butterknife,绑定scrollview,并且必须手动滚动到该视图的这个位置,但是我想知道是否没有其他方法可以做到这一点 有人有想法吗?我编写了一个小型自定义BindingAdapter,它滚动到包含视图id的变量定义的位置。 我知道它包含find

我在android应用程序中使用数据绑定,但有一个问题没有人能回答我

如果我的ScrollView包含一些TextInputLayout包含InputditText元素,我如何使用数据绑定滚动到特定的TextInputLayout? 现在我使用Butterknife,绑定scrollview,并且必须手动滚动到该视图的这个位置,但是我想知道是否没有其他方法可以做到这一点


有人有想法吗?

我编写了一个小型自定义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>

[...]

可能不是您正在寻找的完全不同的方法。但我认为你至少可以通过使用活页夹的字段:binding.myscroller.scrollTo(如果你在这里将视图的id设置为R.id.myscroller)来摆脱butterknife是的@Till,你完全正确。经过更多的研究,我得到了这样一个事实:我可以得到这样一个绑定视图不要忽略方法声明中的
静态
。我在@grumpyshoe的回答中对它进行了掩饰。如果我不能将ScrollView作为参数传递,你如何调用
scrollTo
函数?@AntonisTsimourtos我不明白为什么你不能将ScrollView作为参数传递!?您编写的任何BindingAdapter都将有自己的视图作为第一个参数,第二个是您传入的视图。你能解释更多或者给一个小例子说明你想在哪里使用这个吗?对不起,我的错,我必须清理构建这个项目,因为它无法通过FindView找到ScrollView