use*_*104 1 android android-coordinatorlayout android-nestedscrollview
我试图实现的是滚动到scroll_position_1时tab1(等)被点击喜欢这样。我根本不明白发生了什么。下面是我的布局结构。
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/scroll_position_1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/scroll_position_2"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|snap">
<ImageView
android:layout_width="match_parent"
android:layout_height="180dp"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax"/>
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TabItem
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<android.support.design.widget.TabItem
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</android.support.design.widget.TabLayout>
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
这就是我在单击选项卡时滚动但不起作用的方式:
mNestedScroller.smoothScrollTo(0, scroll_position_1_text_view.getY());
Run Code Online (Sandbox Code Playgroud)
而将其修改为:
mNestedScroller.scrollTo(0, scroll_position_1_text_view.getY());
Run Code Online (Sandbox Code Playgroud)
工作没有问题!请帮忙。谢谢!
在 CoordinatorLayout 中以编程方式滚动 NestedScrollView 时似乎存在错误。这解决了我的问题:
private void scrollToView(final View view) {
mScroller.scrollBy(0, 1);
mScroller.smoothScrollTo(0, view.getTop());
}
Run Code Online (Sandbox Code Playgroud)
或为了更好的控制:
private void scrollToView(final View view) {
mScroller.scrollBy(0, 1);
ObjectAnimator.ofInt(mScroller, "scrollY", view.getTop()).setDuration(700).start();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3160 次 |
| 最近记录: |