Android 如何在scrollview内部的recyclerview中滚动到特定位置?
我已经搜索了很多关于我的查询,没有一个有用。我在根parentlayout中的滚动视图中有一个recyclerview和一些静态数据,如下所示 我已设定—Android 如何在scrollview内部的recyclerview中滚动到特定位置?,android,android-recyclerview,scrollview,android-nestedscrollview,linearlayoutmanager,Android,Android Recyclerview,Scrollview,Android Nestedscrollview,Linearlayoutmanager,我已经搜索了很多关于我的查询,没有一个有用。我在根parentlayout中的滚动视图中有一个recyclerview和一些静态数据,如下所示 我已设定— scrollview.scrollto(0,0); 因为每次我打开活动时,它都跳转到recyclerview firstitem,并跳过recyclerview上方的静态数据 recyclerview.setNestedScrollingEnabled(false); recyclerview.setfocusable(false);
scrollview.scrollto(0,0);
因为每次我打开活动时,它都跳转到recyclerview firstitem,并跳过recyclerview上方的静态数据
recyclerview.setNestedScrollingEnabled(false);
recyclerview.setfocusable(false);
用于平滑滚动
问题在于:
layoutmanager.scrollToPositionWithOffset(pos,0);
它不起作用。在将适配器设置为recyclerview后,我已设置了上述行。也尝试了NestedScrollView,但没有成功
虽然我用过
layoutmanager.scrollToPosition(pos);
对于那些跳过代码的人,我将match_parent设置为ScrollView和
将视口填充为true
这是我的布局
<?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-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/bottomsheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.inkdrops.khaalijeb.BrandCouponActivity">
<RelativeLayout
android:id="@+id/inclusionviewgroup"
android:layout_width="match_parent"
android:layout_height="match_parent">
<static data/>
<ScrollView
android:id="@+id/scrollviewmain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/one"
android:fillViewport="true"
android:scrollbars="none"
android:layout_above="@+id/donelayout">
<staticdata/>
<TextView
android:id="@+id/dealstext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/card1"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:textSize="16sp"
android:text="Deals & Coupons"
android:textColor="#444444" />
<RelativeLayout
android:id="@+id/recyclerlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/dealstext"
android:layout_marginTop="5dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:background="@color/colorbackground">
<android.support.v7.widget.RecyclerView
android:id="@+id/coupon_rec_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorbackground"
android:visibility="visible"/>
</RelativeLayout>
<statisdata>
</RelativeLayout>
</ScrollView>
include layout="@layout/activity_coupons"
android:visibility="gone"
/>
</RelativeLayout>
</RelativeLayout>
include layout=“@layout/activity\u优惠券”
android:visibility=“消失”
/>
需要通过获取RecyclerView
的子项的顶部位置来滚动ScrollView
:
float y = recyclerView.getY() + recyclerView.getChildAt(selectedPosition).getY();
scrollView.smoothScrollTo(0, (int) y);
我知道我回答得很晚,但如果您遇到自动滚动问题,您需要在您的RelativeLayout中添加android:DegenantFocusability=“BlocksDescents”。因此,您的RealityOut标记将如下所示:
<RelativeLayout
android:id="@+id/inclusionviewgroup"
android:layout_width="match_parent"
android:descendantFocusability="blocksDescendants"
android:layout_height="match_parent">
步骤1:添加此行以在recyclerview中选择该位置
第2步:通过获取当前位置检查以下构造函数
最后一步4。在类中的适配器外部添加此函数
在listview项目中单击此行,然后单击listner
当您打开listview时,将选择该位置。使用此方法这是我的工作。希望这对你有帮助
mMessagesRecyclerView = (RecyclerView) findViewById(R.id.messagesRecyclerView);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setStackFromEnd(true);
mMessagesRecyclerView.setLayoutManager(layoutManager);
mMessagesAdapter = new MessagesAdapter();
mMessagesRecyclerView.setAdapter(mMessagesAdapter);
mMessagesRecyclerView.scrollToPosition(your position);
这是我的layoutstructer
您可以使用滚动查看方法到达所需位置,如下所示:
scrollView.smoothScrollTo(int x, int y);
X是零速度吗?@danny117否(不看),我希望它与jquery类似,在scrollto方法上有速度和y。在我的例子中,scrollView正在移动,但它没有移动到指定的适配器位置。有什么想法吗?@msrowley如果您的RecyclerView不是XML文件中的第一个元素,请将RecyclerView的Y位置添加到孩子的Y位置。您也可以发布一些android代码吗?是否确实引用了正确的
布局管理器
?不是RecyclerView
,而是ScrollView
的右侧?您想在旋转时保留滚动位置,还是只想通过编程滚动到某个位置?将适配器和相关活动/片段代码也粘贴到@akshay_shahane。
public void setCurrentItem(int item)
{
this.currentItemPos = item;
}
private void onPagerItemClick2(Integer tag)
{
adaptercat.setCurrentItem(tag);
catsp.setAdapter(adaptercat);
}
((ClassName) context).onPagerItemClick2(position);
mMessagesRecyclerView = (RecyclerView) findViewById(R.id.messagesRecyclerView);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setStackFromEnd(true);
mMessagesRecyclerView.setLayoutManager(layoutManager);
mMessagesAdapter = new MessagesAdapter();
mMessagesRecyclerView.setAdapter(mMessagesAdapter);
mMessagesRecyclerView.scrollToPosition(your position);
scrollView.smoothScrollTo(int x, int y);