RecyclerView does not show any items when included - android

I have a Recycler view with the code below, which is working perfectly with all items when the view is accessed directly, but when it is included the RecyclerView is visible but empty
RecyclerView View:
<?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=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/list_items"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/purple_200"
app:layoutManager="LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
Parent View
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<include layout="#layout/list_fragment" />
<include layout="#layout/controls_fragment" />
</LinearLayout>
In my list fragment onCreateView function I also have the below code
val context = requireContext()
binding.listItems.layoutManager = LinearLayoutManager(context)
val listItemAdapter = ListItemAdapter(context, items)
binding.listItems.adapter = listItemAdapter

The issues is fixed by using "fragment" instead of "include" in the Parent View.
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<fragment
android:id="#+id/listFragment"
android:layout_width="match_parent"
android:layout_height="500sp"
android:name="com.example.project.listFragment"
tools:layout="#layout/list_fragment">
</fragment>
<fragment
android:layout_width="match_parent"
android:layout_height="200sp"
android:id="#+id/controlsFragment"
android:name="com.example.project.ControlsFragment"
tools:layout="#layout/controls_fragment">
</fragment>

Related

How to add text views and buttons on Google Maps fragment?

This is a simple layout and I wanted to add text views and 5 buttons on it but whatever I do those buttons don't appear on the map.
<?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">
<!--fragment to display our maps-->
<!--Search View for the google autocomplete support -->
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" />
</RelativeLayout>
Your xml should look something like this:
<?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:layoutDirection="ltr">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/map_layout"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="#+id/btn"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_marginBottom="25dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
you can add more buttons/TextView

How to fix swiperefresh when one view is visible/invisible

I have recyclerview list.
If list is empty recyclerview goes to invisible or gone and another view appears to visible
If list is not empty recyclerview goes to visible.
It works fine, but when we have empty list another view appears I can't swipe to refresh in the right place (look at the screen).
I tried https://github.com/airbnb/epoxy/issues/74 but it hadn't help me.
my xml
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.example.deepdiveandroid.base.ui.view.FullScreenErrorStateView
android:id="#+id/vErrorStateNotification"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:id="#+id/vSwipeToRefreshNotification"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvNotificationItems"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingTop="#dimen/spacing_16"
android:paddingBottom="#dimen/spacing_72"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="#layout/layout_list_item_notification" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</LinearLayout>
</FrameLayout>
Try to change your xml layout to this
<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout 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">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.deepdiveandroid.base.ui.view.FullScreenErrorStateView
android:id="#+id/vErrorStateNotification"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvNotificationItems"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingTop="#dimen/spacing_16"
android:paddingBottom="#dimen/spacing_72"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="#layout/layout_list_item_notification" />
</FrameLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

Zoom photo inside viewpager2

I tried to use PhotoŁDarweeView inside ViewPager2. When I want to zoom in on the photo horizontally, it does not zoom in and instead the ViewPager2 swipes.
activity_media_view:
<?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:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black"
tools:context=".ui.MediaTopicActivity">
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/viewPager"
android:layout_width="0dp"
android:layout_height="match_parent"
app:layout_constraintBottom_toTopOf="#+id/title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/header"
tools:background="#drawable/one"
tools:layout_height="300dp" />
<!-- other views -->
</androidx.constraintlayout.widget.ConstraintLayout>
fragment_photo:
<?xml version="1.0" encoding="utf-8"?>
<me.relex.photodraweeview.PhotoDraweeView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" />
I resolved this by enabling nestedScrolling for my viewPager2
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled="true" />

The last element of a recyclerView is being cutoff inside swipeToRefresh layout in Android

I have a recyclerView is positioned inside a SwipeRefreshLayout , the problem is the last element of the recyclerView is always cutoff. here is the Layout code of the fragment.
<?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=".ui.deliveries.DeliveriesFragment">
<ProgressBar
android:id="#+id/DeliveryProgressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="invisible"/>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/deliveriesList"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</RelativeLayout>

cannot resolve R when applying BottomNavigationView bottombar

when I add bottomNavigationView, I got cannot resolve R , but when I remove the bottomNavigationView the error disappear, so why I am getting such error ?
the below code result to unresolved R
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.learn2crack.recyclerviewgrid.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/card_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:background="#android:color/holo_blue_light"
app:menu="#menu/my_navigation_items" />
</android.support.design.widget.CoordinatorLayout>
when remove it as the below the error disappear
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.learn2crack.recyclerviewgrid.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/card_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.design.widget.CoordinatorLayout>
If your XML's contain errors your generated file might not build try erasing most of their content and making sure id's are not duplicated.
Check if you have imported the right lib to work with BottomNavigation and none of your props are null.
Here is my Activity code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="MainActivity">
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemIconTint="#drawable/bottom_menu_selector"
app:itemTextColor="#drawable/bottom_menu_selector"
app:menu="#menu/bottom_navigation_menu" />
<ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/bottom_navigation"/>

Categories

Resources