我已经通过堆栈溢出的几个解决方案,但无法实现所需的结果。我有一个评论部分的firebase recyclerview。 firebase recyclerview存在于滚动视图中,因为我滚动布局的其他部分以及正常的firebase recyclerview。我设置了RecyclerView.setNestedScrollingEnabled(false)
,因此scrollview可以顺利滚动。现在我能够成功滚动出布局但我的问题只出现在我进入活动时,布局从顶部显示,这是我不想要的东西。我希望firebase recyclerview或滚动视图显示在底部的最后一项。此外,我希望RecyclerView
在用户输入新评论时自动滚动到最后一个项目位置
任何帮助将不胜感激,谢谢
以下是我的代码
oncreate里面的代码用于初始化
replyInDetailCommentsRecyclerView = findViewById(R.id.replyInDetailCommentsRecyclerView);
linearLayoutManager1 = new LinearLayoutManager(this);
replyInDetailCommentsRecyclerView.setLayoutManager(linearLayoutManager1);
replyInDetailCommentsRecyclerView.setNestedScrollingEnabled(false);
firebase recyclerview中的代码,我只添加了问题的相关部分。希望能帮助到你
adapter1.startListening();
adapter1.notifyDataSetChanged();
adapter1.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
super.onItemRangeInserted(positionStart, itemCount);
int friendlyMessageCount = adapter1.getItemCount();
int lastVisiblePosition =
linearLayoutManager1.findLastCompletelyVisibleItemPosition();
if (lastVisiblePosition == -1 ||
(positionStart >= (friendlyMessageCount - 1) &&
lastVisiblePosition == (positionStart - 1))) {
linearLayoutManager1.scrollToPosition(positionStart);
} else {
replyInDetailCommentsRecyclerView.scrollToPosition(adapter1.getItemCount() - 1);
}
}
});
replyInDetailCommentsRecyclerView.setAdapter(adapter1);
adapter1.notifyDataSetChanged();
我的XML代码
<ScrollView
android:layout_below="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:fillViewport="true"
android:fitsSystemWindows="true"
android:layout_marginBottom="50dp"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/RelativeLayout5"
android:layout_width="match_parent"
android:layout_below="@id/RelativeLayout1"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/RelativeLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/RelativeLayout4">
<android.support.v7.widget.RecyclerView
android:id="@+id/replyInDetailCommentsRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
<RelativeLayout
android:id="@+id/RelativeLayout4"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/imageInDetailComments"
android:layout_width="30dp"
android:src="@drawable/profile"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_height="30dp"/>
<android.support.v7.widget.AppCompatTextView
android:id="@+id/nameInDetailComments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textStyle="bold"
android:layout_alignTop="@+id/imageInDetailComments"
tools:text="Saqib"
android:layout_toRightOf="@+id/imageInDetailComments"
android:layout_marginLeft="5dp"
android:textColor="#096697"
android:layout_toEndOf="@+id/imageInDetailComments"
android:layout_marginStart="5dp" />
<android.support.v7.widget.AppCompatRatingBar
android:id="@+id/ratingInDetailComments"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:max="5"
android:numStars="5"
android:progressBackgroundTint="@color/colorAccent"
android:progressTint="@color/colorAccent"
android:rating="0.0"
android:secondaryProgressTint="@color/colorAccent"
android:stepSize="0.0"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/imageInDetailComments"
android:layout_marginLeft="5dp"
android:layout_below="@+id/nameInDetailComments"
android:layout_marginStart="5dp"
android:layout_toEndOf="@+id/imageInDetailComments" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/userCommentInDetailComments"
android:layout_width="0dp"
android:layout_below="@+id/ratingInDetailComments"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/imageInDetailComments"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:background="#efeeee"
android:padding="5dp"
android:text="i can do it mate, my bidding price includes gotaskie fee"
/>
<View
android:layout_below="@+id/userCommentInDetailComments"
android:layout_marginTop="8dp"
android:id="@+id/lineAboveCommentsRecyclerView"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#dad8d8"
/>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
您可以使用NestedScrollView而不是ScrollView