RecyclerView inside bottom sheet is not displayed

40 Views Asked by At

I'm trying to place a RecyclerView inside a bottom sheet dialog, but the problem is my RecyclerView is not displayed.

I saw solutions related to this, but none of them worked for me.

What should I do? Any help is appreciated.

Main UI

Bottom sheet with no RecyclerView

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fragment_container"/>

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <com.google.android.material.bottomappbar.BottomAppBar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/bottomAppBar"
            android:layout_gravity="bottom"
            android:backgroundTint="@color/green"
            app:fabCradleMargin="10dp"
            app:fabCradleRoundedCornerRadius="50dp">
            <com.google.android.material.bottomnavigation.BottomNavigationView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/bottom_navigation"
                app:labelVisibilityMode="labeled"
                app:menu="@menu/bottom_menu"
                android:background="@android:color/transparent"/>
        </com.google.android.material.bottomappbar.BottomAppBar>
        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:backgroundTint="@color/green"
            android:src="@drawable/ic_autorenew"
            app:layout_anchor="@id/bottomAppBar"
            app:maxImageSize="40dp"
            android:id="@+id/fab"/>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</RelativeLayout>

</androidx.drawerlayout.widget.DrawerLayout>

bottomsheetlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/dialogbg"
    tools:context=".MainActivity">
    <ImageView
        android:id="@+id/image"
        android:layout_width="70dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ic_round_remove_24"
        android:layout_gravity="center|top"
        android:scaleType="centerCrop"/>
<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_alignTop="@id/image"
    android:layout_marginTop="30dp"
    android:fontFamily="@font/anjoman_black"
    android:gravity="center"
    android:text="انتخاب دستگاه"
    android:textColor="@color/black"
    android:textSize="18sp" />

    <LinearLayout
        android:layout_centerHorizontal="true"
        android:layout_marginTop="80dp"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/mRecycler"
            tools:listitem="@layout/item_device"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</RelativeLayout>

DeviceSelection data class

data class DeviceSelection(val name: String, var isSelected: Boolean)

DeviceSelectionAdapter.kt

class DeviceSelectionAdapter(val data: MutableList<DeviceSelection>, private val listener : (DeviceSelection) -> Unit): RecyclerView.Adapter<DeviceSelectionAdapter.ViewHolder>() {
    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {

    var deviceName: TextView = view.findViewById(R.id.device_name)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DeviceSelectionAdapter.ViewHolder {
    //parent.context
    val view = LayoutInflater.from(parent.context).inflate(R.layout.item_device,parent,false)
    return ViewHolder(view)
}

override fun onBindViewHolder(holder: DeviceSelectionAdapter.ViewHolder, position: Int) {
    val list = data[position]
    holder.deviceName.text = list.name

    if(list.isSelected)
        holder.deviceName.setBackgroundColor(Color.RED)
    else
        holder.deviceName.setBackgroundColor(Color.GREEN)

    holder.deviceName.setOnClickListener {
        data.forEach {
            it.isSelected = false
        }
        list.isSelected = true
        notifyDataSetChanged()
    }
}

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

}

MainActivity.kt

class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
private lateinit var fragmentManager : FragmentManager
private lateinit var binding: ActivityMainBinding
private lateinit var dataRecyclerView : RecyclerView
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding.root)

    binding.bottomNavigation.background = null
    binding.bottomNavigation.setOnItemSelectedListener { item ->
        when(item.itemId){
            R.id.bottom_home -> openFragment(HomeFragment())
            R.id.bottom_settings -> openFragment(SettingsFragment())
            R.id.bottom_inputs -> openFragment(InputsFragment())
            R.id.bottom_outputs -> openFragment(OutputsFragment())
        }
        true
    }
    fragmentManager = supportFragmentManager
    openFragment(HomeFragment())
    binding.fab.setOnClickListener {
        //Toast.makeText(this@MainActivity,"Hey",Toast.LENGTH_SHORT).show()
        showBottomDialog()
    }
}

override fun onNavigationItemSelected(item: MenuItem): Boolean {
    TODO("Not yet implemented")
}

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

private fun openFragment(fragment: Fragment){
    val fragmentTransaction:FragmentTransaction = fragmentManager.beginTransaction()
    fragmentTransaction.replace(R.id.fragment_container,fragment)
    fragmentTransaction.commit()
}

private fun showBottomDialog() {
    val inflater = LayoutInflater.from(this)
    val v = inflater.inflate(R.layout.bottomsheetlayout, null)
    dataRecyclerView = v.findViewById(R.id.mRecycler)
    val data = mutableListOf(DeviceSelection("دستگاه 1",false),DeviceSelection("دستگاه 2",false))
    val recyclerAdapter = DeviceSelectionAdapter(data= data, listener = {
        Toast.makeText(this@MainActivity, it.name, Toast.LENGTH_SHORT).show()
    })
    androidx.core.view.ViewCompat.setNestedScrollingEnabled(dataRecyclerView, false);
    dataRecyclerView.layoutManager=LinearLayoutManager(this)
    //dataRecyclerView.layoutManager = LinearLayoutManager(this)
    dataRecyclerView.adapter = recyclerAdapter

    val dialog = Dialog(this)
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
    dialog.setContentView(R.layout.bottomsheetlayout)

    dialog.show()
    dialog.window!!.setLayout(
        ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.WRAP_CONTENT
    )
    dialog.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
    dialog.window!!.attributes.windowAnimations = R.style.DialogAnimation
    dialog.window!!.setGravity(Gravity.BOTTOM)
}
0

There are 0 best solutions below