在Android中创建锁定的ScrollView

时间:2017-07-10 08:59:30

标签: 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

谢谢!

4 个答案:

答案 0 :(得分:16)

您可以使用import tensorflow as tf import keras.backend as K from keras.backend.tensorflow_backend import _to_tensor import numpy as np def high_med_low(x, lower_threshold=0.33, higher_threshold=0.66): """ x: tensor return a tensor """ # x_shape = K.get_variable_shape(x) # x_flat = K.flatten(x) x_array = K.get_value(x) for index in range(x_array.shape[0]): if x_array[index,0] < lower_threshold: x_array[index,0] = 0.0 elif x_array[index,0] > higher_threshold: x_array[index,0] = 1.0 else: x_array[index,0] = 0.5 # x_return = x_array.reshape(x_shape) return _to_tensor(x_array, x.dtype.base_dtype) x = K.ones((10,1)) * 0.7 print(high_med_low(x)) # the following line of code is used in building a model with keras get_custom_objects().update({'custom_activation': Activation(high_med_low)}) 的{​​{1}}轻松获得相反的行为,并重置addOnLayoutChangeListener的{​​{1}}。以下是LinearLayout

中的实施方式
ScrollView

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

答案 1 :(得分:1)

您可以尝试使用RecyclerView并实施onDataSetChanged()。然后检测是否按下了add to TOPadd to END按钮。然后使用scrollToPositionWithOffset(int, int)管理滚动。

例如:

//Scroll item 3 to 20 pixels from the top
linearLayoutManager.scrollToPositionWithOffset(3, 20);

为了恢复RecyclerView的滚动位置,这是如何保存滚动位置(方法scrollToPositionWithOffset(int, int)的两个参数):

int index = linearLayoutManager.findFirstVisibleItemPosition(); 
View v = linearLayoutManager.getChildAt(0); 
int top = (v == null) ? 0 : (v.getTop() - linearLayoutManager.getPaddingTop())

答案 2 :(得分:1)

实施这项工作对我有用。您可以查看我在原始问题中添加的示例应用。

public class LockableScrollView extends ScrollView {

    private boolean _enabled = true;

    public LockableScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        super.setFillViewport(true);
    }

    @Override
    public void addView(View layout, ViewGroup.LayoutParams params) {
        super.addView(layout, params);

        ((ViewGroup)layout).setOnHierarchyChangeListener(new OnHierarchyChangeListener() {
            @Override
            public void onChildViewAdded(View layout, View item) {
                if (_enabled) {
                    item.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
                        @Override
                        public void onLayoutChange(View item, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                        item.removeOnLayoutChangeListener(this);

                        int scrollViewY = ((LockableScrollView)item.getParent().getParent()).getScrollY();
                        int layoutPosition = ((View)item.getParent()).getTop();
                        boolean shouldScroll = item.getTop() + layoutPosition <= scrollViewY || item.getBottom() + getTop() <= scrollViewY;



                        if (shouldScroll) {
                            final int childViewHeight = item.getHeight();

                            ((View)item.getParent()).addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
                                @Override
                                public void onLayoutChange(View layout, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                                    layout.removeOnLayoutChangeListener(this);

                                    LockableScrollView scrollView = ((LockableScrollView)layout.getParent());
                                    scrollView.scrollTo(scrollView.getScrollX(), scrollView.getScrollY() + childViewHeight);
                                }
                            });
                        }
                        }
                    });
                }
            }

            @Override
            public void onChildViewRemoved(View layout, View item) {
                if (_enabled) {
                    int scrollViewY = ((LockableScrollView)layout.getParent()).getScrollY();
                    int layoutPosition = layout.getTop();
                    boolean shouldScroll = item.getTop() + layoutPosition <= scrollViewY || item.getBottom() + getTop() <= scrollViewY;


                    if (shouldScroll) {
                        final int childViewHeight = item.getHeight();

                        layout.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
                            @Override
                            public void onLayoutChange(View layout, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                                layout.removeOnLayoutChangeListener(this);

                                LockableScrollView scrollView = ((LockableScrollView)layout.getParent());
                                scrollView.scrollTo(scrollView.getScrollX(), scrollView.getScrollY() - childViewHeight);
                            }
                        });
                    }
                }
            }
        });
    }
}

答案 3 :(得分:0)

你可以轻松地做到这一点..

首先在onclick事件中存储int值 使用getFirstVisiblePosition()

例如

点击添加元素按钮

执行此操作---

  

int currentpos = recycleview.getFirstVisiblePosition();

将elment插入list / recyclerview后

  

recyclerview.scrolltoposition(currentpos);

如果你想这样做而没有错误就试试。

祝你好运:)