Scrollview中的RecyclerView(嵌套?),如何动态绑定项目?

问题描述 投票:1回答:1

我正在创建一个简单的视图,在顶部我有一些元素,在recyclerView下面。当我向下滚动时,想要滚动整个屏幕,而不是唯一的回收器。

我已经使用NestedScrollView实现了它,但现在出现了问题。列表中的项目将非常繁重,在此配置中,所有项目同时绑定(调用onBindViewHolder)。

任何想法如何使他们回收并解决这个问题?

这是我的xml文件

   <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/darker_gray"
    android:orientation="vertical"
    tools:context="com.gkuziel.testkotlin.MainActivity">


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

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/ic_available_stores_default" />

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="test text" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/list_test"

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:isScrollContainer="false"
            android:nestedScrollingEnabled="false">

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

更新:

找到了一个很好的解决方案:你添加一个复杂的标题作为ItemDecoration,它的主要原因是你的适配器可以保持不变,你只需添加这样的东西:recyclerView.addItemDecoration(dividerItemDecoration);这个解决方案的唯一缺点是我无法使这个标题可点击(在我的情况下,它包含另一个recyclerView),但我知道有些人也实现了它。

在这一刻,我决定实现异构的recyclerview,其中包含1个头类型实例和其他简单行类型。

重要的是,头文件类型在HeaderViewHolder构造函数中完全绑定一次,onBindViewHolder如下所示:

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
    if (holder is HeaderViewHolder) {
        //do nothing
        Log.d("ProductAdapter", "Binding: Header")

    } else if (holder is ItemViewHolder) {
        Log.d("ProductAdapter", "Binding: " + position.toString())
        val searchItem = items!![position - 1]

        //here the proper binding is going on
    }
}
android android-recyclerview scrollview android-nestedscrollview
1个回答
0
投票

您可以尝试将recyclerview布局管理器的方法canScrollVertical设置为false,它不会响应任何触摸内部滚动事件。

覆盖下面的方法并返回false。

boolean canScrollVertically()

这是设置方法。

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         // ...
         // Lookup the recyclerview in activity layout
         RecyclerView listTest = (RecyclerView) findViewById(R.id.list_test);

         // Attach the adapter to the recyclerview to populate items
         listTest.setAdapter(adapter);
         // Set layout manager to position the items
         listTest.setLayoutManager(new LinearLayoutManager(this){
         @Override
         public boolean canScrollVertically(){
         return false;
       }        
     });
         // That's all!
     }
© www.soinside.com 2019 - 2024. All rights reserved.