我在一个片段中有 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
}
}
我哪里错了?我什至使那些父线性布局方向水平
您可以将 LinearLayoutManager 与 recyclerView 关联起来,如下所示: