我有各种Cardviews,我想在点击它们后转到每个特定的片段。我使用导航图,我想使用NavController转到另一个片段。我试图使用导航,但我只是缺少一些东西。下面是我的代码。FlashcardsAdapter.kt
class FlashcardsAdapter(private val imageList: List<FlashCardInfo>) :RecyclerView.Adapter<FlashcardsAdapter.ViewHolder>(){
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)
}
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
val imageFlashcards = holder.itemView.findViewById<ImageView>(com.example.visuallithuanian.R.id.imageViewFlashcards)
val textViewFlashCards = holder.itemView.findViewById<TextView>(com.example.visuallithuanian.R.id.textflashCardName)
val cardviewFlashcard = holder.itemView.findViewById<CardView>(com.example.visuallithuanian.R.id.cardFlashCards)
val textLithuanian = holder.itemView.findViewById<TextView>(com.example.visuallithuanian.R.id.textflashCardLithuanian)
}
override fun getItemCount(): Int {
return imageList.size
}
}
Flashcards.kt
class FlashCards : Fragment() {
lateinit var bottomNav:BottomNavigationView
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 recyclerViewCards1 = view.findViewById<RecyclerView>(R.id.recyclerViewFlashcards)
recyclerViewCards1.layoutManager = LinearLayoutManager(context)
val navController = Navigation.findNavController(requireActivity(),
R.id.nav_host_fragment
)
val flashCardList = generateFlashCards()
val adapter = FlashcardsAdapter(flashCardList)
recyclerViewCards1.adapter = adapter
// setting up listener
back_icon.setOnClickListener {
activity?.onBackPressed()
}
return view
}
private fun generateFlashCards():List<FlashCardInfo> {
return listOf(
FlashCardInfo(R.drawable.talking,"Daily Conversation","Kasdienis pokalbis"),
FlashCardInfo(R.drawable.doctorvisit,"Questions and Pronouns","Klausimai ir įvardžiai"),
FlashCardInfo(R.drawable.food,"Food","Maistas"),
FlashCardInfo(R.drawable.relatives,"Relatives","Giminaičiai"),
FlashCardInfo(R.drawable.clothing,"Clothing","Drabužiai"),
FlashCardInfo(R.drawable.village,"Towns and Villages","Miestai ir kaimai"),
FlashCardInfo(R.drawable.nature,"Nature","Gamta"),
FlashCardInfo(R.drawable.verbs,"Popular Verbs","Populiariausi veiksmažodžiai"),
)
}
这里列表中的所有元素都是cardview元素,每个元素都有一个片段,下面是我的xml布局文件。
fragments_flash_cards.xml
<RelativeLayout
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="match_parent">
<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_marginLeft="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_marginLeft="30dp"
android:text="Pick a set to practice"
android:textSize="20sp"
android:textColor="#FFF" />
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerViewFlashcards"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_below="@id/toolbar"
android:layout_alignParentBottom="true"
android:scrollbars="vertical" />
</RelativeLayout>
item_flashcards.xml
<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="10dp"
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>
1条答案
按热度按时间0yycz8jy1#
您需要的是一个
CLickListener
,在绑定ViewHolders
时通过Adapter
在所有项目上设置。当单击项目时,FlashFragment
会收到通知,然后可以处理导航到下一个Fragment
。你可以使用ListAdapter来简化你的适配器。它为
RecyclerView
中的数据列表提供了基本的处理。