android - 在 Android 中创建锁定的 ScrollView

标签 android android-scrollview

我正在尝试在 Android 中实现一个 ScrollView,当在当前滚动位置上方添加项目时它不会滚动。

ScrollView的默认实现如下:

在当前滚动位置上方添加一个项目: enter image description here

在当前滚动位置下方添加一个项目: enter image description here

如何在当前滚动位置上方添加项目之前“锁定”ScrollView

这是我的布局文件,我目前已经覆盖了 ScrollViewLinearLayout,但还没有做任何改动。

<LinearLayout
      android:layout_height="fill_parent"
      android:orientation="vertical"
      android:layout_width="fill_parent">

    <LinearLayout
        android:id="@+id/LinearLayout02"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_alignParentBottom="true">
        <Button
            android:id="@+id/Button02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" android:text="Add To Top"
            android:onClick="addToStart">
        </Button>
        <Button
            android:id="@+id/Button03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Add to End"
            android:onClick="addToEnd">
        </Button>
    </LinearLayout>
    <com.poc.scroller.locable.lockablescrollerpoc.LockedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fillViewport="true"
        android:scrollbarAlwaysDrawVerticalTrack="true"
        android:verticalScrollbarPosition="right"
        android:fadeScrollbars="false"
        android:background="@color/scrollColor">

        <com.poc.scroller.locable.lockablescrollerpoc.LockedLinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/Container">
        </com.poc.scroller.locable.lockablescrollerpoc.LockedLinearLayout>

    </com.poc.scroller.locable.lockablescrollerpoc.LockedScrollView>
</LinearLayout>
</android.support.constraint.ConstraintLayout>

示例源代码: https://github.com/Amaros90/android-lockable-scroller-poc

谢谢!

最佳答案

您可以通过使用 LinearLayoutaddOnLayoutChangeListener 轻松获得相反的行为并重置 ScrollViewScrollY。这是您的 ScrollViewActivity.onCreate

中的实现
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.scrollview);

    _linearLayout = (LockedLinearLayout)findViewById(R.id.Container);
    _scrollView = (LockedScrollView)findViewById(R.id.ScrollView);
    _layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    _linearLayout.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
            _scrollView.scrollTo(0, _scrollView.getScrollY() + (bottom - oldBottom ));
        }
    });

    addExampleImage(10, _linearLayout);
}

然后您可以轻松标记 addToEnd 函数或检查添加子项的位置以避免在将某些子项添加到底部时更改滚动。

关于android - 在 Android 中创建锁定的 ScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45007762/

相关文章:

php - 身份验证过程,为什么将sessionID存储在数据库中?

php - 移动 Chrome 主屏幕 webapp 无法打开 index.php

android - 如何检测ScrollView的触摸事件?

android - 动态插入 3 张图像到水平 ScrollView 或 Viewpager

Android - Horizo​​ntalScrollView - 获取焦点 View (或它的索引)

android - ScrollView 内的可滚动多行 TextView(不涉及触摸交互)

android - 外部库和 Android list

Android-sdk 管理器未显示所有包

java - ViewPagerIndicator - 将 TabPageIndicator 设置到中心

android - 在 oncreate 方法中获取 NFC 唯一 ID