代码之家  ›  专栏  ›  技术社区  ›  user2606782

调整回收器视图内的项目+方向更改

  •  0
  • user2606782  · 技术社区  · 7 年前

    我有这个问题——我想肯定有人能弄清楚这里到底出了什么问题。。。

    我的整个流程是这样的-活动(@+id/Activity)显示屏幕底部的一个片段(@+id/image_容器),当点击一个按钮时(键盘关闭,显示的片段从底部向上滑动),该片段显示一个带有线性布局的页面指示器的viewpager,viewpager显示了另一个片段,其中包含带有水平gridlayoutmanager的recycler视图。这个recycler视图包含各种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>
    

    //图像容器中的碎片

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

    //点击按钮后添加到图像容器中的片段代码

    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个项目,包括宽度和边距(L+R)),因此,我知道每个页面中要显示的图像,并将其传递给ImageViewFragment,其中包含适配器使用的recycler视图。 我用 recyclerView.setLayoutManager(new CustomGridLayoutManager(getActivity(), 3, CustomGridLayoutManager.HORIZONTAL, false)); 使我的回收者视图,因此我使用固定行。

    所以我的问题是-
    1.我编写代码是为了固定行数,并根据屏幕宽度计算列数,但有时我看到回收器视图右侧到屏幕边缘的间距太大,我将其固定为26 dp,但它显示的更多。当我看到网格形式的均匀间距时,如何实现这种功能??
    2.当网格片段打开时,如何处理方向变化,我的活动不会被重新绘制。在我看来,我只能超越 onConfigurationChanged .

    0 回复  |  直到 7 年前