【问题标题】:fixing horizontal recyclerview XML + stuck horizontal scrolling修复水平 recyclerview XML + 卡住水平滚动
【发布时间】:2019-11-13 10:17:50
【问题描述】:

我有以下布局 -

使用以下xml -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_margin="7dp"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="start"
        android:text="@string/fragment_marketplace_marketplace_title"
        android:textSize="25sp"
        android:paddingStart="14dp"
        android:paddingLeft="14dp"
        android:paddingEnd="14dp"
        android:textStyle="bold" />

    <SearchView
        android:id="@+id/fragment_marketplace_searchview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="14dp"
        android:layout_marginTop="15dp"
        android:layout_marginEnd="14dp"
        android:queryHint="Search..."
        app:iconifiedByDefault="false"
        tools:layout_editor_absoluteX="1dp"
        tools:layout_editor_absoluteY="1dp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="14dp"
        android:layout_marginLeft="14dp"
        android:layout_marginTop="15dp"
        android:text="@string/fragment_marketplace_discover_products_from_myverte"
        android:textSize="17sp"
        android:textStyle="bold" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/fragment_marketplace_brands_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:orientation="horizontal"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"
        tools:listitem="@layout/fragment_marketplace_vendor_row_item" />

    <android.support.v4.widget.NestedScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingStart="14dp"
                android:paddingLeft="14dp"
                android:paddingEnd="14dp"
                android:background="@color/very_light_grey"
                android:paddingTop="15dp"
                android:text="@string/fragment_marketplace_featured_products"
                android:textSize="17sp"
                android:textStyle="bold" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/fragment_marketplace_products_recycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:background="@color/very_light_grey"
                tools:listitem="@layout/fragment_marketplace_products_row_item" />

        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>




</LinearLayout>

具有以下行项布局 -

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="200dp"
    android:layout_height="130dp">

    <ImageView
        android:id="@+id/vendorImageview"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginStart="15dp"
        android:layout_marginLeft="15dp"
        android:layout_marginEnd="10dp"
        android:layout_marginRight="10dp"
        android:src="@mipmap/ic_launcher"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintHorizontal_weight="0.8"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_weight="0.8" />


</android.support.constraint.ConstraintLayout>

我遇到的问题是* recyclerview - 品牌 recyclerview。

所需的结果应该是这样的 -

所以我遇到的问题是以下问题 -

1) 我需要图像是圆角的,并且要在起始位置显示下一个视图的一部分,这样它就会给人一种“列表”的感觉,而不是陈旧的图像。不知道该怎么办。

2) 顶部 recyclerview 的滚动本身非常非常卡。它根本不是流体。什么可能会打扰滚动?它确实滚动,但以一种非常不流畅的方式。

这是我用于初始化视图和相关适配器类的 java 代码 -

private void initViews(View view) {
        gson = new Gson();
        miniVendorModelList = new ArrayList<>();
        miniProductModelList = new ArrayList<>();
        searchView = view.findViewById(R.id.fragment_marketplace_searchview);
        Drawable drawable = getResources().getDrawable(R.drawable.search_widget_very_light_grey_background);
        searchView.setBackground(drawable);

        //adapters
        vendorsAdapter = new VendorAdapter(miniVendorModelList);
        productsAdapter = new ProductsAdapter(miniProductModelList, getContext());

        //lists
        vendorsList = view.findViewById(R.id.fragment_marketplace_brands_recycler_view);
        productsList = view.findViewById(R.id.fragment_marketplace_products_recycler_view);
        vendorsList.setNestedScrollingEnabled(false);
        productsList.setNestedScrollingEnabled(false);

        //brands recycler
        vendorsList.setHasFixedSize(true);
        vendorsList.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.HORIZONTAL, false));
        vendorsList.setAdapter(vendorsAdapter);

        //products recycler
        productsList.setLayoutManager(new GridLayoutManager(getContext(), 2));
        productsList.setHasFixedSize(true);
        productsList.setAdapter(productsAdapter);

    }
public class VendorAdapter extends RecyclerView.Adapter<VendorsHolder> {

    private ArrayList<MiniVendorModel> miniVendorModels;

    public VendorAdapter(ArrayList<MiniVendorModel> miniVendorModels) {
        this.miniVendorModels = miniVendorModels;
    }

    @NonNull
    @Override
    public VendorsHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.fragment_marketplace_vendor_row_item, viewGroup, false);
        return new VendorsHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull VendorsHolder vendorsHolder, int i) {
        MiniVendorModel model = miniVendorModels.get(i);
        Picasso.get().load(model.getImageURL()).memoryPolicy(MemoryPolicy.NO_CACHE).into(vendorsHolder.vendorImageView);
    }

    @Override
    public int getItemCount() {
        return miniVendorModels.size();
    }
}

我的 XML 文件缺少什么?我检查了一个不同的项目,其中 2 个 recyclerviews 一起存在于同一个布局文件中,并且它们的滚动工作完美,没有任何问题。我的供应商列表特别有问题。

这是我的问题的记录 -

https://gph.is/g/EGRnXed

【问题讨论】:

    标签: android xml android-recyclerview scroll


    【解决方案1】:

    对于圆角图片,您可以使用RoundedBitmapDrawableFactory 或查看帖子here。此外,更改行布局以填充总宽度的 80%,而不是 match_constraints。例如:android:layout_constraintWidth_percent="0.8"

    但是对于您的缓慢滚动,我认为这可能是因为您的毕加索的NO_CACHE 政策。您实际上是说不要使用缓存并每次都从互联网加载它。您可以尝试将RecyclerView 缓存大小增加setItemViewCacheSize()。另外here可以通过RecyclerView的自定义缓存策略找到一些优化。

    【讨论】:

    • no_cache 这件事实际上是为了解决这个问题哈哈。没有它就会出现问题,即使没有从网络加载任何图像但在本地添加了图像。
    • @AlonShlider 你试过增加recyclerview缓存大小吗?
    • 我已经编辑了帖子并添加了我的问题的视频,以便您更好地理解。关于缓存大小是什么意思?
    【解决方案2】:

    您应该删除 vendorsList.setNestedScrollingEnabled(false); 这一行,因为供应商列表 recyclerview 不是您布局中嵌套滚动视图的一部分

    【讨论】:

    • bhoji 已删除,没有帮助。请在主帖中查看我的编辑 - 我添加了记录问题的视频
    • 从 xml 方向、app:layoutManager、tools:listitem 中删除以下行,因为您已经在 Activity 中定义了布局管理器。
    • 工具仅用于可见性目的,我删除了布局管理器但没有帮助