禁用特定孩子后的滚动

时间:2018-08-13 09:07:10

标签: android horizontalscrollview

就我而言,我有一个HorizontalScrollView。我需要实现的是,HorizontalScrollView应该是可扩展的,直到一个特定的孩子。 (我的孩子是指视图位于HorizontalScrollView内。)用户不应滚动其余部分。我当前的最低SDK是21。因此,我不能使用HorizontalScrollView.setOnScrollChange()方法。它适用于API级别23+。

在网上搜索了一段时间,但找不到适合我情况的解决方案。

我该如何实现?任何建议都会有很大帮助。

执行此操作的原因是在scrollbar停止在指定位置滚动时产生影响。参见下图。 enter image description here

1 个答案:

答案 0 :(得分:1)

您可以通过添加一个ScrollView来伪装这种暗示的行为,该ScrollView会填满整个屏幕的一半,并在其旁边添加静态元素。

下面是关于如何完成操作的伪布局:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">
    <ScrollView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".5">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#FF0000">

            <!-- Scrollable Items here -->
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="item1"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="item2"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="item3"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:text="item4"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:text="item5"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:text="item6"/>
        </LinearLayout>
    </ScrollView>
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".5"
        android:orientation="horizontal"
        android:background="#00FF00">
        <!-- Unscrollable Items here -->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="item1"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="item2"/>


    </LinearLayout>

</LinearLayout>

重要的部分是{{1}的weightSum=1layout_weights的{​​{1}}以及.5的{​​{1}}。您当然可以根据需要进行调整。