0

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.

Ali Khaki
  • 1,184
  • 1
  • 13
  • 24
user2606782
  • 260
  • 1
  • 5
  • 15
  • Answer1: Instead of having fixed spaces, try using it inside `ConstraintLayout`. Answer2: instead of onConfigurationChanged() use `ViewModel` – Ümañg ßürmån Oct 26 '18 at 18:04
  • can u be a bit more descriptive please!! – user2606782 Oct 26 '18 at 18:06
  • OKay, ConstraintLayout is more adaptive layout and can adjust to screens of different sizes and resolutions. ViewModel of the Architectural Components survives configuration changes, you'll not be needing onConfigurationChanges() method. – Ümañg ßürmån Oct 26 '18 at 18:08
  • can you please tell me how?.. by spaces i guess you mean to say spacing between 2 items of recycler view and that imageView should have constraintlayout as root view?? – user2606782 Oct 26 '18 at 18:14

0 Answers0