ScrollView无法在3个recyclerviews内部平滑滚动

时间:2019-11-15 05:20:49

标签: android android-recyclerview scrollview

这是我的代码。我想在我的布局中放置3个recyclerViews,但是在运行时设备上速度很慢。如果有2个recyclerview可以正常运行,但如果3个非常慢:

 <?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:id="@+id/scroll_view"
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="wrap_content"
tools:context=".fragments.MainFragment">





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



           <TextView
               android:textColor="@color/blacker"
               android:textStyle="bold"
               android:textSize="18sp"
               android:text="Техника для офиса"
               android:layout_marginTop="20dp"
               android:layout_marginLeft="15dp"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_marginStart="15dp" />



           <android.support.v7.widget.RecyclerView
               android:nestedScrollingEnabled="false"
               android:fillViewport="true"
               android:layout_marginBottom="15dp"
               android:layout_marginTop="15dp"
               android:id="@+id/recyclerView_popular_goods"
               android:layout_width="wrap_content"
               android:layout_height="match_parent"/>



           <TextView
               android:textColor="@color/blacker"
               android:textStyle="bold"
               android:textSize="18sp"
               android:text="Компьютерная техника"
               android:layout_marginTop="20dp"
               android:layout_marginLeft="15dp"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_marginStart="15dp" />



           <android.support.v7.widget.RecyclerView
               android:nestedScrollingEnabled="false"
               android:fillViewport="true"
               android:layout_marginBottom="15dp"
               android:layout_marginTop="15dp"
               android:id="@+id/recyclerView_competitive_goods"
               android:layout_width="wrap_content"
               android:layout_height="match_parent"/>



           <TextView
               android:textColor="@color/blacker"
               android:textStyle="bold"
               android:textSize="18sp"
               android:text="Канцелярские товары"
               android:layout_marginTop="20dp"
               android:layout_marginLeft="15dp"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_marginStart="15dp" />



           <android.support.v7.widget.RecyclerView
               android:visibility="gone"
               android:fillViewport="true"
               android:layout_marginBottom="15dp"
               android:layout_marginTop="15dp"
               android:id="@+id/recyclerView_stationery_goods"
               android:layout_width="wrap_content"
               android:layout_height="match_parent"/>

       </LinearLayout>

如果有人遇到此类问题,请帮助我解决问题。

我尝试了Nested scrollviewsetNestedScrollingEnabled = false。但是没有帮助

这是我的活动

   @SuppressLint("ValidFragment")
   public class MainFragment extends BasicFragment implements 
   MainFragmentView, PopularGoodsAdapter.ProductListener {


  @BindView(R.id.scroll_view)
  NestedScrollView mScrollView;



@BindView(R.id.recyclerView_stationery_goods)
RecyclerView mRecyclerViewStationeryGoods;


@BindView(R.id.recyclerView_popular_goods)
RecyclerView mRecyclerViewPopularGoods;


@BindView(R.id.recyclerView_competitive_goods)
RecyclerView mRecyclerViewCompetitiveGoods;


@BindView(R.id.imageSlider)
SliderView mSliderView;


@Inject
Navigator mNavigator;


@Inject
@Named(DISPLAY_WIDTH)
int mDisplayWidth;


@InjectPresenter
MainFragmentPresenter mPresenter;


List<Product> list1;
List<Product> list2;
List<Product> list3;


private PopularGoodsAdapter mAdapterPopularGoods;
private PopularGoodsAdapter mAdapterPopularGoods2;
private PopularGoodsAdapter mAdapterPopularGoods3;
private LayoutAnimationController layoutAnimationController;
private List<String> images;


@SuppressLint("ValidFragment")
public MainFragment() {

}





private void initView(View view) {
    ButterKnife.bind(this,view);

    images = new LinkedList<>();
    images.add("https://images.pexels.com/photos/747964/pexels-photo-747964.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260");
    images.add("https://images.pexels.com/photos/929778/pexels-photo-929778.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260");
    images.add("https://images.pexels.com/photos/218983/pexels-photo-218983.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260");

    mSliderView.setSliderAdapter(new SliderAdapterExample(getContext(), images, "mainPage"));


    LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
    LinearLayoutManager layoutManager2 = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
    LinearLayoutManager layoutManager3 = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);

    mRecyclerViewPopularGoods.setLayoutManager(layoutManager);

    layoutAnimationController = AnimationUtils.loadLayoutAnimation(getContext(),R.anim.layout_item_from_left);


    list1 = new LinkedList<>();
    mRecyclerViewPopularGoods.setLayoutAnimation(layoutAnimationController);



    mRecyclerViewCompetitiveGoods.setLayoutManager(layoutManager2);

    list2 = new LinkedList<>();
    mRecyclerViewCompetitiveGoods.setLayoutAnimation(layoutAnimationController);



    mRecyclerViewStationeryGoods.setLayoutManager(layoutManager3);

    list3 = new LinkedList<>();
    mRecyclerViewStationeryGoods.setLayoutAnimation(layoutAnimationController);



    mRecyclerViewPopularGoods.setAdapter(mAdapterPopularGoods = new PopularGoodsAdapter(getContext(),list1,mDisplayWidth,"popular",this));
    mRecyclerViewCompetitiveGoods.setAdapter(mAdapterPopularGoods2 = new PopularGoodsAdapter(getContext(),list2,mDisplayWidth,"competitive",this));
    mRecyclerViewStationeryGoods.setAdapter(mAdapterPopularGoods3 = new PopularGoodsAdapter(getContext(),list3,mDisplayWidth,"stationery",this));

    mRecyclerViewPopularGoods.setNestedScrollingEnabled(false);
    mRecyclerViewCompetitiveGoods.setNestedScrollingEnabled(false);
    mRecyclerViewStationeryGoods.setNestedScrollingEnabled(false);


    getLoadingDialog().showDialog(getFragmentManager());
    mPresenter.GET_TOP_HOME_VIEW();

}




@Override
  public View onCreateView(LayoutInflater inflater, ViewGroup 
   container, 
  Bundle savedInstanceState) {

      View view =  inflater.inflate(R.layout.fragment_main, container, 
     false);
    initView(view);
    return view;
  }










@OnClick({R.id.item_sellers,
 R.id.item_price_request,R.id.item_price_competitions})
  void Onclick(View view) {
      switch (view.getId()) {

        case R.id.item_sellers:

                mNavigator.toSellersActivty(getContext());

            break;

        case R.id.item_price_request:

                mNavigator.toPriceRequestActivty(getContext());

            break;

        case R.id.item_price_competitions:

                mNavigator.toCompetitionsActivty(getContext());

            break;

    }
}




@Override
public void initTopHomeView(List<HomeViewObject> result) {

    if (null != result ) {

        if (result.size() >= 1 && null != result.get(0)) {
            list1.addAll(result.get(0).getTopProducts());
            mAdapterPopularGoods.notifyDataSetChanged();
            mRecyclerViewPopularGoods.setLayoutAnimation(layoutAnimationController);

        }

        if (result.size() >= 2 && null != result.get(1)) {
            list2.addAll(result.get(1).getTopProducts());
            mAdapterPopularGoods2.notifyDataSetChanged();
            mRecyclerViewPopularGoods.setLayoutAnimation(layoutAnimationController);
        }

        if (result.size() >= 3 && null != result.get(2)) {
            list3.addAll(result.get(2).getTopProducts());
            mAdapterPopularGoods3.notifyDataSetChanged();
            mRecyclerViewStationeryGoods.setLayoutAnimation(layoutAnimationController);
        }

    }




}



@Override
public void stopProgress() {
    getLoadingDialog().hideDialog();
}



@Override
public void onProductClicked(Product product) {
    mNavigator.toProductActivity(getContext(),product);
 }
  }

有什么错误吗?

5 个答案:

答案 0 :(得分:0)

尝试以下方法-

  1. 尝试将所有视图放入NestedScrollView内,即根视图。

  2. 设置recyclerView.setNestedScrollingEnabled(false); xml中的两个recyclerView。

答案 1 :(得分:0)

将您的回收站视图的android:layout_height="match_parent"更改为android:layout_height="wrap_content",然后将嵌套的scrollView与recyclerView.setNestedScrollingEnabled(false);一起使用

答案 2 :(得分:0)

尝试用ScrollView更改NestedScrollView,并将回收站视图的android:layout_height="match_parent"更改为android:layout_height="wrap_content"

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
    android:id="@+id/scroll_view"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".fragments.MainFragment">

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

        <TextView
            android:textColor="@color/blacker"
            android:textStyle="bold"
            android:textSize="18sp"
            android:text="Техника для офиса"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="15dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="15dp" />

        <android.support.v7.widget.RecyclerView
            android:nestedScrollingEnabled="false"
            android:fillViewport="true"
            android:layout_marginBottom="15dp"
            android:layout_marginTop="15dp"
            android:id="@+id/recyclerView_popular_goods"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <TextView
            android:textColor="@color/blacker"
            android:textStyle="bold"
            android:textSize="18sp"
            android:text="Компьютерная техника"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="15dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="15dp" />

        <android.support.v7.widget.RecyclerView
            android:nestedScrollingEnabled="false"
            android:fillViewport="true"
            android:layout_marginBottom="15dp"
            android:layout_marginTop="15dp"
            android:id="@+id/recyclerView_competitive_goods"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <TextView
            android:textColor="@color/blacker"
            android:textStyle="bold"
            android:textSize="18sp"
            android:text="Канцелярские товары"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="15dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="15dp" />

        <android.support.v7.widget.RecyclerView
            android:visibility="gone"
            android:fillViewport="true"
            android:layout_marginBottom="15dp"
            android:layout_marginTop="15dp"
            android:id="@+id/recyclerView_stationery_goods"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

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

和:

RecyclerView recycleView1 = (RecyclerView) findViewById(R.id.recyclerView_popular_goods);
RecyclerView recycleView2 = (RecyclerView) findViewById(R.id.recyclerView_competitive_goods);
RecyclerView recycleView3 = (RecyclerView) findViewById(R.id.recyclerView_stationery_goods);

recycleView1.setNestedScrollingEnabled(false);
recycleView2.setNestedScrollingEnabled(false);
recycleView3.setNestedScrollingEnabled(false);

如果您的设备API低于v 21,请使用以下命令:

ViewCompat.setNestedScrollingEnabled(recycleView1, false);
ViewCompat.setNestedScrollingEnabled(recycleView2, false);
ViewCompat.setNestedScrollingEnabled(recycleView3, false);

答案 3 :(得分:0)

您的设计需要更改。在添加禁用了嵌套滚动的RecyclerView时,应牢记该视图将立即创建其所有项。

从根本上讲,当仅根据屏幕空间创建和渲染然后再使用所需数量的项目时,就没有典型的RecyclerView优化魔术了。

在您的情况下,情况更加糟糕,因为您拥有3个这样的RecyclerViews。解决方案的性能当然取决于硬件和每个RecyclerView中的项目数量,但是请务必考虑仅使用一个RecyclerView而根本不使用ScrollView。

答案 4 :(得分:0)

最后我实现了。我以NestedScrollView为根,只使用了1个recyclerview。并且在此recyclerview的适配器中,我创建了其他recyclerViews。平滑滚动感谢@Venky