首页 > 解决方案 > 如何修复滚动不顺畅的 Recyclerview?

问题描述

我正在使用带有 LinearLayoutManager 的 RecyclerView 来获取我使用 Retrofit 从我的 API 获得的数据。问题是每当我尝试向下滚动时,它都会转到第一个位置。

Github & Here 上的一些答案建议降低图像的分辨率并避免使用可聚焦的对象,但这些解决方案都不适合我。

这是我的 RecyclerView 声明

<android.support.v7.widget.RecyclerView
            app:layout_constraintTop_toBottomOf="@id/btn_rech"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="50dp"
            android:descendantFocusability="beforeDescendants"
            android:id="@+id/recyler_view_voiture_occasion">

 </android.support.v7.widget.RecyclerView>

这是我用来绑定数据的 CardView 项

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="340dp"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        tools:ignore="MissingConstraints"
        app:cardCornerRadius="10sp"
        android:elevation="15dp"
        android:background="@color/colorGrey"
        app:cardBackgroundColor="@color/colorLightGrey"
        android:layout_margin="7dp"
        xmlns:android="http://schemas.android.com/apk/res/android">
    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        <android.support.v4.view.ViewPager
                android:layout_width="match_parent"
                android:layout_height="170dp"
                android:background="@color/colorWhite"
                android:scaleType="centerCrop"
                android:id="@+id/annonce_image"
        />

        <android.support.v7.widget.AppCompatTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="2dp"
                android:text="@string/general"
                android:layout_below="@+id/annonce_image"
                android:layout_alignStart="@id/annonce_image"
                android:layout_marginStart="10dp"
                android:textAppearance="@android:style/TextAppearance.Material.Widget.Toolbar.Title"
                android:id="@+id/annonce_info"
                />

        <android.support.v7.widget.AppCompatTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="@android:style/TextAppearance.Material.Widget.Toolbar.Subtitle"
                android:padding="2dp"
                android:layout_marginStart="10dp"
                android:id="@+id/annonce_price_info"
                android:layout_below="@+id/annonce_info"
                android:text="this is a sample text"
        />

        <android.support.v7.widget.LinearLayoutCompat
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:backgroundTint="#ffffff"
                android:outlineProvider="@color/colorPrimaryDark"
                android:orientation="horizontal"
                android:layout_marginTop="10dp"
                android:weightSum="90"
                android:background="@drawable/item_edit_versement"
                android:layout_below="@id/annonce_price_info"
                android:layout_marginHorizontal="10dp"
                android:padding="5dp"
                android:id="@+id/infos_holder"
        >
            <android.support.v7.widget.AppCompatTextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/annonce_km"
                    android:layout_weight="30"
                    android:textAlignment="center"
                    android:text="@string/prix"
                    android:textSize="14sp"
                    android:fontFamily="sans-serif-medium"
            />
            <android.support.v7.widget.AppCompatTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/annonce_annee"
                android:textAlignment="center"
                android:layout_weight="30"
                android:text="@string/prix"
                android:textSize="14sp"
                android:fontFamily="sans-serif-medium"
            />
            <android.support.v7.widget.AppCompatTextView
                android:layout_width="wrap_content"
                android:layout_weight="30"
                android:id="@+id/annonce_fuel"
                android:textAlignment="center"
                android:layout_height="wrap_content"
                android:text="@string/Fuel"
                android:textSize="14sp"
                android:fontFamily="sans-serif-medium"
            />
        </android.support.v7.widget.LinearLayoutCompat>
        <android.support.v7.widget.LinearLayoutCompat
                android:layout_width="match_parent"
                android:orientation="horizontal"
                android:layout_marginTop="12dp"
                android:weightSum="100"
                android:gravity="center"
                android:layout_below="@id/infos_holder"
                android:layout_height="wrap_content">

            <Button android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/item_edit_versement"
                    android:layout_marginHorizontal="10dp"
                    android:layout_weight="40"
                    android:textStyle="bold"
                    android:textColor="@color/colorPrimaryDark"
                    android:textSize="14sp"
                    android:text="Faire un offre"
            />

            <Button android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/item_edit_versement"
                    android:backgroundTint="#424242"
                    android:drawableStart="@drawable/phone"
                    android:paddingStart="15dp"
                    android:text="Call owner"
                    android:textColor="@color/colorWhite"
                    android:textStyle="bold"
                    android:textSize="15sp"
                    android:layout_marginHorizontal="10dp"
                    android:layout_weight="35"
            />
        </android.support.v7.widget.LinearLayoutCompat>

    </RelativeLayout>

</android.support.v7.widget.CardView>

这是我的代码部分,我从数据库中获取数据并将其调整到 RV

fun prepareRecyclerView(v : View, id : String){
        val layout = LinearLayoutManager(v.context)
        layout.orientation = LinearLayoutManager.VERTICAL
        val adapter = v.findViewById<RecyclerView>(R.id.recyler_view_voiture_occasion)
        adapter.layoutManager = layout
        val annonceAdapter = OccasionCarsAdapter(v.context,annonceList )
        adapter.adapter = annonceAdapter
        //initLineaire(v , R.id.recyler_view_voiture_occasion, LinearLayoutManager.VERTICAL ,annonceAdapter as RecyclerView.Adapter<RecyclerView.ViewHolder>)
    }

    fun getAnnonceList(id: String, maView: View, filters: VehiculeRechFilters?){
        val service = prepareService()
        val requestCall = service.GetOccasionAnnouncement(id,
            filters!!.minPrix, filters.maxPrix, filters.minAnnee,
            filters.minAnnee, filters.maxKm, filters.codeVersion)

        requestCall.enqueue(object : Callback<List<VehiculeOccasion>> {
            override fun onFailure(call: Call<List<VehiculeOccasion>>, t: Throwable) {
                Log.e("Call response" , "Can't get the data" , t.cause)
            }

            override fun onResponse(call: Call<List<VehiculeOccasion>>, response: Response<List<VehiculeOccasion>>) {
                if(response.isSuccessful){
                    Toast.makeText(this@CustomOccasionFragment.context,response.body().toString(), Toast.LENGTH_LONG).show()
                    for (e in response.body()!!){
                        annonceList.add(e)
                    }
                    prepareRecyclerView(maView!! ,idUser)
                }
                else {
                    Log.i("response assert" , "couldn't get the data correctly")
                }
            }

        })
    }

标签: androidkotlinandroid-recyclerview

解决方案


推荐阅读