Issue
I’m still learning and I see that there’s a long way ahead of me. However I got stuck with my app and I can’t find any clues online so I need your help.
I have a problem with connect listView with RecyclerView: in the first ACTIVITY I have a list and after selecting the first position, my intention was that it should return a FRAGMENT with recycler view (and cardView). Unfortunately I try a lot of methods but I still don’t know the solution. Anyone help me, please? 😊
Animal Habitats
class AnimalHabitats : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_animal_habitats)
val itemClickListener =
OnItemClickListener { listView, v, position, id ->
if (position == 0) {
val intent = Intent(
this@AnimalHabitats,
SavannaFragment::class.java
)
startActivity(intent)
}
}
val listView = findViewById<View>(R.id.listAnimal) as ListView
listView.onItemClickListener = itemClickListener
}}
Fragment with CardView, RecyclerView
class SavannaFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val savannaRecyclerView =
inflater.inflate(R.layout.fragment_savanna, container, false) as RecyclerView
val savannaAnimalsName = arrayOfNulls<String>(Savanna.savannaArray.size)
for (i in savannaAnimalsName.indices) {
savannaAnimalsName[i] = Savanna.savannaArray[i].name
}
val savannaAnimalsImage = IntArray(Savanna.savannaArray.size)
for (i in savannaAnimalsImage.indices) {
savannaAnimalsImage[i] = Savanna.savannaArray[i].imageResourcesId
}
val adapter = RecyclerViewAdapter(savannaAnimalsName, savannaAnimalsImage)
savannaRecyclerView.adapter = adapter
val layoutManager = GridLayoutManager(activity, 2)
savannaRecyclerView.layoutManager = layoutManager
adapter.setListener(object : RecyclerViewAdapter.Listener {
override fun onClick(position: Int) {
val intent = Intent(activity, SavannaDetailActivity::class.java)
intent.putExtra(SavannaDetailActivity.EXTRA_ANIMAL_ID, position)
activity!!.startActivity(intent)
}
})
return savannaRecyclerView
}}
RecyclerViewAdapter
internal class RecyclerViewAdapter(
private val title: Array<String?>,
private val imageIds: IntArray) : RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>(){
private var listener: Listener? = null
internal interface Listener {
fun onClick(position: Int)
}
class ViewHolder(internal val cardView: CardView) : RecyclerView.ViewHolder(
cardView
)
override fun getItemCount(): Int {
return title.size
}
fun setListener(listener: Listener?) {
this.listener = listener
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val cv: CardView = LayoutInflater.from(parent.context)
.inflate(R.layout.card_view_animal, parent, false) as CardView
return ViewHolder(cv)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val cardView: CardView = holder.cardView
val imageView: ImageView = cardView.findViewById(R.id.info_image) as ImageView
val drawable = ContextCompat.getDrawable(cardView.context, imageIds[position])
imageView.setImageDrawable(drawable)
imageView.contentDescription = title[position]
val textView = cardView.findViewById(R.id.info_text) as TextView
textView.text = title[position]
cardView.setOnClickListener {
if (listener != null) {
listener!!.onClick(position)
}
}
}
}
Solution
Then do this in your activity to add fragment:
val itemClickListener =
OnItemClickListener { listView, v, position, id ->
if (position == 0) {
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.container,YOUR_FRAGMENT_NAME,YOUR_FRAGMENT_STRING_TAG);
transaction.addToBackStack(null);
transaction.commit();
}
}
//View id where do you want to display your fragment
R.id.container
//Instance of your fragment
YOUR_FRAGMENT_NAME
//Any String value to differentiate fragment from other fragments
YOUR_FRAGMENT_STRING_TAG
Answered By - Beant Singh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.