Подтвердить что ты не робот

Создание заблокированного ScrollView в Android

Я пытаюсь реализовать ScrollView в Android, который не прокручивается при добавлении элемента выше текущей позиции прокрутки.

Стандартная реализация ScrollView ведет себя следующим образом:

Добавление элемента выше текущей позиции прокрутки: введите описание изображения здесь

Добавление элемента ниже текущей позиции прокрутки: введите описание изображения здесь

Как я могу "заблокировать" ScrollView до добавления элемента выше текущей позиции прокрутки?

Это мой файл макета, в настоящее время я переопределял как ScrollView, так и LinearLayout, но пока не внес никаких изменений.

<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

Спасибо!

4b9b3361

Ответ 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);
                            }
                        });
                    }
                }
            }
        });
    }
}

Ответ 2

Вы можете легко получить противоположное поведение, используя addOnLayoutChangeListener ваших LinearLayout и reset ScrollView ScrollY. Вот реализация в 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 или проверить, где был добавлен ребенок, чтобы избежать изменения прокрутки, когда какой-либо дочерний элемент добавлен в нижнюю часть.

Ответ 3

Вы можете попробовать использовать RecyclerView и реализовать onDataSetChanged(). Затем определите, была ли нажата кнопка add to TOP или add 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())

Ответ 4

вы можете легко это сделать.

в первую очередь в событии onclick хранится значение int по используя getFirstVisiblePosition()

Пример.

в onclick кнопки добавить элемент сделать это ---

int currentpos = recycleview.getFirstVisiblePosition();

теперь после того, как вы вставляете elment в список /recyclerview ---

recyclerview.scrolltoposition(currentpos);

thats try, если вы хотите сделать это без ошибок..

Удачи:)