recylerview项目内部如何在NavigationDrawer的动态列表内部居中?

时间:2018-08-29 12:05:54

标签: android android-recyclerview

我有两个不同的列表。一个用于访客帐户的列表,另一个用于登录帐户的列表。我曾经把抽屉适配器和导航抽屉弄成碎片。 İ是否要根据recylerview列表中列表大小的项目位置中心进行更改?

İ尝试了一些方法,但是没有用。

if (adapter.getItemCount()==2){
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT);
    rl_root.setGravity(Gravity.CENTER);
    rl_root.setLayoutParams(params);
}
 RelativeLayout rl_root;

这是xml文件

<RelativeLayout 
    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/rl_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context="com.smartapps.myos.Fragment.NavigationDrawerFragment">

    <!-- TODO: Update blank fragment layout -->

    <include
        android:id="@+id/inc_nav_head"
        layout="@layout/nav_header_layout"
        android:layout_width="match_parent"
        android:layout_height="100dp" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rec_frag_nav_list"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:layout_below="@+id/inc_nav_head"
        android:layout_gravity="center"
        android:visibility="visible"
        android:background="@drawable/bgcolormulti"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"
        tools:listitem="@layout/item_layout_nav_drawer" />
</RelativeLayout>

回收站项目视图

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/rl_nav_item_root"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_margin="5dp"
    android:paddingLeft="10dp">

    <ImageView
        android:id="@+id/iv_item_nav_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/nav_header_ic" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:id="@+id/rl_nav_item_root2"
        android:layout_height="match_parent"
        android:layout_alignBottom="@+id/iv_item_nav_icon"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_toEndOf="@+id/iv_item_nav_icon">

        <TextView
            android:id="@+id/tv_item_nav_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#fff" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_below="@+id/tv_item_nav_title"
            android:layout_marginTop="2dp"
            android:background="#fff">

        </RelativeLayout>
    </RelativeLayout>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

RelativeLayout rl_root;  rl_root = itemView.findViewById(R.id.rl_nav_item_root);

@Override     public void onBindViewHolder(@NonNull vHolder h,最终int位置){

    final DrawerItemModel m = list.get(position);

    if (position==0){

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT,100);
        params.setMargins(0,350,0,0);
        h.rl_root.setLayoutParams(params);

    }

}