正在使用RecyclerView更新ImageView



我在RecyclerView之上有一个ImageView,当我滚动通过RecyclerView时,我正试图找到一种方法来更新ImageView,其中RecyclerView的顶部项目(图像(在滚动时被放大并显示在ImageView上。

经过一番挖掘,我发现我可以使用ItemTouchHelper.SimpleCallbackItemTouchHelper.Callback。然而,两者都实现了我不想在自己的RecyclerView中使用的滑动动画,或者我根本没有找到摆脱这些动画的方法。

我希望我的解释足够清楚,如果没有评论,我将不胜感激。

这是我的xml文件供参考:

<layout 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">
<data>
<variable
name="viewModel"
type="com.example.collection.presentation.overview.CollectionOverviewViewModel" />
<variable
name="plantPhoto"
type="com.example.storage.data.PlantPhoto" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true">
<Button
android:id="@+id/to_collection_overview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/collection_overview"
app:layout_constraintBottom_toTopOf="@+id/collection_individual_imageview"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.882"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.17000002" />
<ImageView
android:id="@+id/collection_individual_imageview"
android:layout_width="150dp"
android:layout_height="200dp"
android:adjustViewBounds="false"
app:singleImage="@{viewModel.plantPhotoDisplay.plantFilePath}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.26999998"
tools:srcCompat="@tools:sample/avatars" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/collection_individual_recyclerview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/collection_individual_imageview"
app:layout_constraintVertical_bias="0.498"
app:listPhotoData="@{viewModel.listPlantPhoto}"
tools:listitem="@layout/image_plant_photo_view" />
<TextView
android:id="@+id/test_individual"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:text="Name"
app:layout_constraintBottom_toTopOf="@+id/collection_individual_recyclerview"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/collection_individual_imageview" />

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

试试这个——我不确定它可能对你有帮助。

使用检查此

reyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {

LinearLayoutManager myLayoutManager = reyclerView.getLayoutManager();
int scrollPosition = myLayoutManager.findFirstVisibleItemPosition(); // its provide you the recycler view first item postion 
// than here  get your image from your imageList[scrollPosition] and set on your top imageView


}

})

尝试使用给定的链接-https://stackoverflow.com/a/34647005/10752962

在这里,实现了从可见项目中查找回收视图的主要中心项目的逻辑,并且每当您滚动回收视图时,它将继续查找重点项目,这个任务将在calculatePositionAndScrollDate(...)方法中完成,在recyclerview的onScrollStateChanged中使用变量expectedPositionDate(可能是因为我不确定(,并且为了检查您是否必须尝试这个代码,以及它是否有效,您将获得位于中心的recycleriew项。因此,现在您可以按位置从recyclerview获取图像,并将其设置为recyclerview上方的ImageView,谢谢您,并让我们知道这是否对您有帮助。。

最新更新