Adjust Items inside recycler view + orientation changes

ぐ巨炮叔叔 提交于 2019-12-11 17:39:25

问题


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.

来源:https://stackoverflow.com/questions/53014041/adjust-items-inside-recycler-view-orientation-changes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!