GridLayoutManager 3 columns in RecyclerView - android

I'm trying to make simple GridLayout with 3 columns but although I get 3 columns I get strange gap between rows. So, I get 3 images in row than one row that is empty (which it seems has height matching height of the row abov(that has images), then row of 3 images, than strange gap...etc. I expected not to get than blank gap. How to remove this blank gap? Trying to set wrap_content to layout_height on recycler view, and fragment didn't helped.
This is my code:
final GridLayoutManager layoutManager = new GridLayoutManager(this,3);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
This is xml of the activity:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.adriagate.adriagateonlineandroid.activities.ImagesActivity">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="#+id/fragmentImagesHeader"
android:name="com.adriagate.adriagateonlineandroid.fragments.ImagesHeader"
tools:layout="#layout/fragment_images_header" android:layout_width="match_parent"
android:layout_height="match_parent" >
</fragment>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="#+id/fragmentImagesGrid"
android:name="com.adriagate.adriagateonlineandroid.fragments.ImagesList"
tools:layout="#layout/fragment_images_list" android:layout_width="match_parent"
android:layout_height="wrap_content" >
</fragment>
</RelativeLayout>
Notice that this layout has an important fragment called fragmentImagesGrid which has this layout:
<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:context="com.adriagate.adriagateonlineandroid.fragments.ImagesList">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view_images"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:listitem="#layout/recycler_view_images_one_row"
>
</FrameLayout>
This i the layout of the one element inside recycler view:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
>
<ImageView
android:id="#+id/imageViewImagesAllOneRow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
tools:src="#drawable/slika_200_200"
/>
</FrameLayout>

Looks like it happens because your images have different sizes.
Change your item to LinearLayout and set its layout height and width to match_parent. Set gravity="center" (Of the linear layout).
Another issue is android:layout_height="wrap_content". The RecyclerView doesn't work well when you set the height property to wrap_content. If you still want to use RecyclerView with wrap_content, you can ckeck this solution

Related

Adjust Items inside recycler view + orientation changes

I have this problem - I guess someone can surely put light on what the heck is going wrong here...
My overall flow goes like this - Activity(#+id/activity) shows a fragment(#+id/image_container) from screen bottom when a button is tapped(keyboard closes if present and fragment shown sliding upwards from bottom), this fragment shows a viewpager with page indicator in linear layout, viewpager shows another fragment which contains recycler view with horizontal gridlayoutmanager. This recycler view contains various imageview and i need to arrange them keeping my rows fixed say 3 but columns can be variable based on screen width and density. Imageview has fixed height and width.
This is my activity layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/activity">
<include
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentTop="true" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="4dp"
android:id="#+id/divider"
android:layout_below="#+id/toolbar"/>
<include
layout="#layout/edit_bar"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_below="#+id/divider"/>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/edit_bar" />
<FrameLayout
android:id="#+id/image_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/container"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
// Fragment inside the image_container
<?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="wrap_content"
android:orientation="vertical"
android:weightSum="1">
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="12dp"
android:layout_weight=".9"/>
<LinearLayout
android:id="#+id/sliderDots"
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center_vertical|center_horizontal"
android:orientation="horizontal"
android:layout_weight=".1"/>
// Fragment inside viewpager
<?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="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="26dp"
android:paddingRight="26dp">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
// Item inside recycler view
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/imageView"
android:layout_width="48dp"
android:layout_height="48dp"
tools:visibility="visible"
android:layout_margin="6dp"/>
// Fragment code which gets added to image_container upon button tap
FragmentManager fm = activity.getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.view_transition_up_from_bottom, R.anim.view_transition_down_to_bottom);
fragmentTransaction.add(R.id.image_container, new ImageFragment(activity.getApplicationContext())).commit();
Inside this ImageFragment i calculate the rows(this is fixed let's say 3) and columns(Math.round((dpWidth - 52) / 60)) - 52(26*2 - for both side of recycler view, 60 - space occupied my 1 item including width and margin(L+R)), hence i know the images to be shown in each page and pass it to ImageViewFragment containing the recycler view which is used by adapter.
I use
recyclerView.setLayoutManager(new CustomGridLayoutManager(getActivity(), 3, CustomGridLayoutManager.HORIZONTAL, false));
to make my recycler view and hence i use fixed rows.
So my question is -
1. I wrote the code to keep my rows fixed and calculate columns based on screen width but sometimes i see too much spacing from right side of recycler view to screen edge, i kept it fixed with 26 dp but it shows more that that. How can i implement this type of functionality where i see uniform spacing in grid form??
2. How to handle orientation change when grid fragment is open, my activity is not redrawn. as i see i can only override onConfigurationChanged.

Android RecyclerView scrollbar doesn't work

the scrollbar in my RecyclerView doesn't work, I mean I can scroll the RecyclerView with touch, I also can see the scrollbar but it's can not move
Before ask the question I also tried with some answers here for example:
https://www.dev2qa.com/android-recyclerview-horizontal-scroll-example/
Android RecyclerView Scroll not working after Layout edit
But none of them work for me
Below is my code, thanks in advance
Layout file:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HexViewActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_hex"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fadeScrollbars="false"
android:scrollbarSize="15dp"
android:scrollbars="vertical" />
</ScrollView>
Code to create the view
RecyclerView recyclerView = findViewById(R.id.rv_hex);
recyclerView.setLayoutManager(new LinearLayoutManager(HexConstant.app_ontext));
m_fileDataAdapter = new FileDataAdapter(HexConstant.app_ontext, files[0]);
recyclerView.setAdapter(m_fileDataAdapter);
recyclerView.setNestedScrollingEnabled(true);
Is there any example for Scrollbar + recyclerView, pls let me know
Change the outer layout with scroll view layout.
<ScrollView
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/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HexViewActivity">
<!-- Your stuff here -->
</ScrollView>
Have you tried using RecyclerView's attribute app:fastScrollEnabled="true"?
Check the full answer here. https://stackoverflow.com/a/46026362/6455501

Android Studio - How to inflate an xml layout containing a view that use percentage params

I am here because I would like to use multiple times the same cardView layout, changing simply some information contained in it and then adding it to a view contained in the main xml layout.
The problem is that the view to which append the created cardViews is a PercentRelativeLayout, so I need to specify cardView's xml layout using Percent params.
I show you an example of what I tried (with no success) to make it a bit more clear.
main_layout.xml
<ScrollView
android:id="#+id/scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/textview"
percent:layout_heightPercent="85%"
percent:layout_marginTopPercent="0.5%">
<android.support.percent.PercentRelativeLayout
android:id="#+id/scrollview_prl"
android:layout_width="match_parent"
android:layout_height="match_parent">
...here I want to add views programmatically...
</android.support.percent.PercentRelativeLayout>
...
cardView_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:percent="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_width="match_parent"
percent:layout_heightPercent="65%"
percent:layout_marginTopPercent="2%"
percent:layout_marginLeftPercent="2%"
percent:layout_marginRightPercent="2%"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="4dp">
</android.support.v7.widget.CardView>
Then, this is the piece of Java code I wrote (in a fragment, but this does not matter now)
myfragment.java
...
CardView cv = (CardView)
LayoutInflater.from(getActivity()).inflate(R.layout.cardView_layout, null)
cv.setId(currentId++);
PercentRelativeLayout prl = (PercentRelativeLayout)
view.findViewById(R.id.scrollview_prl);
prl.addView(cv);
...
As you can see I simply keep a currentId variable to assign different ids to the cardviews I am going to create.
Clearly, these cardviews must be one below the other, and I do not even know how to add this other param to them programmatically.
The final layout I would like to obtain is something like this:
<ScrollView
android:id="#+id/scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/textview"
percent:layout_heightPercent="85%"
percent:layout_marginTopPercent="0.5%">
<android.support.percent.PercentRelativeLayout
android:id="#+id/scrollview_prl"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
xmlns:percent="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#id/firstcardview"
android:layout_width="match_parent"
percent:layout_heightPercent="65%"
percent:layout_marginTopPercent="2%"
percent:layout_marginLeftPercent="2%"
percent:layout_marginRightPercent="2%"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="4dp">
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
xmlns:percent="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#id/secondcardview"
android:layout_width="match_parent"
percent:layout_heightPercent="65%"
percent:layout_marginTopPercent="2%"
percent:layout_marginLeftPercent="2%"
percent:layout_marginRightPercent="2%"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="4dp">
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
xmlns:percent="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#id/thirdcardview"
android:layout_width="match_parent"
percent:layout_heightPercent="65%"
percent:layout_marginTopPercent="2%"
percent:layout_marginLeftPercent="2%"
percent:layout_marginRightPercent="2%"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="4dp">
</android.support.v7.widget.CardView>
....
</android.support.percent.PercentRelativeLayout>
...
Thanks in advance for your help.
The problem is in your inflate() call. You have to pass a parent value in order for the inflated view's LayoutParams to be interpreted correctly.
Try this instead:
CardView cv = (CardView) LayoutInflater.from(getActivity()).inflate(R.layout.cardView_layout, prl, false);
By passing prl (the PercentRelativeLayout) to inflate(), everything should work.

Android-Last item of the ListView is not visible

I have added the ListView inside ViewPager, as soon as the page is loaded the first tab is selected and the last item of the list does not show up for the first time, its becoming visible when I go to next tab and come back. I would like to see all the items of the listview when its loaded for the first time. Please help me to fix this problem.
This is the xml for the listview -
<?xml version="1.0" encoding="utf-8"?>
<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">
<ListView
android:id="#+id/listview_categories"
android:layout_width="fill_parent"
android:divider="#color/light_gray_a1"
android:dividerHeight="0.667dp"
android:background="#color/white"
android:layout_height="fill_parent" />
</RelativeLayout>
add bottom padding like this and change fill_parent to match_parent
<?xml version="1.0" encoding="utf-8"?>
<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">
<ListView
android:id="#+id/listview_categories"
android:layout_width="match_parent"
android:divider="#color/light_gray_a1"
android:dividerHeight="0.667dp"
android:paddingBottom="40dp"
android:background="#color/white"
android:layout_height="match_parent" />
</RelativeLayout>
I had this issue. Just now I solved it. Look for android:layout_weight in all the Layouts you used and if it is set some non zero values like android:layout_weight="1" or android:layout_weight="0.91", then set them to zero like this,
android:layout_weight="0"

Why does my fragment fill the screen qua height? - Android

I've got a layout like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/wrapper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background">
<fragment
android:id="#+id/fragment_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
class="<headerClass>"/>
<fragment
android:id="#+id/fragment_body"
android:layout_below="#id/fragment_header"
android:layout_above="#+id/fragment_banner"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="<bodyClass>"/>
<fragment
android:id="#id/fragment_banner"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
class="<BannerClass>"/>
</RelativeLayout>
Notice that the first fragment has "wrap_content" as param for the layout_height.
This fragment is defined qua layout as:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/headerWrapper"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="#drawable/header_background">
// stuff here...
</RelativeLayout>
As you can see the height is set to 40dp.
Yet, the header takes the full height of the screen. I am not sure why. I was looking at the layoutInflater class, but since I am unfamiliar with it I cannot see any problems with that. I use this line to inflate my fragment:
View view = inflater.inflate(R.layout.fragment_header, container, false);
Why is it showing this behavior and how can I fix it?
I've managed to fix it by putting every fragment in its own LinearLayout and giving the first a height of 40dp. I don't think this is the best solution, but it's all I could come up with.

Categories

Resources