简体   繁体   English

Android - Recyclerview 不是从顶部开始

[英]Android - Recyclerview not starting from the top

I've already seen similar problem and tried all kind of solutions, this is what I ended up with:我已经看到了类似的问题并尝试了各种解决方案,这就是我最终得到的:

    layoutManagerMemes = LinearLayoutManager(
        this@MainActivity,
        LinearLayoutManager.VERTICAL,
        false
    )

    recyclerViewMemes.setHasFixedSize(false)
    recyclerViewMemes.adapter = adapterMemes
    recyclerViewMemes.layoutManager = layoutManagerMemes

    recyclerViewMemes.scrollToPosition(0)

The XML: XML:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000000"
        android:orientation="vertical">
    
    
        <LinearLayout
            android:id="@+id/linearLayout4"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:orientation="vertical"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
    
    
            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.NoActionBar">
            </androidx.appcompat.widget.Toolbar>
    
    
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/recyclerViewMemes"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent" />
            </RelativeLayout>

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Could the issue be the images that are loaded after RecyclerView is set?问题可能是设置 RecyclerView 后加载的图像吗?

Ignore this: Stackoverflow demands some more text, Enter each item on a new line, choose the amount of groups unders settings, and click the button to generate your randomized list.忽略这一点:Stackoverflow 需要更多文本,在新行中输入每个项目,选择设置下的组数,然后单击按钮生成随机列表。 Don't like the first team?不喜欢一线队? Just click again until you do只需再次单击,直到你这样做

Apparently the only way to avoid it is to know the image dimensions and set them before the image is loaded by calculating the ratio显然,避免它的唯一方法是知道图像尺寸并通过计算比率在加载图像之前设置它们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM