水平Recyclerview无法正确滚动

时间:2019-11-13 07:48:21

标签: android android-recyclerview

我有一个fragment的一部分layout PageViewer

fragment有2个RecyclerViews-一个在layout的顶部,它是水平的,另一个在底部的是垂直。

这是我的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="center"
        android:text="@string/fragment_marketplace_marketplace_title"
        android:textSize="30sp"
        android:textStyle="bold" />

    <SearchView
        android:id="@+id/fragment_marketplace_searchview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        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:layout_marginStart="14dp"
                android:layout_marginLeft="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>

enter image description here

1)向左/向右滚动时,滚动有时会卡住且无响应。为什么会这样?

2)如何使view显示列表中下一视图的某些部分,以便它将填充实际的scrollable列表,而不仅仅是陈旧的图像?

编辑-

这是我的行项目xml-

<?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="match_parent"
    android:layout_height="200dp"
    android:padding="10dp">

    <ImageView
        android:id="@+id/vendorImageview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@mipmap/ic_launcher"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</android.support.constraint.ConstraintLayout>

编辑-

我的recyclerview初始化-

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

    }

我的适配器-

package com.twoverte.adapters;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.twoverte.R;
import com.twoverte.adapters.holder.VendorsHolder;
import com.twoverte.models.Vendor.MiniVendorModel;

import java.util.ArrayList;

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

2 个答案:

答案 0 :(得分:1)

  

如何使视图显示列表中下一个视图的一部分,以便填充实际的可滚动列表,而不仅仅是陈旧的图像?

在这里您可以做的是让layout/fragment_marketplace_vendor_row_item占据80%的宽度,并添加一些填充

将宽度更新为80%

 android:layout_width="0dp"
 android:layout_weight="0.8"
  

向左/向右滚动时,滚动有时会卡住且无响应。为什么会这样?

这可能是answered here

答案 1 :(得分:0)

找到了解决滚动不良的方法-用NestedScrollView包裹水平RV。完美的作品,我不知道为什么。只是反复试验。

如果有人知道为什么这样做会很棒。