元素的宽度和高度放大时,Recyclerview缺少项

时间:2018-07-06 09:35:26

标签: android android-recyclerview kotlin recyclerview-layout

我一直在为这个问题挠头。我在一个recyclerview内部有一个recyclerview,该视图显示了网格布局,您可以在其中垂直和水平滚动。

我决定扩大每个项目的宽度和高度。以前,宽度和高度是:80 x 100

enter image description here

<?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:id="@+id/book_container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/inner_dropshadow"
        android:layout_width="80dp"
        android:layout_height="100dp"
        android:scaleType="fitXY"
        app:layout_constraintBottom_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/bookdropshadow" />

</android.support.constraint.ConstraintLayout>

然后将其放大至150x180。然后,无论我向上滚动多少,第4-6级行都完全消失。我只希望recyclerview显示更大的图像。为什么会这样?

[EDIT]我运行了一个调试程序,当在手机上运行该应用程序时,似乎在外部适配器中仅绑定了3个位置,而在平板电脑上运行时有4个位置。

enter image description here

<?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:id="@+id/book_container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/inner_dropshadow"
        android:layout_width="150dp"
        android:layout_height="180dp"
        android:scaleType="fitXY"
        app:layout_constraintBottom_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/bookdropshadow" />

</android.support.constraint.ConstraintLayout>

请注意,当我在设备上运行应用程序时,也会发生这种情况。在平板电脑中,它显示所有级别,而在电话中,它仅显示1-3级。

外部回收站视图布局

<?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:id="@+id/mainlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/color_white">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v4.widget.SwipeRefreshLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:id="@+id/swiperefresh"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="@+id/toolbar">

            <ScrollView
                android:id="@+id/scrollView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:overScrollMode="always">


                <android.support.constraint.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <include layout="@layout/browsebooksheader_layout"
                        android:id="@+id/bb_header"
                        app:layout_constraintBottom_toTopOf="@+id/guidelinetop"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="parent"/>

                    <android.support.constraint.Guideline
                        android:id="@+id/guidelinetop"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        app:layout_constraintGuide_percent="0.30"/>

                    <Button
                        android:id="@+id/btn_search"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center_horizontal"
                        android:background="@drawable/search_btn_border"
                        android:padding="8dp"
                        android:text="Search"
                        android:textSize="@dimen/browse_search_textsize"
                        android:textColor="@android:color/darker_gray"
                        app:layout_constraintBottom_toTopOf="@+id/recyclerview_main"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@+id/bb_header" />

                    <ProgressBar
                        android:id="@+id/main_progressbar"
                        style="?android:attr/progressBarStyleSmall"
                        android:layout_marginTop="16dp"
                        android:layout_width="50dp"
                        android:layout_height="50dp"
                        android:layout_gravity="center"
                        android:layout_centerHorizontal="true"
                        android:layout_centerVertical="true"
                        android:indeterminateTint="@color/color_grey"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@+id/btn_search"/>


                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/recyclerview_main"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:nestedScrollingEnabled="false"
                        android:background="@color/color_white"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@+id/btn_search"
                        app:layout_constraintBottom_toBottomOf="parent"/>

                </android.support.constraint.ConstraintLayout>
            </ScrollView>



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




        <android.support.constraint.ConstraintLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/toolbar"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@id/swiperefresh"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="parent">

            <include layout="@layout/toolbar_layout_wo_learn"/>

        </android.support.constraint.ConstraintLayout>

    </android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>

内部回收站视图布局

<?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="wrap_content">

    <TextView
        android:id="@+id/book_category"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Category Title"
        android:textColor="@color/defaultdark_color"
        android:textSize="@dimen/browse_category_textsize"
        android:fontFamily="@font/vagroundedstd_bold"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:layout_width="@dimen/browse_seeall_imagesize"
        android:layout_height="@dimen/browse_seeall_imagesize"
        android:layout_marginBottom="2dp"
        android:layout_marginTop="2dp"
        android:layout_marginEnd="8dp"
        android:id="@+id/see_all_btn"
        android:src="@drawable/seeall"
        app:layout_constraintBottom_toTopOf="@+id/inner_recyclerview"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/inner_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:nestedScrollingEnabled="false"
        android:background="@color/color_white"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/book_category"/>

    <TextView
        android:id="@+id/h_line"
        android:layout_width="match_parent"
        android:layout_height="0.8dp"
        android:background="@color/color_grey"
        android:gravity="center"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:alpha="0.5"
        app:layout_constraintTop_toBottomOf="@id/inner_recyclerview"/>


</android.support.constraint.ConstraintLayout>

外部适配器

class BrowseBooksAdapter : RecyclerView.Adapter<BrowseBooksAdapter.SimpleViewHolder>() {

    private var sortedList : SortedMap<String, List<ResourcesList>> = emptyMap<String, List<ResourcesList>>().toSortedMap()

    fun setData(sortedList : SortedMap<String, List<ResourcesList>>) {
        if (this.sortedList != sortedList) {
            this.sortedList = sortedList
            notifyDataSetChanged()
        }
    }

    override fun getItemCount(): Int {
        return sortedList.count()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SimpleViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.browsebooks_layout, parent, false)
        return SimpleViewHolder(view)
    }

    override fun onBindViewHolder(holder: SimpleViewHolder, position: Int) {

        val dataList = ArrayList(sortedList.values)[position]
        holder.category_text.text = ArrayList(sortedList.keys)[position]
        holder.booksByCategory = dataList
        holder.horizontalAdapter.setData(dataList)
    }

    class SimpleViewHolder(v: View, var booksByCategory : List<ResourcesList>? = null) : RecyclerView.ViewHolder(v) {
        var recyclerView : RecyclerView = v.findViewById(R.id.inner_recyclerview)
        var category_text : TextView = v.findViewById(R.id.book_category)
        var fragmentCallback: FragmentCallback? = null
        val horizontalAdapter: BooksByCategoryAdapter


        init {
            recyclerView.layoutManager = object : LinearLayoutManager(v.context, LinearLayoutManager.HORIZONTAL, false) {
                override fun canScrollHorizontally(): Boolean {
                    return true
                }

                override fun canScrollVertically(): Boolean {
                    return false
                }
            }
            horizontalAdapter = BooksByCategoryAdapter()
            recyclerView.adapter = horizontalAdapter
            fragmentCallback = v.context as FragmentCallback
            var mLastClickTime : Long = 0


            v.see_all_btn.setOnClickListener {
                val category = v.book_category.text.toString()

                // double-clicking prevention, using threshold of 1000 ms
                if (!(SystemClock.elapsedRealtime() - mLastClickTime < 1000)){
                    mLastClickTime = SystemClock.elapsedRealtime()

                    if(fragmentCallback != null){
                        fragmentCallback!!.callBackSeeAllFragment(booksByCategory!!, category)
                    }
                }

            }
        }

    }

}

内部适配器

class BooksByCategoryAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

private var booksByCategory: List<ResourcesList> = emptyList()

fun setData(data: List<ResourcesList>) {
    if (this.booksByCategory != data) {
        this.booksByCategory = data
        notifyDataSetChanged()
    }
}

override fun getItemCount(): Int {
    return booksByCategory.size
}


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
    val layoutInflater = LayoutInflater.from(parent.context)
    val cellForRow = layoutInflater.inflate(R.layout.bookcontainer_layout, parent, false)
    return CustomVH(cellForRow)
}

override fun onBindViewHolder(rawHolder: RecyclerView.ViewHolder, position: Int) {
    val holder = rawHolder as CustomVH
    val booksProcessModel = BooksProcessModel()
    booksProcessModel.setItem(booksByCategory[position])
    holder.booksByCategory = booksByCategory[position]
    Picasso.get().load(booksByCategory[position].coverURI).into(holder.inner_bookcover)


}

private inner class CustomVH(v: View, var booksByCategory : ResourcesList? = null): RecyclerView.ViewHolder(v) {

    var inner_bookcover : ImageView = v.findViewById(R.id.inner_bookcover)
    var fragmentCallback: FragmentCallback? = null
    var ctx : Context = v.context
    var mLastClickTime : Long = 0

    init {
        fragmentCallback = ctx as FragmentCallback

        v.setOnClickListener {

            // double-clicking prevention, using threshold of 1000 ms
            if (!(SystemClock.elapsedRealtime() - mLastClickTime < 1000)){
                mLastClickTime = SystemClock.elapsedRealtime()

                if(fragmentCallback != null){
                    fragmentCallback!!.callBackDownloadBookFragment(booksByCategory!!)
                }
            }

        }

    }
}

class BooksProcessModel {

    private var processBooks : ResourcesList? = null

    fun setItem(books: ResourcesList) {
        this.processBooks = books
    }

    fun setVisibility(ctx: Context) : Int {

        val sp = PreferenceManager.getDefaultSharedPreferences(ctx)
        val page = sp.getString(processBooks!!.id.toString(), "")
        if (page != "") {
            return View.VISIBLE
        }
        return View.INVISIBLE
    }

}

}

3 个答案:

答案 0 :(得分:0)

您需要将外部回收站视图的layout_height更改为wrap_content

答案 1 :(得分:0)

没关系,想通了。我应该使用NestedScrollView而不是ScrollView。丢失的物品仍然在那里。只是我无法滚动嵌套的recyclerview。

答案 2 :(得分:0)

就我而言,这是因为我直接拥有 RecyclerView,即使它滚动(水平),如果例如出现键盘,也会发生同样的事情。

解决方案:将 RecyclerView 放入 NestedScrollView 并将滚动委托给它,如下所示:

<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:listitem="@layout/item"/>
</androidx.core.widget.NestedScrollView>