2

I have a FrameLayout at top and ViewPager at bottom
FrameLayout has PagerTabStrips
ViewPager has ScrollView in it.

What I am trying to achieve is that on the scroll of ViewPager's ScrollView, I want to translate Y position of ViewPager and FrameLayout together in sync smoothly. And at some point I have to fix the position of FrameLayout and stop its further scrolling to top.

Below is my xml code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.example"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:id="@+id/parentRelativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black"
    android:clickable="true"
    android:clipToPadding="false"
    android:fitsSystemWindows="true" >

      <FrameLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


            <com.sticky.pager.PagerSlidingTabStrip
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="48dip"
                android:layout_gravity="bottom"
                android:layout_marginTop="2dp" />

    </FrameLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/header"
        android:background="@color/blue" />

</RelativeLayout>

How can I achieve this programmatically:

@Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount, int pagePosition) {


        float scrollY=getScrollY(view);
        mViewPager.setTranslationY(-scrollY);
        mHeader.setTranslationY(-scrollY);
       //What to write here??? This does not work
Adnan
  • 1,440
  • 2
  • 18
  • 38

2 Answers2

0

You should change the top and bottom of that view instead of moving it. You can synchronize the scroll via the scroll offset

 override fun onScrollChange(v: NestedScrollView?, scrollX: Int, scrollY: Int, oldScrollX: Int, oldScrollY: Int) {
   view.bottom = valueBottomTranslate.toInt()
   view.top = valueTopTranslate.toInt()
}
Đốc.tc
  • 530
  • 4
  • 12
0

You can use this code below. You should put frame inside scrollView.

 bindingView.yourSrollView.setOnScrollListener(object : ScrollViewWithEndFunc.OnScrollListener {
        override fun onScrollChanged(scrollView: ScrollViewWithEndFunc?, x: Int, y: Int, oldX: Int, oldY: Int) {

            if(scrollView!!.scrollY >= 700) {
                bindingView.yourFrame.translationY = scrollView!!.scrollY.toFloat() - 1400 //stop scrolling frame
            }else {
                bindingView.yourFrame.translationY = -scrollView!!.scrollY.toFloat()
            }
        }
  })

enter image description here