【问题标题】:Android Translate ScrollView and Views in sync (Programmatically)Android 同步翻译 ScrollView 和视图(以编程方式)
【发布时间】:2016-09-15 23:15:17
【问题描述】:

我在顶部有一个FrameLayout ViewPager 在底部
FrameLayoutPagerTabStrips
ViewPagerScrollView 在里面。

我想要实现的是,在 ViewPager 的 ScrollView 的滚动上,我想平滑地同步平移 ViewPagerFrameLayout 的 Y 位置。 在某些时候,我必须修复 FrameLayout 的位置并停止其进一步滚动到顶部。

以下是我的 xml 代码:

<?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>

如何以编程方式实现这一点:

@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

【问题讨论】:

    标签: android animation scrollview translate


    【解决方案1】:

    您应该更改该视图的顶部和底部,而不是移动它。您可以通过滚动偏移量来同步滚动

     override fun onScrollChange(v: NestedScrollView?, scrollX: Int, scrollY: Int, oldScrollX: Int, oldScrollY: Int) {
       view.bottom = valueBottomTranslate.toInt()
       view.top = valueTopTranslate.toInt()
    }
    

    【讨论】:

      猜你喜欢
      • 2016-10-30
      • 2013-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 2017-09-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多