【问题标题】:RecyclerView in Scrollview (nested?), how to bind items dynamically?Scrollview中的RecyclerView(嵌套?),如何动态绑定项目?
【发布时间】:2017-12-21 18:32:25
【问题描述】:

我正在创建一个简单的视图,其中顶部有一些元素,下面是 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
    }
}

【问题讨论】:

  • 在 Recycler 视图中添加标题视图。
  • 为recyclerview的项目中的图片添加一个尺寸作为占位符,这样它就不会占据1px,使它在屏幕上全部膨胀
  • mudit_sen - 我认为这是不可能的,因为在主 recyclerView 上方会有一个小的 recyclerView ..
  • realdm - 我认为它的情况不同,但稍后会看看 Marcos Vasconcelos - 它已经固定大小,问题出在其他地方

标签: android android-recyclerview scrollview android-nestedscrollview


【解决方案1】:

您可以尝试将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!
     }

【讨论】:

    猜你喜欢
    • 2020-12-05
    • 2020-09-07
    • 1970-01-01
    • 1970-01-01
    • 2020-07-05
    • 2020-05-31
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    相关资源
    最近更新 更多