Android 如何在NestedScrollView中的RecyclerView中滚动到(x,y)?
这是我的xml代码Android 如何在NestedScrollView中的RecyclerView中滚动到(x,y)?,android,android-recyclerview,nestedscrollview,android-nestedscrollview,Android,Android Recyclerview,Nestedscrollview,Android Nestedscrollview,这是我的xml代码 <android.support.v4.widget.NestedScrollView android:id="@+id/nav_scroll" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width=
<android.support.v4.widget.NestedScrollView
android:id="@+id/nav_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/nav_header_main" />
<android.support.v7.widget.RecyclerView
android:id="@+id/nav_list"
android:layout_width="match_parent"
android:layout_height="@dimen/weight_based_height"
android:layout_weight="1"
android:nestedScrollingEnabled="false"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
它们都不起作用。什么也没发生,根本没有滚动,娜达
但有趣的是,当我这么做的时候
navigationScroll.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
@Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if (scrollY == 0) {
navigationScroll.scrollTo(0, 150);
}
}
});
导航滚动到(0,150)代码>工作。你们认为我怎样才能让它工作而不必把它放在监听器中?你们把这些方法放在哪里(即,在哪个活动/片段生命周期方法中)?可能是在视图实际呈现之前调用了这些方法。在这种情况下,您应该将这些方法放在不同的生命周期方法中,或者使用
post(Runnable r)
将这些方法放在哪里(即,在哪个活动/片段生命周期方法中)?可能是在视图实际呈现之前调用了这些方法。在这种情况下,您应该将它们放在不同的生命周期方法中,或者使用post(Runnable r)
延迟它们好的,我的嵌套滚动视图
在我的抽屉布局中。我将scrollTo()
方法放入onDrawerOpened
中后,它就开始工作了
toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close){
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
// Scroll to current item
navigationScroll.smoothScrollTo(0,200);
}
};
drawer.setDrawerListener(toggle);
好的,我的NestedScrollView
在我的抽屉布局中。我将scrollTo()
方法放入onDrawerOpened
中后,它就开始工作了
toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close){
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
// Scroll to current item
navigationScroll.smoothScrollTo(0,200);
}
};
drawer.setDrawerListener(toggle);
我终于明白了,但是谢谢你的意见!我终于明白了,但是谢谢你的意见!我也有类似的问题。介意看看这个问题吗?我也有类似的问题。介意看看这个问题吗?