我在一个应用程序上工作,recyclerView发出喀哒声,这让我知道onClickListener函数。它只是不显示下一个片段。对于是否应该使用FragmentManager以及在这种情况下应该放在哪里,我确实需要帮助。
HomeFragment
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.spirituality101.R
import com.example.spirituality101.R.drawable.*
import kotlinx.android.synthetic.main.fragment_home.*
class HomeFragment : Fragment(), ZodiacAdapter.MyItemClickListener {
private lateinit var homeViewModel: HomeViewModel
private lateinit var adapter: ZodiacAdapter
private var listener: OnRecyclerInteractionListener? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
homeViewModel =
ViewModelProviders.of(this).get(HomeViewModel::class.java)
return inflater.inflate(R.layout.fragment_home, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val linearLayoutManager: RecyclerView.LayoutManager
linearLayoutManager = LinearLayoutManager(view.context)
zodiacRec.setHasFixedSize(true)
zodiacRec.layoutManager = linearLayoutManager
ArrayList<Zodiac>().apply {
add(Zodiac("Aries", "Zodiac Date: March 21 - April 20, The Aries zodiac sign is the 1st sign of the zodiac. The Aries people are willful, feisty, self-centered, courageous, bold, foolhardy, independent and straightforward.",
aries
))
add(Zodiac("Taurus", "April 21 - May 21, The Taurus zodiac sign is the 2nd sign of the zodiac. The Taurus people are practical, creative, loyal, possessive, temperamental, sensually indulgent, down-to-earth, dependable, persistent and practical.",
taurus
))
add(Zodiac("Gemini", "May 22 - June 21, The Gemini zodiac sign is the 3rd sign of the zodiac. The Gemini people are curious, elusive, unpredictable, changeable, versatile, childlike, romantic, playful, friendly, talkative and have a keen intellect.",
gemini
))
add(Zodiac("Cancer", "June 22 - July 22, The Cancer zodiac sign is the 4th sign of the zodiac. The Cancer people are cautious, protective, nurturing, secretive, instinctive, needy, sensitive, funny, empathetic and deeply complex people.",
cancer
))
add(Zodiac("Leo", "July 23 - August 21, The Leo zodiac sign is the 5th sign of the zodiac. The Leos are distinctive, provocative, demanding, goal-oriented, flamboyant, self-made, warm, outgoing, sincere and loyal people.",
leo
))
add(Zodiac("Virgo", "August 22 - September 23, The Virgo zodiac sign is the 6th sign of the zodiac. The Virgos are discriminating, obsessive, realistic, analytical, reliable, self-contained, knowledgeable, predictable, obsessive, discriminating, street-smart, detailed and modest people.",
virgo
))
add(Zodiac("Libra", "September 24 - October 23, The Libra zodiac sign is the 7th sign of the zodiac. The Libra is the only zodiac sign that has an inanimate object, the scales, as its symbol. Librans are harmonious, civilized, intellectual, sophisticated, seductive, elegant, creative, witty, balanced, sociable and people who maintain their status quo.",
libra
))
add(Zodiac("Scorpio", "October 24 - November 22, The Scorpio zodiac sign is the 8th sign of the zodiac. The Scorpio people are intense, magnetic, erotic, challenging, secretive, powerful, broody, passionate, immovable and penetrating.",
scorpio
))
add(Zodiac("Sagittarius", "November 23 - December 22, The Sagittarius zodiac sign is the 9th sign of the zodiac. The Sagittarius are adventurous, hilarious, extrovert, romantics, spirited, unstoppable, generous, happy and open-minded.",
saggitarius
))
add(Zodiac("Capricorn", "December 23 - January 20, The Capricorn zodiac sign is the 10th sign of the zodiac. The Capricorn people are organized, respectful, devoted, classy, materialistic, serious, staid, ambitious and practical.",
capricorn
))
add(Zodiac("Aquarius", "January 21 - February 19, The Aquarius zodiac sign is the 11th sign of the zodiac. Aquarius people are original, idealistic, rebellious, independent, inventors, open minded and honest.",
aquarius
))
add(Zodiac("Pisces", "February 20 - March 20, The Pisces zodiac sign is the 12th sign of the zodiac. Pisceans are dreamy, erratic, creative, romantic, compassionate, elusive, imaginative, sensitive and kind people.",
pisces
))
adapter = ZodiacAdapter(this)
}
adapter.setMyItemClickListener(this)
zodiacRec.adapter = adapter
}
interface OnRecyclerInteractionListener {
fun onItemClicked(zodiac: Zodiac)
}
override fun onItemClickedFromAdapter(zodiac: Zodiac) {
onItemClickedFromRecyclerViewFragment(zodiac)
}
private fun onItemClickedFromRecyclerViewFragment(zodiac: Zodiac) {
listener?.onItemClicked(zodiac)
}
}
ZodiacAdapter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.spirituality101.R
class ZodiacAdapter(private val zodiacList: ArrayList<Zodiac>) : RecyclerView.Adapter<ZodiacAdapter.ViewHolder>() {
private var zodiacTable: MutableMap<String, Int> = mutableMapOf()
var myListener: MyItemClickListener? = null
init {
zodiacTable = mutableMapOf()
zodiacTable["Aries"] = R.drawable.aries
zodiacTable["Taurus"] = R.drawable.taurus
zodiacTable["Gemini"] = R.drawable.gemini
zodiacTable["Cancer"] = R.drawable.cancer
zodiacTable["Leo"] = R.drawable.leo
zodiacTable["Virgo"] = R.drawable.virgo
zodiacTable["Libra"] = R.drawable.libra
zodiacTable["Scorpio"] = R.drawable.scorpio
zodiacTable["Sagittarius"] =
R.drawable.saggitarius
zodiacTable["Capricorn"] =
R.drawable.capricorn
zodiacTable["Aquarius"] = R.drawable.aquarius
zodiacTable["Pisces"] = R.drawable.pisces
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var zodiacImage: ImageView = itemView.findViewById(R.id.z_image)
var zodiacName: TextView = itemView.findViewById(R.id.z_name)
var zodiacDescription: TextView = itemView.findViewById(R.id.z_desc)
init {
itemView.setOnClickListener {
if (myListener != null) {
if (adapterPosition !=
RecyclerView.NO_POSITION
) {
myListener!!.onItemClickedFromAdapter(zodiacList[adapterPosition])
}
}
}
}
}
override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): ViewHolder {
val v = LayoutInflater.from(viewGroup.context)
.inflate(R.layout.zodiac_design, viewGroup, false)
return ViewHolder(v)
}
override fun getItemCount(): Int {
return zodiacList.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val zodiac = zodiacList[position]
holder.zodiacName.text = zodiac.name
holder.zodiacDescription.text = zodiac.description
holder.zodiacImage.setImageResource(zodiac.photoId)
}
interface MyItemClickListener {
fun onItemClickedFromAdapter(zodiac : Zodiac)
}
fun setMyItemClickListener ( listener: MyItemClickListener){
this.myListener = listener
}
}
ZodiacDetailFragment
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.example.spirituality101.R
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
/**
* A simple [Fragment] subclass.
* Activities that contain this fragment must implement the
* [ZodiacDetailFragment.OnFragmentInteractionListener] interface
* to handle interaction events.
* Use the [ZodiacDetailFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class ZodiacDetailFragment : Fragment() {
private var listener: OnFragmentInteractionListener? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_zodiac_detail, container, false)
}
override fun onAttach(context: Context) {
super.onAttach(context)
if (context is OnFragmentInteractionListener) {
listener = context
} else {
throw RuntimeException("$context must implement OnFragmentInteractionListener")
}
}
override fun onDetach() {
super.onDetach()
listener = null
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
*
*
* See the Android Training lesson [Communicating with Other Fragments]
* (http://developer.android.com/training/basics/fragments/communicating.html)
* for more information.
*/
interface OnFragmentInteractionListener
companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment ZodiacDetail.
*/
@JvmStatic
fun newInstance(param1: String, param2: String) =
ZodiacDetailFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}
最后一个是单击回收器项目后要显示的那个。由于未显示页面,我没有将任何内容合并到“详细信息片段”中。我尝试使用FragmentManager,但在应用程序不断关闭的情况下,无法弄清楚将其放置在何处。