【问题标题】:Updating ImageView with RecyclerView使用 RecyclerView 更新 ImageView
【发布时间】:2021-03-26 13:06:17
【问题描述】:

我在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>

【问题讨论】:

    标签: android kotlin android-recyclerview imageview


    【解决方案1】:

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

    使用check this

     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
                
                
                }
            
        })
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-20
      相关资源
      最近更新 更多