I have used #bindingAdapter to set Avatar in my layout but when change some value the bindingAdapter is not loading the image. This is my viwmodel
class ProfileViewModel constructor(val repository: ProfileRepository, val mContext: Context, photo: Photo = Photo("")): ViewModel() {
var avatar: ObservableField<String> = ObservableField("")
var responseListener: ResponseListener? = null
var fileUploadEvent: FileEvents? = null
fun getAvatarUrl() : ObservableField<String> {
return avatar
}
companion object {
#BindingAdapter("avatarUrl")
#JvmStatic
fun loadAvatar(imageView: ImageView, avatarUrl: String) {
Glide.with(imageView.context)
.load(avatarUrl)
.placeholder(R.mipmap.profile)
.apply(RequestOptions.circleCropTransform())
.into(imageView)
}
}
fun onProfileImageUpload(uri: Uri) {
fileUploadEvent!!.onUpload()
Coroutines.main {
try {
val imageResponse = repository.updateProfileAvatar(
getImageForUpload(uri, "avatar")
)
avatar.set(imageResponse.data.avatar.thumbnail)
fileUploadEvent!!.onUploadSuccess(avatar.get()!!)
} catch(e : Exception) {
fileUploadEvent!!.onUploadFailure(e.toString())
}
}
}
}
In my XML I defined the namespace like this
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewmodel"
type="com.kreeti.gogal.ui.profile.ProfileViewModel" />
</data>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="#+id/profile_root"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="15dp">
<TextView
android:id="#+id/welcome_header"
style="#style/glacial_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/welcome"
android:textColor="#color/violet"
android:textSize="32sp" />
<LinearLayout
android:id="#+id/profile_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
>
<ImageView
android:id="#+id/avatar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:scaleType="center"
android:onClick="#{viewmodel::onAvatarClick}"
app:avatarUrl="#{viewmodel.avatarUrl}" />
<ProgressBar
android:id="#+id/avatar_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone" />
</FrameLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>
I am getting photo url from api, I want change the photo when I get the phot through the viewbinding. How can I load the image when I am changing the url? Do I have to make some listener?
Related
My RecyclerView loads in the middle of the list instead of the most top item first. The list should load the topmost item. In this case, it's the "latest" post. Which I have set to the reverse layout to true and that works.
My RecyclerView
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:viewBindingIgnore="true">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/postsList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:padding="5dp"
tools:listitem="#layout/post_item" />
</FrameLayout>
My Post list fragment
// Set up FirebaseRecyclerAdapter with the Query
val postsQuery = database.child(Globals().POSTS_ROOT_PATH)
// This configuration comes from the Paging Support Library
// https://developer.android.com/reference/androidx/paging/PagedList.Config
val config = PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setPrefetchDistance(5)
.setPageSize(10)
.build()
// The options for the adapter combine the paging configuration with query information
// and application-specific options for lifecycle, etc.
val options = DatabasePagingOptions.Builder<Post>()
.setLifecycleOwner(this)
.setQuery(postsQuery, config, Post::class.java)
.build()
adapter = object : FirebaseRecyclerPagingAdapter<Post, PostViewHolder>(options) {
override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): PostViewHolder {
val inflater = LayoutInflater.from(viewGroup.context)
return PostViewHolder(inflater.inflate(R.layout.post_item, viewGroup, false))
}
override fun onBindViewHolder(viewHolder: PostViewHolder, position: Int, model: Post) {
val postRef = getRef(position)
// Set click listener for the whole post view
val postKey = postRef.key
// Log.d("PostListFragment", "post key id is: " + postKey)
viewHolder.itemView.setOnClickListener {
// Launch PostDetailActivity
val intent = Intent(activity, PostDetailActivity::class.java)
intent.putExtra(PostDetailActivity.EXTRA_POST_KEY, postKey)
startActivity(intent)
}
}
override fun onLoadingStateChanged(state: LoadingState) {
when (state) {
LoadingState.LOADING_INITIAL -> {
Log.d(TAG, "Initial data loading")
}
LoadingState.LOADING_MORE -> {
}
LoadingState.LOADED -> {
}
LoadingState.FINISHED -> {
Log.d(TAG, "All data loaded.")
}
LoadingState.ERROR -> {
Log.d(TAG, "Error occured while loading data")
}
}
}
override fun onError(databaseError: DatabaseError) {
super.onError(databaseError)
databaseError.toException().printStackTrace()
}
}
// Set up Layout Manager, reverse layout
manager = LinearLayoutManager(activity)
manager.reverseLayout = true
manager.stackFromEnd = true
recyclerList.layoutManager = manager
recyclerList.adapter = adapter
}
When I set the setPageSize() to a very large number then it loads the most top item. What do I need to do so this always loads the most top list item first?
Thanks
Edit:
post_item.xml
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
tools:viewBindingIgnore="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/postTextLayout"
layout="#layout/post_text_include"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:layout_marginStart="2dp" />
<include
android:id="#+id/postThumbnailImg"
layout="#layout/post_video_thumbnail_include"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/postTextLayout"
android:layout_alignParentStart="true" />
<include
android:id="#+id/postAuthorLayout"
layout="#layout/post_author_thumbnail_include"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/postThumbnailImg"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
Sub layouts
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/postTitle"
style="#style/TextAppearance.AppCompat.Medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:textStyle="bold"
android:textColor="#color/colorPrimaryDark"
tools:text="My First Post"
android:paddingStart="5dp"
android:paddingEnd="5dp" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:viewBindingIgnore="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="#+id/videoThumbnailImg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/main_background" />
/>
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent">
<ImageView
android:id="#+id/playButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="centerCrop"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_baseline_play_arrow_24" />
</RelativeLayout>
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:gravity="center_vertical"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:layout_width="40dp"
android:layout_height="40dp"
app:cardCornerRadius="20dp">
<ImageView
android:id="#+id/postAuthorPhoto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/ic_person_outline_40" />
</androidx.cardview.widget.CardView>
<TextView
android:id="#+id/postAuthor"
style="#style/Base.TextAppearance.AppCompat.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:gravity="center_vertical"
android:textColor="#color/colorPrimaryDark"
tools:text="someauthor#email.com" />
</LinearLayout>
You need to set height wrap_content rather than match_parent for sublayouts.
I have a navigation header as bellow
I want to update TextView in nav_header.xml from MainActivity.kt by Kotlin
I want to update text of TextView(id=drawer_user_name) from MainActivity.kt
main_activity.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"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="45dp"
android:background="#android:drawable/screen_background_light_transparent"
tools:listitem="#layout/item_list" />
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header"
app:menu="#menu/nav_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
nav_header.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#4cAF50"
android:orientation="vertical"
android:layout_gravity="top"
android:paddingTop="35dp"
android:paddingLeft="15dp"
android:paddingBottom="15dp"
android:id="#+id/ddd">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/ic_launcher"
android:id="#+id/imageView"
android:layout_marginBottom="15dp"/>
<TextView
android:id="#+id/drawer_user_name" //--> i want to update this TextView from MainActivity.kt
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="title"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="this our navigation drawer"
android:textColor="#FFFFFF" />
</LinearLayout>
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/colorPrimary"
android:background="?attr/colorPrimary"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
style="#style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:id="#+id/home_page_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chat"
android:textColor="#FFFFFF" />
</LinearLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="this is my content"/>
</LinearLayout>
</LinearLayout>
Please Help me,I really need a way to solve this problem
thank you
Babbab forces us to write stories here to allow us to post a question(It looks like your post is mostly code; please add some more details)
Edit
my MainActivity.kt
package com.example.myapplication
...
import android.view.LayoutInflater
import android.widget.LinearLayout
import kotlinx.android.synthetic.main.activity_main_page.*
import kotlinx.android.synthetic.main.nav_header.view.*
class MainPageActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener,CustomAdapter.OnItemClickListener {
lateinit var toolbar:androidx.appcompat.widget.Toolbar
lateinit var drawerLayout: DrawerLayout
lateinit var navView:NavigationView
private var data = arrayListOf<UserChanel>()
private lateinit var myadapter:CustomAdapter
private val gson = Gson()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
toolbar =findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
drawerLayout = findViewById(R.id.drawer_layout)
navView = findViewById(R.id.nav_view)
val toggle = ActionBarDrawerToggle(this,drawerLayout,toolbar,0,0)
drawerLayout.addDrawerListener(toggle)
toggle.syncState()
navView.setNavigationItemSelectedListener(this)
// val userNameTextView = nav_view.findViewById<TextView>(R.id.drawer_user_name)
// userNameTextView.text = "SOME TEXT" // error
nav_view.drawer_user_name.text = "SOME TEXT" // Error
val recyclerview = findViewById<RecyclerView>(R.id.recycler_view)
// for insert line divider to recyclerview items
recyclerview.addItemDecoration(SimpleDividerItemDecoration(this)) // for insert line divider to recyclerview items
send_request()
val adapter = CustomAdapter(this,data,this)
recyclerview.layoutManager = LinearLayoutManager(this)
recyclerview.adapter = adapter
recyclerview.setHasFixedSize(true)
myadapter = adapter
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
when(item.itemId){
R.id.nav_profile->{
Toast.makeText(this,"profile clicked",Toast.LENGTH_SHORT).show()
}
R.id.nav_message->{
Toast.makeText(this,"message clicked",Toast.LENGTH_SHORT).show()
}
R.id.nav_frinds->{
Toast.makeText(this,"frinds clicked",Toast.LENGTH_SHORT).show()
}
R.id.nav_update->{
Toast.makeText(this,"update clicked",Toast.LENGTH_SHORT).show()
}
R.id.nav_logout->{
Toast.makeText(this,"logout clicked",Toast.LENGTH_SHORT).show()
}
}
drawerLayout.closeDrawer(GravityCompat.START)
return true
}
private fun send_request(){
...
}
override fun onItemClick(position: Int,adapter: CustomAdapter,v:View) {
...
}
}
You can find the target view from its parent in your activity like either of these ways:
nav_view.getHeaderView(0).findViewById<TextView>(R.id.drawer_user_name).text = "SOME TEXT"
or
import kotlinx.android.synthetic.main.nav_header.view.*
...
nav_view.getHeaderView(0).drawer_user_name.text = "SOME TEXT"
It's possible to access the header view using getheaderview(int index) according to the documentation. As you have one header view in the NavigationView, it is accessible by index 0.
I have a RecyclerView implemented along with Paging to load a list from Room Database. The list works fine when the size is small. When the size reaches around 50 - 60, the list still works fine but when i switch to another fragment and then come back to the list, its blocks the UI for around 1.5 - 2 seconds which is super dull in user experience (See GIF below):
My code is as follows:
DAO
#Query("SELECT * FROM account_table WHERE userID = :userID")
fun getAll(userID: String): DataSource.Factory<Int, Account>
Repository
class AccountRepository private constructor(application: Application) {
private val database =
LockyDatabase.getDatabase(
application
)
private val accountDao = database.accountDao()
companion object {
#Volatile
private var instance: AccountRepository? = null
fun getInstance(application: Application) =
instance ?: synchronized(this) {
instance ?: AccountRepository(application).also { instance = it }
}
}
fun getAll(userID: String) = accountDao.getAll(userID)
}
adapter
class CredentialsPagingAdapter(
private val clickListener: ClickListener,
private val optionsClickListener: OptionsClickListener?,
private val isSimplified: Boolean
) : PagedListAdapter<Credentials, CredentialsViewHolder>(
diffCallback
) {
companion object {
private val diffCallback = object : DiffUtil.ItemCallback<Credentials>() {
override fun areItemsTheSame(oldItem: Credentials, newItem: Credentials): Boolean {
return oldItem.id == newItem.id
}
override fun areContentsTheSame(oldItem: Credentials, newItem: Credentials): Boolean {
return oldItem.equals(newItem)
}
}
}
override fun onBindViewHolder(holder: CredentialsViewHolder, position: Int) {
holder.bind(
clickListener,
optionsClickListener,
getItem(position),
isSimplified
)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CredentialsViewHolder {
return CredentialsViewHolder.from(
parent
)
}
}
viewModel
val accounts = Transformations.switchMap(_sort) {
when (true) {
it.name -> _accounts.sortByEntryName
it.username -> _accounts.sortByUsername
it.email -> _accounts.sortByEmail
it.website -> _accounts.sortByWebsite
it.authType -> _accounts.sortByAuthenticationType
else -> _accounts
}.toLiveData(pageSize = resources.getInteger(R.integer.size_paging_list_default))
}
fragment
private fun subscribeAccounts() {
val adapter = CredentialsPagingAdapter(
/* The click listener to handle account on clicks */
ClickListener {
navigateTo(
AccountFragmentDirections.actionFragmentAccountToFragmentViewAccount(
it as Account
)
)
},
/* The click listener to handle popup menu for each accounts */
OptionsClickListener { view, credential ->
view.apply {
isEnabled = false
}
createPopupMenu(view, credential as Account)
},
false
)
binding.RecyclerViewAccount.apply {
/*
* State that layout size will not change for better performance
*/
setHasFixedSize(true)
/* Bind the layout manager */
layoutManager = LinearLayoutManager(requireContext())
/* Bind the adapter */
this.adapter = adapter
}
viewModel.accounts.observe(viewLifecycleOwner, Observer {
if (it != null) {
/*
* If accounts is not null
* Load recyclerview and
* Update the ui
*/
lifecycleScope.launch {
adapter.submitList(it as PagedList<Credentials>)
}
updateUI(it.size)
}
})
}
Main Activity Layout
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<androidx.drawerlayout.widget.DrawerLayout
android:id="#+id/Drawer_Main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.main.main.MainActivity">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="#+id/Layout_Coordinator_Main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/Toolbar_Main"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#drawable/custom_rounded_background_toolbar"
android:clipChildren="false"
android:outlineAmbientShadowColor="#color/colorShadowColor"
android:outlineSpotShadowColor="#color/colorShadowColor"
android:paddingStart="8dp"
android:paddingEnd="8dp"
app:contentInsetStartWithNavigation="0dp"
tools:targetApi="p">
...
</com.google.android.material.appbar.MaterialToolbar>
<androidx.core.widget.NestedScrollView
android:id="#+id/Nested_Scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:fillViewport="true">
<fragment
android:id="#+id/Navigation_Host"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="#navigation/navigation_drawer_main"
tools:ignore="FragmentTagUsage" />
</androidx.core.widget.NestedScrollView>
<com.google.android.material.floatingactionbutton.FloatingActionButton ... />
<com.google.android.material.floatingactionbutton.FloatingActionButton ... />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/Navigation_View"
style="#style/Locky.Widget.Custom.NavigationView"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:clipToPadding="false"
android:paddingStart="0dp"
android:paddingEnd="16dp"
app:headerLayout="#layout/drawer_header"
app:itemTextAppearance="#style/Locky.Text.Body.Drawer"
app:menu="#menu/menu_drawer_main" />
</androidx.drawerlayout.widget.DrawerLayout>
</layout>
Fragment Account Layout:
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<import type="android.view.View" />
<import type="com.th3pl4gu3.locky_offline.repository.Loading.List" />
<variable
name="ViewModel"
type="com.th3pl4gu3.locky_offline.ui.main.main.account.AccountViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/Layout_Fragment_Account"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorOnSurface">
<!--
Recyclerview
-->
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/RecyclerView_Account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="#{ViewModel.loadingStatus==List.LIST ? View.VISIBLE : View.GONE}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:visibility="gone" />
<!--
Empty Views and group
-->
<androidx.constraintlayout.widget.Group
android:id="#+id/Empty_View"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="#{ViewModel.loadingStatus==List.EMPTY_VIEW ? View.VISIBLE : View.GONE}"
app:constraint_referenced_ids="Empty_View_Illustration,Empty_View_Title,Empty_View_Subtitle" />
<ImageView
android:id="#+id/Empty_View_Illustration" ... />
<TextView
android:id="#+id/Empty_View_Title" ... />
<TextView
android:id="#+id/Empty_View_Subtitle" ... />
<!--
Progress Bar
-->
<include
android:id="#+id/Progress_Bar"
layout="#layout/custom_view_list_loading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="#{ViewModel.loadingStatus==List.LOADING ? View.VISIBLE : View.GONE}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Recyclerview List Layout
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<import type="android.view.View" />
<variable
name="IsSimplifiedVersion"
type="Boolean" />
<variable
name="Credential"
type="com.th3pl4gu3.locky_offline.core.main.credentials.Credentials" />
<variable
name="ClickListener"
type="com.th3pl4gu3.locky_offline.ui.main.main.ClickListener" />
<variable
name="OptionsClickListener"
type="com.th3pl4gu3.locky_offline.ui.main.main.OptionsClickListener" />
</data>
<com.google.android.material.card.MaterialCardView
style="#style/Locky.ListCardView"
credentialCardConfiguration="#{Credential}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginEnd="8dp"
android:clickable="true"
android:focusable="true"
android:onClick="#{() -> ClickListener.onClick(Credential)}">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/Credential_Logo"
configureLogo="#{Credential}"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_marginEnd="16dp"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/Barrier_Logo"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:src="#drawable/ic_locky_with_background_circle" />
<androidx.constraintlayout.widget.Barrier
android:id="#+id/Barrier_Logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="end"
app:constraint_referenced_ids="Credential_Logo" />
<TextView
android:id="#+id/Credential_Entry_Name"
style="#style/Locky.Text.Title6.List"
listTitleMessageCardEligibility="#{Credential}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:ems="11"
android:singleLine="true"
android:text="#{Credential.entryName}"
app:layout_constraintBottom_toTopOf="#+id/Credential_First_Subtitle"
app:layout_constraintStart_toEndOf="#id/Barrier_Logo"
app:layout_constraintTop_toTopOf="#+id/Credential_Logo"
app:layout_constraintVertical_chainStyle="spread_inside"
tools:text="This is an entry name and it can be very very very long" />
<TextView
android:id="#+id/Credential_First_Subtitle"
style="#style/Locky.Text.Subtitle.List.Primary"
setCredentialSubtitle="#{Credential}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:ems="13"
android:singleLine="true"
app:layout_constraintBottom_toTopOf="#+id/Credential_Second_Subtitle"
app:layout_constraintStart_toStartOf="#+id/Credential_Entry_Name"
app:layout_constraintTop_toBottomOf="#+id/Credential_Entry_Name"
app:layout_constraintVertical_chainStyle="spread"
tools:text="This is the very first subtitle and this can be very long too" />
<TextView
android:id="#+id/Credential_Second_Subtitle"
style="#style/Locky.Text.Subtitle.List.Secondary"
setCredentialOtherSubtitle="#{Credential}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:ems="14"
android:singleLine="true"
android:textColor="#color/colorAccent"
app:layout_constraintBottom_toBottomOf="#+id/Credential_Logo"
app:layout_constraintStart_toStartOf="#+id/Credential_Entry_Name"
app:layout_constraintTop_toBottomOf="#+id/Credential_First_Subtitle"
app:layout_constraintVertical_chainStyle="spread"
tools:text="This is the second subtitle and this can be very long too" />
<androidx.constraintlayout.widget.Barrier
android:id="#+id/Barrier_More_Options"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="start"
app:constraint_referenced_ids="Credential_More_Options" />
<ImageButton
android:id="#+id/Credential_More_Options"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:background="#drawable/custom_states_background_button_image"
android:onClick="#{(view) -> OptionsClickListener.onClick(view, Credential)}"
android:scaleType="centerCrop"
android:src="#drawable/ic_more_options"
android:visibility="#{IsSimplifiedVersion ? View.GONE : View.VISIBLE}"
app:layout_constraintBottom_toBottomOf="#+id/Credential_Logo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/Barrier_More_Options"
app:layout_constraintTop_toTopOf="#+id/Credential_Logo" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
</layout>
My paging version is 2.1.2
Can someone please help me on this. I tried several days looking for fix but nothing works.
I appreciate the help.
You must remove your NestedScrollView (Nested_Scroll) in your activity layout - you cannot put a vertical RecyclerView within a NestedScrollView.
A NestedScrollView expands every child in the vertical scroll direction to determine the maximum scroll distance. This means that it gives the RecyclerView an infinite height to expand into. This causes RecyclerView to inflate every element, defeating all view recycling and defeating the use of paging - given infinite height, it'll continue to ask Paging for more and more rows to fill the space.
I have some text field and a recyclerview in my activity. I need to access textfield values from the recycler view adapter class and access a method in activity from the adapter class. but I couldn't do the same using Kotlin help me to solve this
Activity
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_addstaff)
try {
sprefs = this!!.getSharedPreferences(
Config.PACKAGE_NAME, Context.MODE_PRIVATE)
token = sprefs!!.getString("token", "No")!!
} catch (e: Exception) {
}
try {
layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
outletnameslistrecycle!!.layoutManager = layoutManager
Outnameslist = ArrayList()
adapter = outletname_adapter(Outnameslist as ArrayList<Outletnamelist>, this,btneditstaff as Button)
outletnameslistrecycle!!.adapter = adapter
getoutletlist();
} catch (e: Exception) {
e.printStackTrace()
}
}
Adapterclass
class outletname_adapter(internal var Outnamelist: List<Outletnamelist>, private val context: Context, val button: Button) : RecyclerView.Adapter<outletname_adapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
try {
val v = LayoutInflater.from(parent.context)
.inflate(R.layout.lay_outletnamelist, parent, false)
return ViewHolder(v, context, Outnamelist)
} catch (e: Exception) {
e.printStackTrace()
}
return null!!
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
try {
button.setOnClickListener {
}
var OutletList =Outnamelist[position]
holder.outlet_name!!.text=OutletList.name
} catch (e: Exception) {
e.printStackTrace()
}
}
override fun getItemCount(): Int {
return Outnamelist.size
}
inner class ViewHolder(v: View, var context: Context, private val outnamelist: List<Outletnamelist>)
: RecyclerView.ViewHolder(v), View.OnClickListener {
var outlet_checkbox: CheckBox? = null
var outlet_name: TextView?=null
var layTop: LinearLayout?= null
init {
itemView.setOnClickListener(this)
try {
outlet_checkbox=v.findViewById<View>(R.id.checkBox) as CheckBox
outlet_name=v.findViewById<View>(R.id.txt_in)as TextView
} catch (e: Exception) {
e.printStackTrace()
}
}
override fun onClick(v: View) {
val position = adapterPosition
}
}
}
XML`
<ScrollView 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"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/diagonal_bg"
tools:context=".Activity_addstaff">
<de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/img_staff"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="30dp"
android:layout_marginTop="137dp"
android:src="#drawable/subadmin"
app:civ_border_color="#ffffff"
app:civ_border_width="2dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="1dp"
android:paddingRight="1dp"
android:layout_marginTop="250dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:animateLayoutChanges="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:animateLayoutChanges="true">
<android.support.design.widget.TextInputLayout
android:id="#+id/staffname_text_input"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:id="#+id/staffname_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/label_staffname" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:animateLayoutChanges="true">
<android.support.design.widget.TextInputLayout
android:id="#+id/staffaddress_text_input"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:id="#+id/staffaddress_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/label_address" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:animateLayoutChanges="true">
<android.support.design.widget.TextInputLayout
android:id="#+id/staffplace_text_input"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:id="#+id/staffplace_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/label_place" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:animateLayoutChanges="true">
<android.support.design.widget.TextInputLayout
android:id="#+id/staffpin_text_input"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:id="#+id/staffpin_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/label_pin" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:animateLayoutChanges="true">
<android.support.design.widget.TextInputLayout
android:id="#+id/staffmob_text_input"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:id="#+id/staffmob_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/label_mob"
/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/lay_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<LinearLayout
android:id="#+id/lay_root2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/outletnameslistrecycle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="1dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:padding="20dp">
<Button
android:id="#+id/btneditstaff"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_gravity="center"
android:layout_marginEnd="5dp"
style="#style/DefaultButton"
android:background="#drawable/btn_border"
android:foreground="?attr/selectableItemBackground"
android:text="Save"
android:textAllCaps="false"
android:textColor="#color/white" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
From the activity, I have passed my button reference to adapter class but the same way I am getting an error when trying to pass textfield references.
you can access the activity method or others on this way in adapter:
if(context is XXXActivity){
context.xxx()
context.xx
}
but this isn't the good code.you can try like this 2 methods:
you can define and set a Listener to adapter to post the event(such as click?) to activity,
make the activity deal with it initiative(activity can get the data from itself and adapter)。
if not an event make the adapter need the data from activity.you can provide the data from activity with interface like this.
class outletname_adapter(internal var Outnamelist: List<Outletnamelist>, private val context: Context, val button: Button , val xxxtextProvider:()->String={""}/*it return the data you need*/ ) : RecyclerView.Adapter<outletname_adapter.ViewHolder>() {
...
}
I am trying to make a bottom sheet with a recyclerview with cards. I was able to put the the cards within the bottom sheet but I am having problems with the recyclerview. As shown in the photo, it creates multiple bottom sheets with the cards within them. I have tried to fix this but so far have no luck. How can I make it so that the bottom sheet contains the cards with a recyclerview? I believe the problem is the Recyclerview in activity_main but don't know where to place it.
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (supportActionBar != null)
supportActionBar?.hide()
val modelList = readFromAsset()
val adapter = CustomAdapter(modelList, this)
rcv.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false) as RecyclerView.LayoutManager?
rcv.adapter = adapter;
configureBackdrop()
}
private var mBottomSheetBehavior: BottomSheetBehavior<View?>? = null
private fun configureBackdrop() {
// Get the fragment reference
val fragment = supportFragmentManager.findFragmentById(R.id.filter_fragment)
fragment?.let {
// Get the BottomSheetBehavior from the fragment view
BottomSheetBehavior.from(it.view)?.let { bsb ->
mBottomSheetBehavior = bsb
}
}
}
private fun readFromAsset(): List<Model> {
val modeList = mutableListOf<Model>()
val bufferReader = application.assets.open("android_version.json").bufferedReader()
val json_string = bufferReader.use {
it.readText()
}
val jsonArray = JSONArray(json_string);
for (i in 0..jsonArray.length() - 1) {
val jsonObject: JSONObject = jsonArray.getJSONObject(i)
val model = Model(jsonObject.getString("name"), jsonObject.getString("version"))
modeList.add(model)
}
return modeList
}
}
CustomAdapter.kt
class CustomAdapter(val modelList: List<Model>, val context: Context) :
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
(holder as ViewHolder).bind(modelList.get(position));
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
return ViewHolder(layoutInflater.inflate(R.layout.backdrop_fragment, parent, false))
}
override fun getItemCount(): Int {
return modelList.size;
}
lateinit var mClickListener: ClickListener
fun setOnItemClickListener(aClickListener: ClickListener) {
mClickListener = aClickListener
}
interface ClickListener {
fun onClick(pos: Int, aView: View)
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnClickListener {
init {
itemView.setOnClickListener(this)
}
override fun onClick(p0: View?) {
mClickListener.onClick(adapterPosition, itemView)
}
fun bind(model: Model): Unit {
itemView.txt.text = model.name
itemView.sub_txt.text = model.version
val id = context.resources.getIdentifier(model.name.toLowerCase(), "drawable", context.packageName)
itemView.img.setBackgroundResource(id)
}
}
}
activity_main
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView"
android:text="#string/main_activity_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<androidx.recyclerview.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:id="#+id/rcv"/>
<fragment
app:behavior_hideable="false"
app:behavior_peekHeight="100dp"
android:layout_marginTop="?attr/actionBarSize"
app:behavior_skipCollapsed="false"
android:id="#+id/filter_fragment"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:name="behavior.sheet.bottom.BackdropFragment" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
backdrop_fragment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/backdrop_fragment_background"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/backdrop_content" />
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="75dp"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
android:orientation="vertical"
card_view:cardCornerRadius="30dp"
card_view:cardElevation="5dp"
card_view:cardUseCompatPadding="false"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
card_view:contentPadding="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:text="Title"
android:textSize="20sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/img"
android:layout_width="30dp"
android:layout_height="match_parent"
android:layout_marginStart="25dp"
android:layout_toRightOf="#+id/txt"
android:contentDescription="#string/app_name" />
<TextView
android:id="#+id/sub_txt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="25dp"
android:layout_toRightOf="#+id/img"
android:autoSizeMaxTextSize="8sp"
android:autoSizeMinTextSize="6sp"
android:autoSizeStepGranularity="2sp"
android:autoSizeTextType="uniform"
android:text="Title" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
Please set the Bottom sheet Linear Layout to wrap-content
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/backdrop_fragment_background"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/backdrop_content" />
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="75dp"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
android:orientation="vertical"
card_view:cardCornerRadius="30dp"
card_view:cardElevation="5dp"
card_view:cardUseCompatPadding="false"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
card_view:contentPadding="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:text="Title"
android:textSize="20sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/img"
android:layout_width="30dp"
android:layout_height="match_parent"
android:layout_marginStart="25dp"
android:layout_toRightOf="#+id/txt"
android:contentDescription="#string/app_name" />
<TextView
android:id="#+id/sub_txt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="25dp"
android:layout_toRightOf="#+id/img"
android:autoSizeMaxTextSize="8sp"
android:autoSizeMinTextSize="6sp"
android:autoSizeStepGranularity="2sp"
android:autoSizeTextType="uniform"
android:text="Title" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
You have the RecyclerView in the activity_main layout file instead of inside the Fragment layout file.
Here is what you should do, create a class that inherits BottomSheetDialogFragment(). See below.
Add dependency implementation 'com.google.android.material:material:1.2.0-alpha04' inside the app/build.gradle.
Create your bottom sheet class
class BottomSheetExampleDialogFragment : BottomSheetDialogFragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
) : View? =
inflater.inflate(R.layout.bottom_sheet_example_dialog_fragment, container, false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// Handle RecyclerView here
}
}
Bottom sheet layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rcv"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Then in your MainActivity you can show the bottom sheet by using
val bottomSheetFragment = BottomSheetExampleDialogFragment()
bottomSheetFragment.show(supportFragmentManager, bottomSheetFragment.getTag())