AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 77122056
Accepted
vilnius19
vilnius19
Asked: 2023-09-17 21:40:46 +0800 CST2023-09-17 21:40:46 +0800 CST 2023-09-17 21:40:46 +0800 CST

如何使recyclerviews在android中从右到左和从左到右水平滚动?

  • 772

我在一个片段中有 3 个 recyclerview,我希望每个 recyclerview 都位于另一个之下,然后当我滚动时,它们应该从左到左滚动。他们都不应该干扰其他回收者的观点。以下是我的片段的 xml 布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:background="@drawable/flash_tablebar"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        android:layout_alignParentTop="true">

        <ImageView
            android:id="@+id/back_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:src="@drawable/ic_arrow_back" />

        <RelativeLayout
            android:layout_marginLeft="20dp"
            android:id="@+id/relativeFlashcard"
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:layout_toRightOf="@id/back_icon">
            <TextView
                android:id="@+id/title_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:layout_marginStart="30dp"
                android:text="Flashcards"
                android:textSize="40sp"
                android:textColor="#FFF" />
            <TextView
                android:id="@+id/subtext_flash"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:layout_below="@id/title_text"
                android:layout_marginStart="30dp"
                android:text="Pick a set to practice"
                android:textSize="20sp"
                android:textColor="#FFF" />
        </RelativeLayout>
    </androidx.appcompat.widget.Toolbar>

    <LinearLayout
        android:layout_below="@id/toolbar"
        android:layout_width="match_parent"
        android:id="@+id/linear1"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="10dp">
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerViewFlashcardsEasy"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginEnd="10dp"
            android:layout_marginTop="10dp"
            android:layout_below="@id/toolbar"
            android:layout_alignParentBottom="true"
            android:scrollbars="horizontal">
        </androidx.recyclerview.widget.RecyclerView>
    </LinearLayout>


    <LinearLayout
        android:layout_below="@id/linear1"
        android:id="@+id/linear2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="10dp">
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerViewFlashcardsMedium"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginEnd="10dp"
            android:layout_marginTop="10dp"
            android:layout_below="@id/toolbar"
            android:layout_alignParentBottom="true"
            android:scrollbars="horizontal">
        </androidx.recyclerview.widget.RecyclerView>
    </LinearLayout>


    <LinearLayout
        android:id="@+id/linear3"
        android:layout_below="@id/linear2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="10dp">
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerViewFlashcardsHard"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginEnd="10dp"
            android:layout_marginTop="10dp"
            android:layout_below="@id/toolbar"
            android:layout_alignParentBottom="true"
            android:scrollbars="horizontal">
        </androidx.recyclerview.widget.RecyclerView>
    </LinearLayout>
</RelativeLayout>

item_flashcards.xml是Recylerviews中声明元素的布局文件

<androidx.core.widget.NestedScrollView
    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">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <androidx.cardview.widget.CardView
            android:layout_width="270dp"
            android:layout_height="150dp"
            android:layout_margin="10dp"
            android:layout_gravity="center"
            android:id="@+id/cardFlashCards"
            app:cardCornerRadius="25dp"
            app:cardElevation="4dp"
            >
            <LinearLayout
                android:layout_width="300dp"
                android:layout_height="200dp"
                android:gravity="center"
                android:layout_gravity="center"
                android:background="@drawable/flashcard_color"
                android:orientation="vertical">

                <ImageView
                    android:id="@+id/imageViewFlashcards"
                    android:layout_width="90dp"
                    android:layout_height="90dp"
                    android:layout_marginTop="20dp"
                    android:layout_gravity="center"
                    android:scaleType="fitXY"
                    android:background="@drawable/circular_flashcardview" />

                <TextView
                    android:id="@+id/textflashCardName"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Text1"
                    android:textColor="@color/white"
                    android:layout_marginTop="10dp"
                    android:gravity="center"
                    android:textStyle="bold"
                    android:textSize="18sp" />
                <TextView
                    android:id="@+id/textflashCardLithuanian"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Text2"
                    android:textColor="@color/white"
                    android:layout_marginBottom="10dp"
                    android:gravity="center"
                    android:textStyle="bold"
                    android:textSize="18sp" />
            </LinearLayout>
        </androidx.cardview.widget.CardView>
    </FrameLayout>

</androidx.core.widget.NestedScrollView>

以下是片段的代码

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.cardview.widget.CardView
import androidx.navigation.Navigation
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.visuallithuanian.R
import com.example.visuallithuanian.adapter.FlashcardsAdapter
import com.example.visuallithuanian.data.FlashCardInfo
import com.example.visuallithuanian.ui.activities.FirstScreen
import com.google.android.material.bottomnavigation.BottomNavigationView
import dagger.hilt.android.AndroidEntryPoint


@AndroidEntryPoint
class FlashCards : Fragment() {

    lateinit var bottomNav:BottomNavigationView

    @SuppressLint("MissingInflatedId")
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        // Inflate the layout for this fragment
        val view=inflater.inflate(R.layout.fragment_flash_cards, container, false)

        //Taking the bOTTOMNavigation view instance from Activity into Fragment
        bottomNav = (activity as? FirstScreen)?.findViewById(R.id.bottomNavigationView)!!
        bottomNav.visibility = View.VISIBLE

        val back_icon = view.findViewById<ImageView>(R.id.back_icon)
        val recyclerViewCardsEasy = view.findViewById<RecyclerView>(R.id.recyclerViewFlashcardsEasy)
        val recyclerViewCardsMedium = view.findViewById<RecyclerView>(R.id.recyclerViewFlashcardsMedium)
        val recyclerViewCardsHard = view.findViewById<RecyclerView>(R.id.recyclerViewFlashcardsHard)


        recyclerViewCardsHard.layoutManager = LinearLayoutManager(context)
        recyclerViewCardsMedium.layoutManager = LinearLayoutManager(context)
        recyclerViewCardsEasy.layoutManager = LinearLayoutManager(context)

        val navController = Navigation.findNavController(requireActivity(),
            R.id.nav_host_fragment
        )

        val flashCardList = generateFlashCards()
        val adapter = FlashcardsAdapter(flashCardList,navController)
        recyclerViewCardsHard.adapter = adapter

        val easyFlashCardsList = generateEasyFlashCards()
        val adapter1 = FlashcardsAdapter(easyFlashCardsList,navController)
        recyclerViewCardsEasy.adapter = adapter1

        val mediumFlashCardsList = generateMediumFlashCards()
        val adapter2 = FlashcardsAdapter(mediumFlashCardsList,navController)
        recyclerViewCardsMedium.adapter = adapter2



        // setting up listener
        back_icon.setOnClickListener {
            activity?.onBackPressed()
        }

        return view
    }

    private fun generateMediumFlashCards(): List<FlashCardInfo> {
        return listOf(
            FlashCardInfo(R.drawable.clothing,"Clothing",""),
            FlashCardInfo(R.drawable.village,"Towns and Villages",""),
            FlashCardInfo(R.drawable.nature,"Nature",""),

        )

    }

    private fun generateEasyFlashCards(): List<FlashCardInfo> {

        return listOf(
            FlashCardInfo(R.drawable.talking,"Daily Conversation",""),
            FlashCardInfo(R.drawable.food,"Food",""),
            FlashCardInfo(R.drawable.relatives,"Relatives",""),
        )

    }

    private fun generateFlashCards():List<FlashCardInfo> {
        return listOf(

            FlashCardInfo(R.drawable.doctorvisit,"Questions and Pronouns",""),
            FlashCardInfo(R.drawable.verbs,"Popular Verbs",""),
        )

    }


}

以下是适配器类的代码

package com.example.visuallithuanian.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.cardview.widget.CardView
import androidx.navigation.NavController
import androidx.navigation.findNavController
import androidx.recyclerview.widget.RecyclerView
import com.example.visuallithuanian.data.FlashCardInfo
import com.example.visuallithuanian.ui.activities.fragments.FlashCardsDirections


class FlashcardsAdapter(private val imageList: List<FlashCardInfo>
,  private val navController: NavController
) :RecyclerView.Adapter<FlashcardsAdapter.ViewHolder>(){

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val imageViewFlashcards = itemView.findViewById<ImageView>(com.example.visuallithuanian.R.id.imageViewFlashcards)
        val textViewFlashcards = itemView.findViewById<TextView>(com.example.visuallithuanian.R.id.textflashCardName)
        val textViewFlashcardsLithuanian = itemView.findViewById<TextView>(com.example.visuallithuanian.R.id.textflashCardLithuanian)

        val cardviewFlashcard = itemView.findViewById<CardView>(com.example.visuallithuanian.R.id.cardFlashCards)

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(com.example.visuallithuanian.R.layout.item_flashcards, parent, false)
        return ViewHolder(view)

    }

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

        holder.imageViewFlashcards.setImageResource(current_item.imageId)
        holder.textViewFlashcards.text = current_item.name
        holder.textViewFlashcardsLithuanian.text = current_item.translation
        
        holder.cardviewFlashcard.setOnClickListener {
            val action = when(position){
                    1 ->FlashCardsDirections.actionFlashCardsToQuestionsFragment()

                else -> return@setOnClickListener
            }
            action.let {
                navController.navigate(it)
            }

        }


    }


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

我哪里错了?我什至使那些父线性布局方向水平

android
  • 1 1 个回答
  • 22 Views

1 个回答

  • Voted
  1. Best Answer
    Tayfun YILDIRIM
    2023-09-17T22:04:09+08:002023-09-17T22:04:09+08:00

    您可以将 LinearLayoutManager 与 recyclerView 关联起来,如下所示:

     recyclerViewCardsHard.layoutManager = LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL, false)
     recyclerViewCardsMedium.layoutManager = LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL, false)
     recyclerViewCardsEasy.layoutManager = LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL, false)
    
    • 1

相关问题

  • 滚动 Recyclerview 应覆盖工具栏

  • 底部像素溢出并且导航功能不起作用

  • 从列表中选择一种颜色,在 Android 中找不到数组

  • 将 MapLibre 添加到新的 Android 项目不起作用

  • 我应该将矢量图像放入资源还是绘图中?我的应用程序中有数千个向量

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    使用 <font color="#xxx"> 突出显示 html 中的代码

    • 2 个回答
  • Marko Smith

    为什么在传递 {} 时重载解析更喜欢 std::nullptr_t 而不是类?

    • 1 个回答
  • Marko Smith

    您可以使用花括号初始化列表作为(默认)模板参数吗?

    • 2 个回答
  • Marko Smith

    为什么列表推导式在内部创建一个函数?

    • 1 个回答
  • Marko Smith

    我正在尝试仅使用海龟随机和数学模块来制作吃豆人游戏

    • 1 个回答
  • Marko Smith

    java.lang.NoSuchMethodError: 'void org.openqa.selenium.remote.http.ClientConfig.<init>(java.net.URI, java.time.Duration, java.time.Duratio

    • 3 个回答
  • Marko Smith

    为什么 'char -> int' 是提升,而 'char -> Short' 是转换(但不是提升)?

    • 4 个回答
  • Marko Smith

    为什么库中不调用全局变量的构造函数?

    • 1 个回答
  • Marko Smith

    std::common_reference_with 在元组上的行为不一致。哪个是对的?

    • 1 个回答
  • Marko Smith

    C++17 中 std::byte 只能按位运算?

    • 1 个回答
  • Martin Hope
    fbrereto 为什么在传递 {} 时重载解析更喜欢 std::nullptr_t 而不是类? 2023-12-21 00:31:04 +0800 CST
  • Martin Hope
    比尔盖子 您可以使用花括号初始化列表作为(默认)模板参数吗? 2023-12-17 10:02:06 +0800 CST
  • Martin Hope
    Amir reza Riahi 为什么列表推导式在内部创建一个函数? 2023-11-16 20:53:19 +0800 CST
  • Martin Hope
    Michael A fmt 格式 %H:%M:%S 不带小数 2023-11-11 01:13:05 +0800 CST
  • Martin Hope
    God I Hate Python C++20 的 std::views::filter 未正确过滤视图 2023-08-27 18:40:35 +0800 CST
  • Martin Hope
    LiDa Cute 为什么 'char -> int' 是提升,而 'char -> Short' 是转换(但不是提升)? 2023-08-24 20:46:59 +0800 CST
  • Martin Hope
    jabaa 为什么库中不调用全局变量的构造函数? 2023-08-18 07:15:20 +0800 CST
  • Martin Hope
    Panagiotis Syskakis std::common_reference_with 在元组上的行为不一致。哪个是对的? 2023-08-17 21:24:06 +0800 CST
  • Martin Hope
    Alex Guteniev 为什么编译器在这里错过矢量化? 2023-08-17 18:58:07 +0800 CST
  • Martin Hope
    wimalopaan C++17 中 std::byte 只能按位运算? 2023-08-17 17:13:58 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve