如何实现recyclerView,它可以垂直和水平同时滚动?

时间:2018-05-07 12:14:22

标签: android android-recyclerview

如何在Android开发过程中实现像JioTv这样的布局,其中可以垂直滚动查看电视频道列表及其节目,也可以水平滚动,但这个时间的电视频道名称仍然是固定的? ScreenShot from jioTv App is attached

2 个答案:

答案 0 :(得分:0)

我在一个类似的项目上工作,就像你在屏幕截图中提供的项目一样,我们使用的实际上是以这种方式嵌套的RecyclerViews:

  • 我们有一个垂直的RecyclerView,每个项目都是,代表一个频道
  • 每个实际上是一个水平的RecyclerView,每个项目都是 Box ,代表程序
  • 每个 Box 的宽度均以程序持续时间为单位

很抱歉,但我无法提供示例,因为它会有很多文件/代码。我希望这会有所帮助。

答案 1 :(得分:0)

您应该在RecyclerView中实施RecyclerView 主recyclelerView的适配器将数据列表传递给每行适配器

这是一个例子 Main fragment

这是主适配器

   class HistoryMainAdapter(val historyMainList: List<HistoryMainResponse.HistoryBean>, val context: Context) : RecyclerView.Adapter<HistoryMainAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HistoryMainAdapter.ViewHolder {
        val v = LayoutInflater.from(parent.context).inflate(R.layout.history_main_item, parent, false)
        return ViewHolder(v)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        var current = historyMainList[position]

        holder.history_main_level_value.text = EmojiCompat.get().process(current.level.toString())


        val adapter = HistoryPerLevelAdapter(current.submissions!!, context)
        holder.history_recyclerview.adapter = adapter


    }

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

    class ViewHolder(v: View) : RecyclerView.ViewHolder(v) {
        var history_main_level_value: TextView = v.findViewById(R.id.history_main_level_value)
        var history_recyclerview: RecyclerView = v.findViewById(R.id.history_recyclerview)
    }
}

XML:history_main_item

Main item

<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="wrap_content"
    tools:layout_editor_absoluteY="25dp">

    <TextView
        android:id="@+id/textView32"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="12dp"
        android:layout_marginTop="16dp"
        android:text="@string/level_text"
        android:textColor="#ff4a4a4a"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.text.emoji.widget.EmojiTextView
        android:id="@+id/history_main_level_value"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="2dp"
        android:layout_marginStart="2dp"
        android:textColor="#ff4a4a4a"
        app:layout_constraintBottom_toBottomOf="@+id/textView32"
        app:layout_constraintStart_toEndOf="@+id/textView32"
        app:layout_constraintTop_toTopOf="@+id/textView32"
        tools:text="1" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/history_recyclerview"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:orientation="horizontal"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView32"
        tools:listitem="@layout/history_main_level_item" />
</android.support.constraint.ConstraintLayout>

这是子适配器

class HistoryPerLevelAdapter(val historyMainList: List<HistorySubmissionsBean>, val context: Context) : RecyclerView.Adapter<HistoryPerLevelAdapter.ViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HistoryPerLevelAdapter.ViewHolder {
    val v = LayoutInflater.from(parent.context).inflate(R.layout.history_main_level_item, parent, false)
    return ViewHolder(v)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val current = historyMainList[position]

    holder.history_challenge_title.text = EmojiCompat.get().process(current.name!!)

    Picasso.with(context).load(Uri.parse(BASE_URL + current.video!!.thumbnail + tokenForImage)).placeholder(R.drawable.placeholder).into(holder.history_challenge_image)
    holder.history_submission_count.text = current.number_of_submissions.toString()

    if (current.score != null) {
        holder.history_submission_point.text = current.score.toString() + "pt"
        holder.history_submission_point.visibility = View.VISIBLE

    } else {
        holder.history_submission_point.visibility = View.GONE
    }

    holder.itemView.setOnClickListener {
        mySupportFragmentManager
                .beginTransaction()
                .replace(R.id.without_bottom_bar, HistorySubmissionsForChallengeFragment.newInstance(current._id), HistorySubmissionsForChallengeFragment::class.java.simpleName)
                .addToBackStack(HistorySubmissionsForChallengeFragment::class.java.simpleName)
                .commit()
    }


}

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

class ViewHolder(v: View) : RecyclerView.ViewHolder(v) {
    var history_challenge_title: TextView = v.findViewById(R.id.history_challenge_title)
    var history_challenge_image: ImageView = v.findViewById(R.id.history_challenge_image)
    var history_submission_count: TextView = v.findViewById(R.id.history_submission_count)
    var history_submission_point: TextView = v.findViewById(R.id.history_submission_point)
}}

XML:history_main_level_item

Row item

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
        android:id="@+id/cardView3"
        android:layout_width="170dp"
        android:layout_height="80dp"
        android:layout_margin="8dp"
        app:cardCornerRadius="7dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

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

            <ImageView
                android:id="@+id/history_challenge_image"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:scaleType="centerCrop"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <ImageView
                android:id="@+id/imageView17"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/scrim" />

            <android.support.text.emoji.widget.EmojiTextView
                android:id="@+id/history_challenge_title"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:textColor="@color/white"
                android:textSize="15sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                tools:text="T-Test (Without the ball)" />
        </android.support.constraint.ConstraintLayout>
    </android.support.v7.widget.CardView>

    <TextView
        android:id="@+id/history_submission_count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:drawableLeft="@drawable/submission_times"
        android:drawablePadding="8dp"
        android:textSize="13sp"
        app:layout_constraintStart_toStartOf="@+id/cardView3"
        app:layout_constraintTop_toBottomOf="@+id/cardView3"
        tools:text="2" />

    <TextView
        android:id="@+id/history_submission_point"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:drawableLeft="@drawable/points_icon"
        android:drawablePadding="8dp"
        android:textSize="13sp"
        app:layout_constraintStart_toEndOf="@+id/history_submission_count"
        app:layout_constraintTop_toBottomOf="@+id/cardView3"
        tools:text="2 pts" />

</android.support.constraint.ConstraintLayout>