Home > Software engineering >  can't call the bottomsheet from the recyclerview adapter
can't call the bottomsheet from the recyclerview adapter

Time:03-01

i am doing movies app so i am using recycler view to list all the movies list and i need to show the details of the movie in bottomsheetdialog when user click on the movie list the bottom sheet crash the app please help me what is the error here i post my code this is my code with nested recyclerview and bottomsheet with it please help me this is recycleradapter


    class ListMoviesAdapter(
        private val movies: MutableList<Item>,
    ) :
        RecyclerView.Adapter<ViewHolder>() {
    
        inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            var contentImage: ImageView? = itemView.findViewById(R.id.contentImage)
            var contentTitle: TextView? = itemView.findViewById(R.id.contentTitle)
            var contentSubTitle1: TextView? = itemView.findViewById(R.id.contentSubtitle1)
        }
    
        interface onItemClickListener {
            fun onItemCLick(position: Int)
        }
    
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            val inflater = LayoutInflater.from(parent.context)
            val view = inflater.inflate(R.layout.item_6, parent, false)
            return ViewHolder(view)
        }
    
        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    
            val MoviesData: Item = movies.get(position)
    
            holder.contentImage?.let {
                Glide.with(it)
                    .load(MoviesData.media.thumbnail)
                    .fitCenter()
                    .into(holder.contentImage!!)
            }
            holder.contentImage?.setOnClickListener {
                val moviesFragment = MoviesFragment()
                val bottomsheet = DetailsFragment()
               bottomsheet.show(moviesFragment.requireActivity().supportFragmentManager,"Bottomsheet")
                Toast.makeText(holder.itemView.context, MoviesData.text.title, Toast.LENGTH_LONG).show()
            }
            holder.contentTitle?.text = MoviesData.text.title
            holder.contentSubTitle1?.text = MoviesData.text.subtitle1
        }
    
        override fun getItemCount(): Int {
            return movies.size
        }
    }

this is bottom sheet


    override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
    
        }
    
    //    override fun onAttach(context: Context) {
    //        adapter = ImageSliderAdapter(context)
    //        super.onAttach(context)
    //    }
    
        override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
    
            return inflater.inflate(R.layout.fragment_movies, container, false)
        }
    
        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            val imageslider = view.findViewById<SliderView>(R.id.image_slider)
            val slider: MutableList<Item> = mutableListOf()
            adapter = ImageSliderAdapter(requireContext())
            movie_recycle = view.findViewById(R.id.movie_recycle)
            imageslider.setSliderAdapter(adapter)
            partner_recycle = view.findViewById(R.id.partnets_scroll)
    
            imageslider.setIndicatorAnimation(IndicatorAnimationType.WORM) //set indicator animation by using SliderLayout.IndicatorAnimations. :WORM or THIN_WORM or COLOR or DROP or FILL or NONE or SCALE or SCALE_DOWN or SLIDE and SWAP!!
    
            imageslider.setSliderTransformAnimation(SliderAnimations.SIMPLETRANSFORMATION)
            imageslider.autoCycleDirection = SliderView.AUTO_CYCLE_DIRECTION_BACK_AND_FORTH
            imageslider.scrollTimeInSec = 3
            imageslider.isAutoCycle = true
            imageslider.startAutoCycle()
            setupView()
            getMovieList()
            adapter.addImageslider(slider)
            super.onViewCreated(view, savedInstanceState)
        }
    
        fun addmovielist(movies: MutableList<Item>) {
            adapter.addImageslider(movies)
        }
    
        private fun getMovieList() {
            val apicall = Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(BASE_URL)
                .build()
                .create(Api::class.java)
    
            val moviedata = apicall.GetMovieList()
    
            moviedata.enqueue(object : Callback<ImageSLider> {
    
                override fun onResponse(call: Call<ImageSLider>, response: Response<ImageSLider>) {
                    val responeBody = response.body()!!
                    val page = responeBody.page.body.rows[0].items
                    addmovielist(page)
                    partner_list(responeBody.page.body.rows[1].items)
                    val size = responeBody.page.body.rows.size - 1
    
                    for (data in 3..size) {
                        list_movies.add(responeBody.page.body.rows[data])
                    }
    
                    movies_list(list_movies)
                }
    
                override fun onFailure(call: Call<ImageSLider>, t: Throwable) {
                    Toast.makeText(requireActivity(), t.toString(), Toast.LENGTH_LONG).show()
                }
            })
        }
    
        @SuppressLint("NotifyDataSetChanged")
        fun partner_list(part_list: MutableList<Item>) {
            all_partner.addAll(part_list)
            partner_adapter = PartnerAdapter(all_partner)
            partner_recycle.adapter?.notifyDataSetChanged()
        }
    
        @SuppressLint("NotifyDataSetChanged")
        fun movies_list(movie_lst: MutableList<Rows>) {
            all_movies.addAll(movie_lst)
            ContentAdapter(all_movies)
            movie_recycle.adapter?.notifyDataSetChanged()
        }
    
        fun setupView() {
            partner_recycle.adapter = PartnerAdapter(all_partner)
            partner_recycle.layoutManager =
                LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false)
            movie_recycle.adapter = ContentAdapter(all_movies)
            movie_recycle.layoutManager =
                LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
        }
    
    }

this is nested recyclerview


    class ContentAdapter(val item: MutableList<Rows>) :
        RecyclerView.Adapter<ContentAdapter.ContentAdapterVH>() {
    
        var itemlist: MutableList<Item> = mutableListOf()
    
        class ContentAdapterVH(itemView: View?) : RecyclerView.ViewHolder(itemView!!) {
            val recycleMovie: RecyclerView? = itemView?.findViewById(R.id.movie_scroll)
            val categoryText: TextView? = itemView?.findViewById(R.id.title_movie_txt)
            val dashboardScreen = DashboardScreen()
            val moviesFragment = MoviesFragment()
            fun bind(data: Rows) {
    
                recycleMovie?.apply {
                    recycleMovie.adapter = ListMoviesAdapter(data.items, moviesFragment)
                    layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
                }
            }
        }
    
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ContentAdapterVH {
            val inflater = LayoutInflater.from(parent.context)
            val view = inflater.inflate(R.layout.item_6_list, parent, false)
            return ContentAdapterVH(view)
        }
    
        override fun onBindViewHolder(holder: ContentAdapterVH, position: Int) {
            val contentData: Rows = item.get(position)
    
            holder.categoryText?.text = contentData.text.title
            holder.bind(contentData)
        }
    
        override fun getItemCount(): Int {
            return item.size
        }
    }

please help to solve this or give any link to refer this thank you

CodePudding user response:

Just create a object class and create a method for bottom sheet and pass the context to this method. And from adapter, call this method on click on the item or where you want to display the bottom sheet Simple way is that you have to set click listener on onBindViewHolder method in adapter and directly call this showBottomSheet method. And pass the required data to it for displaying is in bottom sheet

object BottomSheetUtils {

    fun showBottomSheet(
        fragment: Fragment
    ) {
        val bottomSheetDialog = BottomSheetDialog(fragment.requireContext())
        bottomSheetDialog.setContentView(R.layout.your_custom_bottom_sheet_layout)
        
        // Just update UI and set click listeners etc...
        bottomSheetDialog.textviewName.text = "Example"

        bottomSheetDialog.show()
    }
}

CodePudding user response:


  

      override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            
                    val MoviesData: Item = movies.get(position)
            
                    holder.contentImage?.let {
                        Glide.with(it)
                            .load(MoviesData.media.thumbnail)
                            .fitCenter()
                            .into(holder.contentImage!!)
                    }
                    holder.contentImage?.setOnClickListener {
                        val moviesFragment = MoviesFragment()
                        val bottomsheet = DetailsFragment()
                       bottomsheet.show(appcompActivity.supportFragmentManager,"Bottomsheet")
                        Toast.makeText(holder.itemView.context, MoviesData.text.title, Toast.LENGTH_LONG).show()
                    }
                    holder.contentTitle?.text = MoviesData.text.title
                    holder.contentSubTitle1?.text = MoviesData.text.subtitle1
                }

I Pass appcompactivity to the bottom adapter and trigger the bottom sheet using support fragment manager thank you all

  • Related