水平嵌套的recyclerviews在垂直父级中加载奇怪的信息

时间:2018-07-16 16:39:56

标签: android android-recyclerview

我已经尝试了一个星期的时间,将一个垂直recyclerview(父级)中的一些水平recyclerviews内加载项目。两者都需要在向下滚动时将水平滚动条向右和垂直滚动无休止。

现在无休止的滚动和加载工作。但是,这不能正常工作,因为水平的正在从其他子级加载疯狂的数据(我的猜测是我正在ViewHolder内创建演示者,并了解如何混合数据)

还有其他方法可以做到这一点。这可能比我的方法更好更快。另外,我可以注意到一些垂直加载不像在其他一些应用程序中看到的那样平滑。

任何帮助将不胜感激

class EventCatalogAdapter(private val presenter: EventCatalogPresenter):
        RecyclerView.Adapter<EventCatalogAdapter.ViewHolder>() {

    override fun getItemCount() = presenter.getCategoryEventCount()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
            EventCatalogAdapter.ViewHolder(parent.inflate(R.layout.item_event_catalog), viewType)

    override fun onBindViewHolder(holder: ViewHolder, position: Int) =
            presenter.bind(holder, position)

    override fun getItemViewType(position: Int) = presenter.getItemType(position)

    class ViewHolder(itemView: View, type: Int): RecyclerView.ViewHolder(itemView),
            EventCatalogItemView, EventViewList, OnItemClickListener<Event>,
            EndlessScrollListener.DataLoader {

        private lateinit var companyId: String
        private lateinit var catId: String
        private val eventPresenter = EventPresenterImpl(this)

        override val titleFormat: String? get() = ""

        init {
            val layoutManager = HorizontalLinearLayoutManager(itemView.context,
                    LinearLayoutManager.HORIZONTAL, false)
            itemView.rvEvents.layoutManager = layoutManager
            itemView.rvEvents.addItemDecoration(HorizontalItemDecorator(itemView.context))
            itemView.rvEvents.adapter = EventAdapter(eventPresenter, type, this)
            itemView.rvEvents.addOnScrollListener(EndlessScrollListener(layoutManager, this))
        }

        override fun setTitleVisibility(b: Boolean) {
            itemView.tvCategoryTitle.visibility = if(!b) View.GONE else View.VISIBLE
        }

        override fun setCategoryTitle(title: String) {
            itemView.tvCategoryTitle.text = title
        }

        override fun eventsByCategory(companyId: String, categoryId: String) {
            this.companyId = companyId
            catId = categoryId
            eventPresenter.getEventsByCategories(companyId, categoryId)
        }

        override fun loadMoreData(totalItems: Int) {
            Log.d("LOAD", "Event presenter: $eventPresenter for category: $catId")
            eventPresenter.getMoreEventsByCategories(companyId, catId, totalItems)
        }

        override fun setActiveEvent(event: Event?) {}

        override fun showSettings() {}

        override fun sendMessage(action: String, bundle: Bundle?) {}

        override fun showStatus(status: Int) {}

        override fun refresh() {
            itemView.rvEvents.adapter.notifyDataSetChanged()
        }

        override fun showMessageTemplate(code: Int) {}

        override fun hideMessageTemplate() {
            refresh()
        }

        override fun onItemClick(item: Event) {

        }

    }

}

class EventAdapter(private val presenter: EventPresenter,
                   private val listener: OnItemClickListener<Event>):
        RecyclerView.Adapter<EventAdapter.ViewHolderItemView>() {

    private var type: Int = EVENT_STANDARD

    constructor(presenter: EventPresenter, type: Int, listener: OnItemClickListener<Event>) :
            this(presenter, listener) {
        this.type = type
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
            ViewHolderItemView(parent.inflate(type))

    override fun onBindViewHolder(holder: ViewHolderItemView, position: Int) =
            presenter.bind(holder, position)

    override fun getItemCount() = presenter.getCount()

    class ViewHolderItemView(itemView: View) : RecyclerView.ViewHolder(itemView), EventItemView {

        override fun setEventDate(date: Date?) {
            itemView.tvDate.text = Tools.formatDate(itemView.context, date)
        }

        override fun setName(name: String) {
            itemView.tvName.text = name
        }

        override fun setScannerVisibility(scannerVisibility: Boolean) {
            itemView.ibScanner.visibility = if (scannerVisibility) View.VISIBLE else View.INVISIBLE
        }

        override fun setEventPoster(posterUrl: String, transformation: Transformation?) {
            Tools.loadImage(posterUrl, itemView.ivPoster, transformation, R.mipmap.portrait_test)
        }

        override fun setTotalRegistrants(totalRegistrants: Long) {
            itemView.tvQtyRegs.text = totalRegistrants.toString()
        }

        override fun addScanAction(event: Event) {
            itemView.ibScanner.setOnClickListener {
                val auth = FirebaseAuth.getInstance()
                val prefs = PreferenceHelper.customPrefs(itemView.context, auth.currentUser!!.uid)
                prefs.edit().putString(EventInteractorImpl.FIELD_EVENT_ID, event.eventId).apply()
                (itemView.context as SettingsActivity).launchScanner()
            }
        }

        override fun addEventAction(event: Event) {
            itemView.setOnClickListener {
                (itemView.context as SettingsActivity).showRegistrants(event)
            }
        }

    }

}

// Parent items:

<android.support.constraint.ConstraintLayout
    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="220dp">

    <TextView
        android:id="@+id/tvCategoryTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/vertical_spacing"
        android:paddingBottom="@dimen/vertical_spacing"
        android:layout_marginEnd="@dimen/horizontal_spacing"
        android:layout_marginStart="@dimen/horizontal_spacing"
        android:textAlignment="center"
        android:textSize="18sp"
        android:textAllCaps="true"
        android:fontFamily="sans-serif-light"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="@string/txt_cat_title" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rvEvents"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginBottom="@dimen/vertical_spacing"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvCategoryTitle" />

</android.support.constraint.ConstraintLayout>

// Children items

<android.support.constraint.ConstraintLayout
    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="110dp"
    android:layout_height="match_parent"
    android:stateListAnimator="@animator/tile_elevation">

        <ImageView
            android:id="@+id/ivPoster"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:contentDescription="@string/txt_event_image"
            android:src="@mipmap/portrait_test"
            android:scaleType="centerCrop"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/tvName"
            style="@style/SubTitle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textIsSelectable="false"
            android:textAllCaps="true"
            app:layout_constraintBottom_toTopOf="@+id/ibRegistrants"
            app:layout_constraintStart_toStartOf="@+id/ibRegistrants"
            tools:text="Washington D.C. " />

        <TextView
            android:id="@+id/tvDate"
            style="@style/SubTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textIsSelectable="false"
            app:layout_constraintBottom_toTopOf="@+id/tvName"
            app:layout_constraintStart_toStartOf="@+id/ibRegistrants"
            tools:text="03/23" />

        <TextView
            android:id="@+id/tvQtyRegs"
            style="@style/Title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:textIsSelectable="false"
            app:layout_constraintBottom_toBottomOf="@+id/ibRegistrants"
            app:layout_constraintStart_toEndOf="@+id/ibRegistrants"
            app:layout_constraintTop_toTopOf="@+id/ibRegistrants"
            tools:text="140" />

        <ImageView
            android:id="@+id/ibRegistrants"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginStart="8dp"
            android:contentDescription="@string/txt_registrants"
            android:src="@drawable/ic_registrants_48px"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

        <ImageButton
            android:id="@+id/ibScanner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="?attr/selectableItemBackgroundBorderless"
            android:contentDescription="@string/txt_registrants"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/ic_scan_action" />

</android.support.constraint.ConstraintLayout>

0 个答案:

没有答案