Bottom Sheet Dialog only showing one item of recyclerview - android

I realise this question has been asked previously but the answers provided haven't helped me out. I cannot see what is going wrong here.
I have an activity, in which I display a bottom sheet, the bottom sheet has a title and a recyclerview. The recycler view is only displaying one item, regardless of how many are passed to the adapter. It is possible to scroll through the items but only one at any time is visible.
Here is my activity xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
tools:context=".features.order.automatedcheckout.view.AutomatedCheckoutQRCodeActivity"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/scanBarcodeInstruction"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="50dp"
android:text="Scan the barcode to get started"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<androidx.constraintlayout.widget.Guideline
android:id="#+id/loadingGuideline"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ProgressBar
android:id="#+id/progressIndicator"
app:layout_constraintBottom_toTopOf="#+id/loadingGuideline"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/scanBarcodeInstruction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/qrcodeImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/scanBarcodeInstruction"/>
<TextView
android:id="#+id/tenderName"
app:layout_constraintTop_toBottomOf="#+id/qrcodeImageView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="#+id/changeTenderButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
android:visibility="gone"
android:layout_marginBottom="20dp"
android:layout_marginRight="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:text = "Change Tender"/>
</androidx.constraintlayout.widget.ConstraintLayout>
and here is my bottom sheet fragment xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/bottomSheetLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tenderTitle"
android:layout_marginTop="20dp"
android:layout_marginStart="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SELECT"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/tenderList"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</androidx.appcompat.widget.LinearLayoutCompat>
my bottom sheet fragment:
class TenderBottomSheetFragment : BottomSheetDialogFragment() {
private var _binding: FragmentTenderBottomSheetBinding? = null
private val binding get() = _binding!!
private lateinit var onTenderClicks:(tenderDetails: Pair<Int?, String?>) -> Unit
lateinit var adapter: TenderAdapter
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentTenderBottomSheetBinding.inflate(inflater, container, false)
adapter = TenderAdapter (
arguments?.parcelableArrayList(ARG_TENDERS)!!,
onTenderClicks
)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val layoutManager = LinearLayoutManager(requireContext())
binding.tenderList.layoutManager = layoutManager
binding.tenderList.addItemDecoration(DividerItemDecoration(requireContext(), layoutManager.orientation))
binding.tenderList.adapter = adapter
}
companion object {
fun newInstance(
campusTenders: List<TenderKt>,
onTenderClick: (tenderDetails: Pair<Int?, String?>) -> Unit
): TenderBottomSheetFragment {
val tenderDialogFragment = TenderBottomSheetFragment()
tenderDialogFragment.onTenderClicks = onTenderClick
tenderDialogFragment.apply {
arguments = Bundle().apply {
putParcelableArrayList(ARG_TENDERS, ArrayList(tenders))
}
}
return tenderDialogFragment
}
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
I'm sure this is something really simple but unfortunately right now, I'm not seeing it. If anyone could help I would really appreciate it.
UPDATE
Here is the recycler item layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="#+id/campus_tender_name"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:text ="Flex Account"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</androidx.constraintlayout.widget.ConstraintLayout>
I have now updated that to:
<TextView
android:id="#+id/campus_tender_name"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:padding="10dp"
tools:text = "Flex Account"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
and that works fine. So it was the constraint layout in the recycler view item

Related

Android RecyclerView item width doesn't match parent and preview doesn't match emulator

This is my recycler view items xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/recyclerItem"
app:cardElevation="10dp"
app:cardUseCompatPadding="false"
app:cardCornerRadius="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/recyclerItemImageView"
android:padding="8dp"
android:src="#mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
<TextView
android:id="#+id/recyclerItemTextView"
android:layout_marginStart="4dp"
android:textSize="18sp"
android:textColor="#color/black"
android:text="item"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
</androidx.cardview.widget.CardView>
And preview show me like this:
Preview
But emulator result this:
Emulator result
So items in recycler view does not match parent width on emulator. Other xml's there are:
<?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"
tools:context=".MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragmentContainerView"
android:layout_margin="16dp"
android:name="com.example.databinding_practice.ListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/activityTextView"/>
<TextView
android:id="#+id/activityTextView"
android:layout_margin="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textColor="#color/black"
android:textSize="20sp" />
</RelativeLayout>
And fragment:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".ListFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Adapter class and ListFragment like this:
RecyclerAdapter.kt:
class RecyclerAdapter(var recyclerItems: ArrayList<RecyclerItemModel>) : RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder>() {
class RecyclerViewHolder(val binding: RecyclerItemBinding) : RecyclerView.ViewHolder(binding.root) {
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder {
val binding = RecyclerItemBinding.inflate(LayoutInflater.from(parent.context))
return RecyclerViewHolder(binding)
}
override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
holder.binding.recyclerItemTextView.text = recyclerItems[position].text
recyclerItems[position].imageMipmapSource?.let { holder.binding.recyclerItemImageView.setImageResource(it) }
}
override fun getItemCount(): Int {
return recyclerItems.size
}
}
ListFragment.kt:
class ListFragment : Fragment() {
private var itemList : ArrayList<RecyclerItemModel> = arrayListOf()
private var recyclerAdapter: RecyclerAdapter = RecyclerAdapter(arrayListOf())
private lateinit var fragmentListBinding: FragmentListBinding
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
fragmentListBinding = FragmentListBinding.inflate(layoutInflater)
return fragmentListBinding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
for (i in 0..3){
itemList.add(RecyclerItemModel(R.mipmap.ic_launcher,"Item $i"))
}
fragmentListBinding.recyclerView.layoutManager = LinearLayoutManager(context)
fragmentListBinding.recyclerView.adapter = recyclerAdapter
recyclerAdapter.recyclerItems = itemList
recyclerAdapter.notifyDataSetChanged()
}
Where am I doing wrong? If I replace match parent to fixed size like 200 dp - 300dp , thats working correctly but match parent does not. And why preview and emulator does not match ? Thank you.
In the fragment you should change layout_width and layout_height to "0dp" in your RecyclerView.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
tools:context=".ListFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

IllegalStateException: TabLayoutMediator attached before ViewPager2 has an adapter

fragment_news_details
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data class="NewsDetailsBinding">
<variable
name="adapter"
type="com.abc.xyz.base.BaseAdapter" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".news.NewsDetailsFragment">
<androidx.constraintlayout.widget.Guideline
android:id="#+id/viewPagerGuideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.4" />
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/imageViewPager"
android:layout_width="0dp"
android:layout_height="0dp"
android:adapter="#{adapter}"
app:layout_constraintBottom_toBottomOf="#id/viewPagerGuideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.tabs.TabLayout
android:id="#+id/dotsTab"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:background="#color/transparent"
app:layout_constraintBottom_toBottomOf="#id/imageViewPager"
app:layout_constraintEnd_toEndOf="#+id/imageViewPager"
app:layout_constraintStart_toStartOf="#+id/imageViewPager"
app:tabBackground="#drawable/dot_selector"
app:tabGravity="center"
app:tabIndicatorHeight="0dp" />
<TextView
android:id="#+id/descriptionTitleTV"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:textColor="#color/textColorPrimary"
android:textSize="#dimen/text_size_20"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/viewPagerGuideline"
tools:text="Fresh snow at Dunga" />
<TextView
android:id="#+id/descriptionTV"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:textColor="#color/textColorSecondary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/descriptionTitleTV"
tools:text="Lorem ipsum dolor sit." />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout><?xml version="1.0" encoding="utf-8"?>
NewsDetailsFragment
class NewsDetailsFragment : Fragment() {
lateinit var binding: NewsDetailsBinding
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = inflater.bind(R.layout.fragment_news_details, container)
return binding.root
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
val templist = ArrayList<String>().apply {
add("https://picsum.photos/id/237/200/300")
add("https://picsum.photos/seed/picsum/200/300")
add("https://picsum.photos/id/1002/300/400")
add("https://picsum.photos/id/237/200/300")
}
binding.adapter =
BaseAdapter(templist, R.layout.item_news_image, ::NewsImageViewHolder)
if (binding.adapter != null)
TabLayoutMediator(binding.dotsTab, binding.imageViewPager) { tab, position ->
tab.icon = ContextCompat.getDrawable(requireContext(), R.drawable.anim_add_star)
}.attach()
}
inner class NewsImageViewHolder(itemBinding: NewsImageItemBinding) :
BaseViewHolder<String, NewsImageItemBinding>(itemBinding) {
}
}
in on activity created its throwing error TabLayoutMediator attached before ViewPager2 has an adapter. I tried adding the adapter above and below the TabloutMediatorI().attach(), does not work in both cases.What I need is to show dots indicator using tabslayout .I am new using ViewPager 2 ,Any help would be appreciated.Thanks.
ViewPager doesn't have an adapter attribute, so android:adapter="#{adapter}" doesn't set the adapter on the ViewPager.
Instead, you can get the view pager from binding and set the adapter in your fragment like this: binding.imageViewPager.adapter = BaseAdapter(templist, R.layout.item_news_image, ::NewsImageViewHolder)

Recycler View and Cards Creating Multiple Copies When Scroll

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())

Can not Click the Items in Fragment

I'm using old way to create bottom navigation and using Fragment as well.
When I starting design some view inside Fragment. Like Edit, TextBox or ScollView etc. Everything in Fragment are unclickable.
(I had run the app on real device and emulator.
I can not use my mouse click the Textbox But I can input the text by using tab to reach the Textbox
Below is the function I use to open Fragment when I clicked navigation
(In my HomeActivity.kt)
private fun openFragment(fragment: Fragment) {
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.container, fragment)
transaction.addToBackStack(null)
transaction.commit()
}
Below is Xml of container
(In my Activity_Home.xml)
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:clickable="true"
android:focusable="true"/>
Below is one of Fragment
class PersonalFragment : Fragment() {
var Personal = getActivity()?.getApplicationContext();
companion object {
fun newInstance(): PersonalFragment = PersonalFragment()
}
override fun onCreateView(inflater: LayoutInflater, container:ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_personal, container, false)
}
}
Below is Xml of Fragment
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:background="#android:color/holo_green_light">
<Button
android:id="#+id/uploadButton"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center_horizontal"
android:layout_marginStart="8dp"
android:layout_marginTop="100dp"
android:layout_marginEnd="8dp"
android:background="#drawable/rounded_select_photo_button"
android:text="#string/UploadIMG"
android:textColor="#android:color/white"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/et_nickname"
android:layout_width="350dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:layout_marginStart="8dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="8dp"
android:background="#drawable/rounded_etext"
android:ems="10"
android:gravity="center"
android:hint="#string/hint_nickname"
android:importantForAutofill="no"
android:inputType="textPersonName|text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/uploadButton" />
<EditText
android:id="#+id/et_birthday"
android:layout_width="350dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:layout_marginStart="8dp"
android:layout_marginTop="36dp"
android:layout_marginEnd="8dp"
android:background="#drawable/rounded_etext"
android:gravity="center"
android:hint="#string/hint_birthday"
android:importantForAutofill="no"
android:inputType="none"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.511"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/et_nickname" />
</androidx.constraintlayout.widget.ConstraintLayout>
override onCreateView as follows:
override fun onCreateView(inflater: LayoutInflater, container:ViewGroup?, savedInstanceState: Bundle?): View? {
var view = inflater.inflate(R.layout.fragment_personal, container, false)
var button = view.findViewById(R.id.uploadButton)
button.setOnClickListener(view ->
Toast.makeText(this, "clicked", Toast.LENGTH_LONG).show()
)
return view
}
and rerun the app. by click on Button you should see clicked

why does this layout take 2-7 seconds to render in the UI?

So I have a layout with a RecyclerView That is taking between 2 and 7 seconds to load 7 items and show them in the list.
I ran the Android Studio profiler on the CPU using Sample Java methods and it appears that the bulk of the wait time is happening in the drawing and measuring of the views. 60% inRun() from FrameDisplayEventReceiver and nested under that call 53% by the first onMeasure() call.
Correct me if I'm wrong, but it appears that the cause of the slowness is a poorly designed xml layout.
Can any one show my what I've done in this layout that makes it run slowly and how I can make it more efficient?
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="#color/white"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="#+id/id_1"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_constraintTop_toTopOf="parent"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" >
<ProgressBar
android:id="#+id/id_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminateTint="#795548"
android:indeterminateTintMode="src_in"
android:layout_gravity="end"
/>
</androidx.appcompat.widget.Toolbar>
<LinearLayout
android:id="#+id/id_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintTop_toBottomOf="#id/id_1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
tools:ignore="UseCompoundDrawables">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:srcCompat="#drawable/image_1"
android:contentDescription="#string/description" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/text_1" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/id_4"
android:name="com.company.MyFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#color/white"
app:layoutManager="LinearLayoutManager"
app:layout_constraintTop_toBottomOf="#id/id_1"
tools:context="MyFragment"
tools:listitem="#layout/fragment_routeitem"
android:visibility="gone"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/id_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/fab_margin"
android:background="?attr/colorPrimary"
android:tint="#color/white"
app:backgroundTint="?attr/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:rippleColor="?attr/colorPrimary"
app:srcCompat="#drawable/ic_add_white_24dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
Here is the Code as Request (Kotlin)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_routeitem_list, container, false)
val toolbar: Toolbar = view.findViewById(R.id.toolbar)
toolbar.title = resources.getString(R.string.my_route_title)
progressBar = view.findViewById(R.id.my_routes_loading)
emptyStateView = view.findViewById(R.id.empty_state)
recyclerView = view.findViewById(R.id.my_routs_list)
dataAdapter = MyRouteItemRecyclerViewAdapter(mutableListOf(), this)
recyclerView.layoutManager = LinearLayoutManager(context)
recyclerView.adapter = dataAdapter
startLoadData()
val newRouteFab: FloatingActionButton = view.findViewById(R.id.new_route_fab)
newRouteFab.setOnClickListener {
createNewRoute()
}
return view
}
fun startLoadData() {
launch{
val routeList: List<Route> = withContext(Dispatchers.IO){
Application.getDaoSession()
.routeDao
.queryBuilder()
.where(RouteDao.Properties.Id.isNotNull)
.orderDesc(RouteDao.Properties.VisibleModified)
.build().list()
}
setData(routeList.toMutableList())
progressBar.visibility = View.GONE
}
}

Categories

Resources