android-fragments 我如何清除我的recyclerView数据?我已经尝试了很多东西,从相同类型的问题,但没有什么帮助我

qojgxg4l  于 2022-11-13  发布在  Android
关注(0)|答案(1)|浏览(131)

搜索电影片段

package com.example.moviemania.views
import android.os.Binder
import android.os.Bundle
import android.text.Editabl
import android.text.TextWatcher
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.Toast
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.moviemania.adapters.MovieAdapter
import com.example.moviemania.models.Movie
import com.example.moviemania.models.Movies
import com.example.moviemania.R
import com.example.moviemania.databinding.FragmentSearchMoviesBinding
import com.example.moviemania.viewModels.FavViewModel
import com.example.moviemania.viewModels.MovieViewModel

class SearchMoviesFragment : Fragment() {
    private lateinit var recyclerView: RecyclerView
    private val viewModel: MovieViewModel by viewModels()
    private val favViewModel: FavViewModel by viewModels()
    private lateinit var binding:FragmentSearchMoviesBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        binding = FragmentSearchMoviesBinding.inflate(layoutInflater,container,false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

        val toolbar = binding.toolbarSearchMoviesFragment
        (activity as MainActivity).setSupportActionBar(toolbar)
        (activity as MainActivity).getSupportActionBar()?.setDisplayShowHomeEnabled(true)
        (activity as MainActivity).getSupportActionBar()?.setDisplayHomeAsUpEnabled(true)
        toolbar.setNavigationOnClickListener {
            (activity as MainActivity).onBackPressed()
        }

        super.onViewCreated(view, savedInstanceState)
        recyclerView = binding.rvSearchMovie
        observeSearchResults()
        setListenersForSearch()
        observeResponseText()

    }

    private fun observeResponseText() = viewModel.responseText.observe(viewLifecycleOwner) {
        //Toast.makeText(this,it.toString(),Toast.LENGTH_SHORT).show()
        Toast.makeText(requireContext(), it, Toast.LENGTH_SHORT).show()
    }

    private fun observeSearchResults() = viewModel.searchReuslt.observe(viewLifecycleOwner) {
        recyclerView.layoutManager = GridLayoutManager(requireContext(), 2)
        val adapter = it?.let { MovieAdapter(it) }
        recyclerView.adapter = adapter
        adapter?.setOnItemClickListener(object : MovieAdapter.onItemClickListener {
            override fun onToggleClick(isFavorite: Boolean, item: Movies) {
                val movie = Movie(
                    movieName = item.original_title,
                    moviePoster = item.poster_path,
                    movieReleaseDate = item.release_date,
                    movieBanner = item.backdrop_path,
                    movieOverview = item.overview
                )
                if (isFavorite) favViewModel.removeMovie(movie) else favViewModel.insertMovie(movie)
            }

            override fun onItemClick(position: Int) {

                val movie = Movie(
                    movieName = it[position].original_title,
                    moviePoster = it[position].poster_path,
                    movieReleaseDate = it[position].release_date,
                    movieBanner = it[position].backdrop_path,
                    movieOverview = it[position].overview,
                    fav = it[position].fav
                )

                val action =
                    SearchMoviesFragmentDirections.actionSearchMoviesFragmentToMovieDetailFragment(
                        it[position].fav,
                        movie,
                        it[position].backdrop_path,
                        it[position].poster_path,
                        it[position].original_title,
                        it[position].release_date,
                        it[position].overview
                    )
                findNavController().navigate(action)
            }
        })
    }

    private fun setListenersForSearch() {

        val etSearchText = view?.findViewById<EditText>(R.id.et_movieSearch)

        etSearchText?.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable) {
                if (s.length > 3) {
                    viewModel.searchApiCall(s.toString())
                }
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }
        })
    }

}

影片适配器

package com.example.moviemania.adapters

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.moviemania.extensions.GlideImageExtension.loadImage
import com.example.moviemania.models.Movies
import com.example.moviemania.R
import com.example.moviemania.databinding.MovieListBinding

class MovieAdapter(private val list: List<Movies>) :
    RecyclerView.Adapter<MovieAdapter.MyViewHolder>() {

    private lateinit var myListener: onItemClickListener

    interface onItemClickListener {
        fun onToggleClick(isFavorite: Boolean, item: Movies)
        fun onItemClick(position: Int)
    }

    fun setOnItemClickListener(listener: onItemClickListener) {
        myListener = listener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val binding: MovieListBinding =
            MovieListBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return MyViewHolder(
            binding,
            myListener
        )
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val currentItem = list[position]
        holder.bind(currentItem)
    }

    override fun getItemCount(): Int {
        return list.size
    }

    inner class MyViewHolder(private val binding: MovieListBinding, listener: onItemClickListener) :
        RecyclerView.ViewHolder(binding.root) {
        init {
            itemView.setOnClickListener {
                with(listener) { onItemClick(adapterPosition) }
            }
        }

        fun bind(item: Movies) {
            item.poster_path?.let { binding.ivMoviePoster.loadImage(it) }
            binding.tvMovieName.text = item.original_title
            binding.movieReleaseDate.text = item.release_date
            binding.favIcon.setImageResource(if (item.fav) R.drawable.redheart else R.drawable.redheart_border)
            binding.favIcon.setOnClickListener {
                myListener.onToggleClick(item.fav, item)
                item.fav = !item.fav
                notifyItemChanged(adapterPosition)
            }
        }
    }

}

我希望当响应结果可用时,意味着搜索到的电影不存在,recyclerview应该清除,屏幕应该为空。目前在这种情况下,它只显示吐司,但上次成功搜索的数据仍然显示在屏幕上。我希望的是,数据从recyclerview中清除

5us2dqdw

5us2dqdw1#

当您必须对任何操作更新RecyclerView时,请将其添加到您的适配器

public fun setData(private val newList: List<Movies>) {
      lists.clear();
      lists.addAll(newList);
      notifyDataSetChanged();
   }

然后使用来自Activity/Fragment的新数据调用此方法
(查看带有新数据的模型);

相关问题