Home > Blockchain >  How to load textView and imageView (from strings & drawables) into a fragment navigated to, from rec
How to load textView and imageView (from strings & drawables) into a fragment navigated to, from rec

Time:03-07

I asked a similar question here. But I feel I didn't express myself vividly. So I'm elaborating.

I want to load textView and imageView (from strings & drawables) on the fragment that is navigated to, from recyclerView.

Please note I am using navigation component.

I am kinda new to this, but I'm done some reach and find out that I could used intent, and just navigate to an activity and use intent putExtra to load whatever I want to load, but I am using single activity and mutiple fragment model. So I can't do this.

I have been at this for over a week, so I'd really appreciate any help. Even if it's just the name of the tool I need to use.. But I wouldn't mind some code, thanks a lot for your help, in advance.

I don't know what info you will need, so I'll just add what I feel is important. I will provide any info if requested thanks.

Here's the recyclerView Adapter, that navigate into the fragment:

class StoriesRecyclerAdapter(private val storiesList: ArrayList<StoriesDataClass>) : RecyclerView.Adapter<StoriesRecyclerAdapter.ViewHolder>() {

    // create new views
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        ...

        return ViewHolder(view)
    }

    // binds the list items to a view
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        ...

        // navigates to stories content destination
        holder.storiesCardView.setOnClickListener {
            val action = StoriesFragmentDirections.actionNavigationStoriesToStoriesContentFragment()
            holder.itemView.findNavController().navigate(action)
        }
    }

    // return the number of the items in the list
    override fun getItemCount(): Int {
        ...
    }

    // Holds the views for adding it to image and text
    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val storiesCardView : CardView = itemView.findViewById(R.id.stories_cardView)
        ...
    }
}

Please inform me if you need any more info, I sinerely thank you for your time and feedback.

CodePudding user response:

CodePudding user response:

you can use intent.extra to pass data

findNavController(R.id.main_content).setGraph(R.navigation.<you_nav_graph_xml>, intent.extras)

or bundle to pass data

val bundle = Bundle()
bundle.putString("some_argument", "some_value")
navController.setGraph(R.navigation.<you_nav_graph_xml>, bundle)
  • Related