在回收者视图中调整项目并更改方向

时间:2018-10-26 17:58:33

标签: android android-fragments android-recyclerview android-viewpager

我有这个问题-我想肯定有人可以弄清楚这里到底出了什么问题...

我的总体流程是这样的-当轻按按钮时,Activity(@ + id / activity)从屏幕底部显示一个片段(@ + id / image_container)(如果存在,键盘将关闭,并且片段从底部向上滑动),该片段显示了一个带有线性布局页面指示符的viewpager,viewpager显示了另一个包含带有水平gridlayoutmanager的回收器视图的片段。此回收器视图包含各种imageview,我需要对其进行排列以使行固定为3,但列可以根据屏幕的宽度和密度而变化。 Imageview具有固定的高度和宽度。

这是我的活动布局

<?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>

// 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"/>

// 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>

//回收站视图中的项目

<?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"/>

//片段代码,在点击按钮后会添加到image_container中

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();

在此ImageFragment的内部,我计算行(固定为3)和列(Math.round((dpWidth-52)/ 60))-52(26 * 2-对于回收站视图的两侧都是60-空间占据了我的1个项目,包括width和margin(L + R)),因此我知道要在每个页面中显示的图像,并将其传递给包含适配器使用的回收站视图的ImageViewFragment。 我用 recyclerView.setLayoutManager(new CustomGridLayoutManager(getActivity(), 3, CustomGridLayoutManager.HORIZONTAL, false)); 使我的回收商视图,因此我使用固定的行。

所以我的问题是-
1.我编写了代码以保持行固定并根据屏幕宽度计算列,但是有时我看到从回收站视图的右侧到屏幕边缘的间距太大,我将其固定为26 dp,但它显示的更多。在网格形式下看到均匀间距的情况下,如何实现这种功能?
2.当打开网格片段时,如何处理方向变化,我的活动没有重画。如我所见,我只能覆盖onConfigurationChanged

0 个答案:

没有答案
相关问题