This is sort of a strange bug I discovered.
But if I have a RecyclerView in a fragment and I open then close my DrawerLayout, my RecyclerView comes in focus. That means closing my DrawerLayout will cause the ScrollView to jump to my RecyclerView. Obviously, I would like the ScrollView's position to not move when the DrawerLayout is closed, and definitely not settle on my RecyclerView.
CODE
Throughout my application, I have RecyclerViews contained within ScrollViews, similar to this setup:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="180dp">
<android.support.v7.widget.AppCompatImageView
android:id="#+id/header_photo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:tint="#80000000"
android:scaleType="centerCrop"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
<TextView android:id="#+id/header_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:textColor="#color/text_SecondaryColor"
android:textSize="#dimen/header_xxlarge"/>
<View android:id="#+id/divider"
android:layout_width="0dp"
android:layout_height="#dimen/hr_thick"
android:background="#color/accent_PrimaryColor"/>
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:orientation="vertical"
android:layout_margin="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button android:id="#+id/button_more"
android:layout_marginTop="6dp"
android:layout_marginBottom="6dp"
android:layout_width="400px"
android:layout_height="100px"
android:layout_gravity="center"
android:gravity="center"
android:background="#drawable/button_dark"
android:textColor="#drawable/button_dark_textcolor"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
And my DrawerLayout's XML design is arranged like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:layout_height="wrap_content"
android:layout_width="match_parent"
layout="#layout/toolbar" />
</LinearLayout>
<FrameLayout
android:id="#+id/contentframe"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<RelativeLayout
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/background_Secondary">
<!-- Relative Layout : Back Button -->
<RelativeLayout
android:id="#+id/menu_backcontainer"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<ImageView
android:id="#+id/menu_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:src="#drawable/angle"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<!-- List View : Menu Drawer Content -->
<ListView
android:id="#+id/menu"
android:layout_below="#id/menu_backcontainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:choiceMode="singleChoice"
android:background="#color/background_PrimaryDarkColor">
</ListView>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
I am not sure what other Java Code I need to include here but this is how I instantiate my RecyclerView:
recyclerView = (RecyclerView)view.findViewById(R.id.recyclerview);
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext().getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setNestedScrollingEnabled(false);
And For My DrawerLayout:
drawerlayout = (DrawerLayout)findViewById(R.id.drawer);
menu = (ListView)findViewById(R.id.menu);
MenuAdapter adapter = new MenuAdapter(
this,
R.layout.listview_menuitem,
menuItemArray);
menu.setAdapter(adapter);
menu.setOnItemClickListener(new menuItemClickListener());
menuToggle = new ActionBarDrawerToggle(
this,
drawerlayout,
toolbar,
R.string.app_name,
R.string.app_name);
drawerlayout.setDrawerListener(menuToggle);
menuToggle.syncState();
Gee, it is amazing what a moment away from the computer and a cup of coffee will do.
I simply set the focusableInTouchMode attribute of my FrameLayout to true.
I guess this nullifies the RecyclerViews desire to be the focused element in the ScrollView when I touch anywhere on the screen after the DrawerLayout is opened. Perhaps there is a better, more thorough explanation. But I hope this helps anyone else who may experience this problem.
Related
I'm making an app and I can't find any solutions.Here is my code:
<?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"
tools:context=".MainActivity"
android:background="#color/DarkGray"
android:orientation="vertical">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/BackGroundColor"
android:orientation="horizontal"
android:paddingTop="4dp"
android:paddingBottom="4dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAlignment="center"
android:textAllCaps="true"
android:textColor="#color/SecondaryTextColor"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAlignment="center"
android:textAllCaps="true"
android:textColor="#color/SecondaryTextColor"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="#color/BlueGray"
android:nestedScrollingEnabled="false"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/BackGroundColor"
android:padding="4dp"
android:textAlignment="center"
android:textColor="#color/PrimaryTextColor"
android:textSize="22sp" />
<ListView
android:id="#+id/eventsListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="2dp"/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</LinearLayout>
What I'm trying to achive is add CardViews inside my ListView and be able to scroll not just the ListView but the whole view. I tried RecyclerView before but that didn't work for me either.
Here's my idea:
and Scroll the whole thing not just the ListView.
You need to use NestedScrollView.
NestedScrollView is just like ScrollView, but it supports acting as both a nested scrolling parent and child on both new and old versions of Android. Nested scrolling is enabled by default.
Please refer NestedScrollView documentation
Don't forget to add recyclerView.setNestedScrollingEnabled(false); to your RecyclerView.
I am developing an app where there is a left Drawer on the Home screen. The drawer fits perfectly on most devices, but on some devices, the drawer is covered whether the screen has overview button(phone without physical button, using on screen button) or its swallowed under the bottom of the screen(mostly happen on Xiaomi, and yes, i've tried on 5 xiaomi phones).This also happening on the emulator from android studio. The Question is, can the app knows if there a overview button on the phone , so the app can be developed to put the layout on top(not covering) of it. Or is there another way, more simple and efficient?
Here is the layout code:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/drawer_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
<!-- *********** NAVIGATION DRAWER *********** -->
<LinearLayout
android:id="#+id/left_drawer"
android:layout_width="#dimen/drawer_width"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#color/drawerColor"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:paddingLeft="#dimen/drawer_user_padding_left"
android:paddingBottom="#dimen/drawer_user_padding_bottom"
android:paddingTop="#dimen/drawer_user_padding_top_newest"
android:orientation="vertical" >
<TextView
android:id="#+id/referal_ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/nav_fontsize"
android:textColor="#color/orange"
android:textStyle="bold"
android:text="\nReferal ID: " />
<TextView
android:id="#+id/profile_no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/content_fontsize"
android:textColor="#color/orange"
android:textStyle="bold"
android:text="000" />
</LinearLayout>
<LinearLayout
android:id="#+id/userdata_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/theme"
android:paddingLeft="#dimen/drawer_user_padding_left"
android:paddingTop="#dimen/drawer_separator_margin_top"
android:paddingBottom="#dimen/drawer_user_padding_bottom"
android:orientation="vertical" >
<TextView
android:id="#+id/profile_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/content_fontsize"
android:textColor="#color/white"
android:textStyle="bold"
android:text="Name" />
<TextView
android:id="#+id/profile_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/content_fontsize"
android:textColor="#color/white"
android:text="email#email.com" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/icon_point"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:adjustViewBounds="true"
android:background="#android:color/transparent"
android:src="#drawable/magno_coin"/>
<TextView
android:id="#+id/profile_credit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="#dimen/content_fontsize"
android:textColor="#color/white"
android:textStyle="bold"
android:text="0"/>
</LinearLayout>
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:id="#+id/upper_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/drawer_upper_nav_margin"
android:isScrollContainer="false"
android:scrollbars="none" >
</ListView>
<View
android:id="#+id/upper_separator"
android:layout_width="fill_parent"
android:layout_height="#dimen/drawer_separator_height"
android:layout_marginTop="#dimen/drawer_separator_margin_top"
android:layout_marginBottom="#dimen/drawer_separator_margin_bottom"
android:background="#color/contentColor"/>
<ListView
android:id="#+id/middle_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:isScrollContainer="false"
android:scrollbars="none">
</ListView>
<View
android:id="#+id/middle_separator"
android:layout_width="fill_parent"
android:layout_height="#dimen/drawer_separator_height"
android:layout_marginTop="#dimen/drawer_separator_margin_top"
android:layout_marginBottom="#dimen/drawer_separator_margin_bottom"
android:background="#color/contentColor"/>
<ListView
android:id="#+id/lower_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:isScrollContainer="false"
android:scrollbars="none">
</ListView>
<View
android:id="#+id/lower_separator"
android:layout_width="fill_parent"
android:layout_height="#dimen/drawer_separator_height"
android:layout_marginTop="#dimen/drawer_separator_margin_top"
android:layout_marginBottom="#dimen/drawer_separator_margin_bottom"
android:background="#color/contentColor"/>
<ListView
android:id="#+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:isScrollContainer="false"
android:layout_marginBottom="#dimen/item_margin"
android:scrollbars="none">
</ListView>
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="#dimen/drawer_separator_height"
android:background="#color/theme"/>
<TextView
android:id="#+id/current_version"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom|right"
android:layout_alignParentBottom="true"
android:layout_marginTop="#dimen/item_space_margin"
android:layout_marginBottom="#dimen/item_space_margin"
android:layout_marginRight="#dimen/version_margin"
android:textSize="#dimen/smaller_fontsize"
android:textColor="#color/black"
android:text="ver 1.0.0"/>
</LinearLayout>
</LinearLayout></android.support.v4.widget.DrawerLayout>
On the picture i scrolled down until cant be scrolled anymore
Sorry, if I dont understand your question.
I dont see in your xml android.support.design.widget.NavigationView - its container for drawer content. You need to add implementation 'com.android.support:design:27.1.1' to your app dependencies to use NavigationView.
And if you want to do static header for your drawer, there is app:headerLayout="#layout/nav_header" property, where nav_header is your header layout.
For other items there are app:menu="#menu/drawer_view property, where drawer_view is your drawers menu. In this menu you can put as many items as you wish and they will be scrollable if needed. Also you can design it as you want and they are easy for handling in NavigationView.OnNavigationItemSelectedListener
See this Googles guide for more details. Hope it will help you.
This is a common problem. Place a listview (or more) inside a scrollview is not good. If you have a finite number of elements I suggest you to build your view programmatically (use addView).
However if you don't want to change your code read that: list view inside scrollview.
Basically you need to calculate the real height of the listview for every item (getMeasuredHeight) after notifyDataSetChanged. This should fix your problem.
To prevent overlapping by Bottom Soft NavigationBar add this to your styles.xml in a values-v21 dir (if dir not exist, create one and copy same styles.xml file from value):
<item name="android:windowDrawsSystemBarBackgrounds">false</item>
Like this eg -
<style name="Theme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowDrawsSystemBarBackgrounds">false</item>
.....
.....
</style>
i have a listview in a fragment(Total 3 tabs in viewpager) and using this below code to get swipe down to refresh functionality but on swiping left to another fragment swipe refresh function is called how to restrict this(happens only if listview is empty).
<RelativeLayout 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.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragmenttab1_swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragmenttab1_list_item"
android:dividerHeight="1dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:longClickable="true">
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/fragmenttab1_emptyElement"
android:text="No Posts"
android:textStyle="bold"
android:textSize="15sp"
android:visibility="gone"
android:layout_centerInParent="true"
android:textColor="#android:color/darker_gray"/>
</RelativeLayout>
Java code
listView.setAdapter(adapter);
listView.setEmptyView(getView().findViewById(R.id.fragmenttab1_emptyElement));
I'm not able to either open fragment in drawer or my layout listeners doesn't work. I need an example of xml containing drawerlayout and some other layout. Below is my code of in xml.
<RelativeLayout
....some initialisation
Relative layout holding toolbar
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="64dp"
android:background="#0D4050" >
<ImageView
android:id="#+id/actionbar_imageview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="15dp"
android:layout_weight="2"
android:gravity="center"
android:maxHeight="50dp"
android:src="#drawable/symbol" />
</android.support.v7.widget.Toolbar>
Relative layout holding my custom views
<RelativeLayout
android:id="#+id/rl1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/toolbar"
android:layout_centerHorizontal="true"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
>
<TextView
android:id="#+id/textView"
android:layout_width="300dp"
android:layout_height="40dp"/>
<TextView
android:id="#+id/cts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
/>
<TextView
android:id="#+id/snt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/cts"
/>
</RelativeLayout>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar"
>
Frame layout serves as my fragment holder
<FrameLayout
android:id="#+id/home_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
<!-- The navigation drawer -->
Listview to display drawer
</RelativeLayout>
I tried inserting rl1 in drawerlayout also added it as a topmost layer but either drawerlayout doesnt load fragments or I'm not able to call the onclick listeners on rl1.
re
Also, I tried hiding and showing the layouts dynamically but same result. :(
My expectaion is working sample of drawerlaout with some custom view.
For some reason android:layout_gravity="bottom" and android:layout_alignParentBottom="true" on my ViewPager (the classical button) do not seem to place the image at the bottom of the screen and I'm unsure why.
Any suggestions are much appreciated.
SCREENSHOT:
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="120dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
<com.google.android.youtube.player.YouTubePlayerView
android:id="#+id/youtubeplayerview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<com.idg.omv.ui.widget.VideosListView
android:id="#+id/videosListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:id="#+id/footer"
android:layout_width="fill_parent"
android:layout_height="70dip"
android:layout_alignParentBottom="true" >
<ScrollView
android:id="#+id/groupScrollView"
android:layout_width="fill_parent"
android:layout_height="match_parent" >
</ScrollView>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:src="#drawable/scroll_lt_arrow" />
<Button
android:id="#+id/fav_up_btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="#drawable/fav_up_btn1" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="#drawable/scroll_rt_arrow" />
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
The Navigation Drawer documentation states (emphasis mine):
To add a navigation drawer, declare your user interface with a DrawerLayout object as the root view of your layout. Inside the DrawerLayout, add one view that contains the main content for the screen (your primary layout when the drawer is hidden) and another view that contains the contents of the navigation drawer.
You cannot have multiple views in your DrawerLayout xml (other than your drawer and content). I would recommend putting your content in a fragment, and then inflating it in code similar to the way the examples do.