Skip to main content
se añadieron 14 caracteres en el cuerpo; título editado
Origen Enlace
Jorgesys
  • 111.1k
  • 14
  • 55
  • 134

No muestra o carga el recycleViewrecyclerView

Estoy implementando un searchViewsearchView y mi problema es que al arrancar la aplicacionaplicación no muestra la lista a buscar  (recyclerViewrecyclerView), sino que me sale e vacio vació el recyclerrecyclerView. Cuando pulso la lupa(Icono) me lo carga perfectamente y la busquedabúsqueda me la realiza correctamente.mi

mi MainActivity:

No muestra o carga el recycleView

Estoy implementando un searchView y mi problema es que al arrancar la aplicacion no muestra la lista a buscar(recyclerView), sino que me sale e vacio el recycler. Cuando pulso la lupa(Icono) me lo carga perfectamente y la busqueda me la realiza correctamente.mi MainActivity:

No muestra o carga el recyclerView

Estoy implementando un searchView y mi problema es que al arrancar la aplicación no muestra la lista a buscar  (recyclerView), sino que me sale vació el recyclerView. Cuando pulso la lupa(Icono) me lo carga perfectamente y la búsqueda me la realiza correctamente.

mi MainActivity:

Origen Enlace
Rafel C.F
  • 543
  • 1
  • 6
  • 20

No muestra o carga el recycleView

Estoy implementando un searchView y mi problema es que al arrancar la aplicacion no muestra la lista a buscar(recyclerView), sino que me sale e vacio el recycler. Cuando pulso la lupa(Icono) me lo carga perfectamente y la busqueda me la realiza correctamente.mi MainActivity:

class MIDrawerActivity : AppCompatActivity(), View.OnClickListener {

private lateinit var mBinding: NavActivityMainBinding
private lateinit var mAdapter: FilmsDbAdapter
private lateinit var queryTextListener: SearchView.OnQueryTextListener
private lateinit var searchView: SearchView

var themeDrawable = R.drawable.ic_android

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    mBinding = NavActivityMainBinding.inflate(layoutInflater)
    setContentView(mBinding.root)

    // Set color for the container's content as transparent
    mBinding.drawerLayout.setScrimColor(Color.TRANSPARENT)

    mBinding.navHome.setOnClickListener(this)
    mBinding.navDoorIn.setOnClickListener(this)
    mBinding.navDoorOut.setOnClickListener(this)

    avoidDoubleClicks(mBinding.navDoorIn)
    slideType = MI_TYPE_DOOR_IN
    updateSliderTypeEvents()

    setSupportActionBar(mBinding.includeToolbar.toolbar)

    // Implement the drawer listener
    mBinding.drawerLayout.setMIDrawerListener(object : MIDrawerView.MIDrawerEvents {
        override fun onDrawerOpened(drawerView: View) {
            super.onDrawerOpened(drawerView)
            Log.d(TAG, "Drawer Opened")
        }

        override fun onDrawerClosed(drawerView: View) {
            super.onDrawerClosed(drawerView)
            Log.d(TAG, "Drawer closed")
        }
    })

    setupRecyclerView()
    setupActionBar()
    getAllFilmsDb()
    applyTheme()
}

private fun setupRecyclerView() {
    mAdapter = FilmsDbAdapter(this)
    mBinding.recyclerView.apply {
        setHasFixedSize(true)
        layoutManager = GridLayoutManager(this@MIDrawerActivity, 2)
        adapter = [email protected]
    }
}

private fun getFilmsDbFlow(dbEntity: DbEntity): Flow<Results> = flow<Results> {
    val films = dbEntity.results
        .filterNot { it.off_line == "1" }
        .sortedByDescending { it.f_subida }
        .sortedBy { it.off_line }

    films.forEach {
        emit(it)
    }
}.flowOn(Dispatchers.Default)

private fun getAllFilmsDb(){
    lifecycleScope.launch {
        getFilmsDbFlow(getFilmsDb()).collect {
            mAdapter.add(it)
        }
    }
}

/**
 * Retrofit
 * */
private fun setupActionBar() {
    lifecycleScope.launch {
        val actionbar = supportActionBar
        actionbar?.setDisplayHomeAsUpEnabled(true)
        actionbar?.setHomeAsUpIndicator(R.drawable.ic_home)
        formatResponse(getFilmsDb())
    }
}

private suspend fun getFilmsDb(): DbEntity = withContext(Dispatchers.IO){
    setupTitle(getString(R.string.main_retrofit_in_progress))

    val retrofit: Retrofit = Retrofit.Builder()
        .baseUrl(Constants.DB_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build()

    val service: DbService = retrofit.create(DbService::class.java)

    service.getFilmsDb()
}

private fun formatResponse(filmsDbEntity: DbEntity){

    val actionbar = supportActionBar
    val films = filmsDbEntity.results
        .filterNot { it.off_line == "1" }

    actionbar!!.title = ("Peliculas - ${films.count()}"
    )
}

private fun setupTitle(newTitle: String) {
    supportActionBar?.let { title = newTitle }
}

/**
 * OnClickListener
 * */
fun onClick(films: Results) {

    intent.putExtra("genero", "Peticiones")

    val intent = Intent(this, DetailsActivity::class.java)
    intent.putExtras(Bundle().apply {
        putParcelable("FILMS_DB", films)
    })
    startActivity(intent)
}

override fun onBackPressed() {
    if (mBinding.drawerLayout.isDrawerOpen(GravityCompat.START)) {
        mBinding.drawerLayout.closeDrawer(GravityCompat.START)
    } else {
        super.onBackPressed()
    }
}

override fun onClick(view: View) {
    when (view.id) {
        R.id.nav_home -> {
            intent.putExtra("genero", "Estrenos")

            val intent = Intent(this, MIDrawerActivity::class.java)
            startActivity(intent)

        }
    }
}

private fun updateSliderTypeEvents() {
    val mainHandler = Handler(Looper.getMainLooper())
    if (handler == null) {
        handler = mainHandler
        mBinding.drawerLayout.closeDrawer(GravityCompat.START)
        handler?.postDelayed(runnable, 500)
    }
}

var handler: Handler? = null
var runnable: Runnable = Runnable {
    when (slideType) {
        MI_TYPE_DOOR_IN -> {
            mBinding.includeToolbar.toolbar.title = [email protected](R.string.title_actionBar)
        }
        MI_TYPE_DOOR_OUT -> {
            mBinding.includeToolbar.toolbar.title = [email protected](R.string.title_actionBar)
        }
    }
    mBinding.drawerLayout.setSliderType(slideType)
    handler = null
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    when (item.itemId) {
        android.R.id.home -> {
            mBinding.drawerLayout.openDrawer(GravityCompat.START)
            return true
        }

        R.id.night_mode -> {
            chooseThemeDialogTheme()
            true
        }
    }
    return super.onOptionsItemSelected(item)
}

/**
 * Avoid double click.
 */
fun avoidDoubleClicks(view: View) {
    val DELAY_IN_MS: Long = 900
    if (!view.isClickable) {
        return
    }
    view.isClickable = false
    view.postDelayed({ view.isClickable = true }, DELAY_IN_MS)
}

override fun onCreateOptionsMenu(menu: Menu):Boolean {
    //inflater.inflate(R.menu.menu_main , menu)
    menuInflater.inflate(R.menu.menu_main, menu)

    menu.findItem(R.id.night_mode)
        ?.setIcon(ContextCompat.getDrawable(this, themeDrawable))

    val searchItem = menu.findItem(R.id.action_search)
    val searchManager = this.getSystemService(Context.SEARCH_SERVICE) as SearchManager
    if (searchItem != null) {
        searchView = searchItem.actionView as SearchView
    }
    searchView!!.setSearchableInfo(searchManager.getSearchableInfo(this.componentName))
    queryTextListener = object : SearchView.OnQueryTextListener {
        override fun onQueryTextChange(newText: String): Boolean {
           mAdapter.filter.filter(newText)
            Log.i("onQueryTextChange" , newText)
            return true
        }

        override fun onQueryTextSubmit(query: String): Boolean {
            searchView!!.clearFocus() //Oculta el teclado al pulsar la lupa
            Log.i("onQueryTextSubmit" , query)
            return true
        }
    }
    searchView!!.setOnQueryTextListener(queryTextListener)
    return true
}


private fun chooseThemeDialogTheme() {
    val builder = AlertDialog.Builder(this)
    builder.setTitle(getString(R.string.select_theme))
    val themeModes: Array<String> = resources.getStringArray(R.array.theme_modes)
    val itemSelected = Theme(this).themeMode
    builder.setSingleChoiceItems(themeModes, itemSelected) { dialog, which ->
        when (which) {
            0 -> {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
                Theme(this).themeMode = 0
                delegate.applyDayNight()
                dialog.dismiss()
            }
            1 -> {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
                Theme(this).themeMode = 1
                delegate.applyDayNight()
                dialog.dismiss()
            }
            2 -> {
                if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
                    AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY
                } else {
                    AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
                }
                Theme(this).themeMode = 2
                delegate.applyDayNight()
                dialog.dismiss()
            }

        }
    }

    val dialog = builder.create()
    dialog.show()
}

private fun applyTheme() {
    when (Theme(this).themeMode) {
        0 -> {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
            delegate.applyDayNight()
            themeDrawable = R.drawable.ic_light
        }
        1 -> {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
            delegate.applyDayNight()
            themeDrawable = R.drawable.ic_night
        }
        2 -> {
            if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
                AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY
            } else {
                AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
            }
            themeDrawable = R.drawable.ic_android
            delegate.applyDayNight()
        }
    }
}

}