代码之家  ›  专栏  ›  技术社区  ›  Manilal Kasera

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

  •  1
  • Manilal Kasera  · 技术社区  · 7 年前

    在Android开发期间,我如何实现JioTv这样的布局,其中可以垂直滚动电视频道列表及其节目,也可以水平滚动,但这次电视频道名称保持不变? ScreenShot from jioTv App is attached

    2 回复  |  直到 7 年前
        1
  •  0
  •   Andrew Hossam    7 年前

    您应该在RecyclerView内部实现RecyclerView 主循环视图的适配器将每行的数据列表传递给适配器

    下面是一个示例 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\u main\u项目

    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\u main\u level\u项目

    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>
    
        2
  •  0
  •   ReDLaNN    7 年前

    我从事的项目与您在屏幕截图中提供的项目类似,我们使用的实际上是嵌套的RecyclerViews,其方式如下:

    • 我们有一个垂直回收视图,每个项目都是 一行 表示通道的
    • 每个 一行 实际上是一个水平循环视图,每个项目都是 代表程序
    • 每个 宽度取决于节目持续时间(毫秒)

    很抱歉,我不能提供一个示例,因为它将有很多文件/代码。我希望这有帮助。