简体   繁体   English

使用自定义GridlayoutManager android的Recyclerview

[英]Recyclerview with custom GridlayoutManager android

I am working on the custom layout of RecyclerView with GridLayoutManager 我正在使用GridLayoutManager RecyclerView的自定义布局

which should looks like this 应该看起来像这样

在此处输入图片说明

adapter.java adapter.java

@Override
    public void onBindViewHolder(ProfilePhotosViewHolder ProfilePhotosViewHolder, final int position) {

        model = list.get(position);


        if (position == 0) {
            ProfilePhotosViewHolder.relativeBucket.setVisibility(View.VISIBLE);
            ProfilePhotosViewHolder.relativeBucket.setDrawingCacheEnabled(true);
            ProfilePhotosViewHolder.relativeBucket.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            ProfilePhotosViewHolder.relativeBucket.layout(0, 0, ProfilePhotosViewHolder.relativeBucket.getMeasuredWidth(), ProfilePhotosViewHolder.relativeBucket.getMeasuredHeight());
            ProfilePhotosViewHolder.relativeBucket.buildDrawingCache(true);
            Bitmap b = Bitmap.createBitmap(ProfilePhotosViewHolder.relativeBucket.getDrawingCache());
            ProfilePhotosViewHolder.relativeBucket.setDrawingCacheEnabled(false);
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
            ProfilePhotosViewHolder.imgProfilePhotos.setImageBitmap(b);
        }

        if (position == 2) {
            ProfilePhotosViewHolder.relativeFollowers.setVisibility(View.VISIBLE);
            ProfilePhotosViewHolder.relativeFollowers.setDrawingCacheEnabled(true);
            ProfilePhotosViewHolder.relativeFollowers.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            ProfilePhotosViewHolder.relativeFollowers.layout(0, 0, ProfilePhotosViewHolder.relativeFollowers.getMeasuredWidth(), ProfilePhotosViewHolder.relativeFollowers.getMeasuredHeight());
            ProfilePhotosViewHolder.relativeFollowers.buildDrawingCache(true);
            Bitmap b = Bitmap.createBitmap(ProfilePhotosViewHolder.relativeFollowers.getDrawingCache());
            ProfilePhotosViewHolder.relativeFollowers.setDrawingCacheEnabled(false);
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
            ProfilePhotosViewHolder.imgProfilePhotos.setImageBitmap(b);
        }
        if (position == 5) {
            ProfilePhotosViewHolder.relativeFollowing.setVisibility(View.VISIBLE);
            ProfilePhotosViewHolder.relativeFollowing.setDrawingCacheEnabled(true);
            ProfilePhotosViewHolder.relativeFollowing.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            ProfilePhotosViewHolder.relativeFollowing.layout(0, 0, ProfilePhotosViewHolder.relativeFollowing.getMeasuredWidth(), ProfilePhotosViewHolder.relativeFollowing.getMeasuredHeight());
            ProfilePhotosViewHolder.relativeFollowing.buildDrawingCache(true);
            Bitmap b = Bitmap.createBitmap(ProfilePhotosViewHolder.relativeFollowing.getDrawingCache());
            ProfilePhotosViewHolder.relativeFollowing.setDrawingCacheEnabled(false);
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
            ProfilePhotosViewHolder.imgProfilePhotos.setImageBitmap(b);
        }
        if (position == 7) {
            ProfilePhotosViewHolder.relativePosts.setVisibility(View.VISIBLE);
            ProfilePhotosViewHolder.relativeFollowing.setDrawingCacheEnabled(true);
            ProfilePhotosViewHolder.relativeFollowing.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            ProfilePhotosViewHolder.relativeFollowing.layout(0, 0, ProfilePhotosViewHolder.relativeFollowing.getMeasuredWidth(), ProfilePhotosViewHolder.relativeFollowing.getMeasuredHeight());
            ProfilePhotosViewHolder.relativeFollowing.buildDrawingCache(true);
            Bitmap b = Bitmap.createBitmap(ProfilePhotosViewHolder.relativeFollowing.getDrawingCache());
            ProfilePhotosViewHolder.relativeFollowing.setDrawingCacheEnabled(false);
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
            ProfilePhotosViewHolder.imgProfilePhotos.setImageBitmap(b);
        }
        ProfilePhotosViewHolder.imgProfilePhotos.setImageResource(model.photos);

        ProfilePhotosViewHolder.imgProfilePhotos.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMethods.showToast(mContext, "" + position);
            }
        });

    }

layout.xml layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/img_photos"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
        android:src="@drawable/im_profile_monuments" />


    <RelativeLayout
        android:id="@+id/relative_bucket"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/grey_box"
        android:paddingBottom="10dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingTop="10dp"
        android:visibility="gone">

        <ImageView
            android:id="@+id/img_profile_bucket"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:src="@drawable/ic_profile_my_bucketlist" />

        <TextView
            android:id="@+id/tv_profile_bucket_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/img_profile_bucket"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp"
            android:gravity="center"
            android:singleLine="true"
            android:text="My"
            android:visibility="visible"
            android:textColor="@color/white"
            android:textSize="@dimen/txt_medium" />
        />

        <TextView
            android:id="@+id/tv_profile_bucket_list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv_profile_bucket_count"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp"
            android:gravity="center"
            android:singleLine="true"
            android:text="Bucket list"
            android:textColor="@color/white"
            android:textSize="@dimen/txt_medium" />
        />

    </RelativeLayout>

The problem is that the custom layout which is converted to Bitmap to fit to fixed position is not aligned properly 问题是,转换为Bitmap以适合固定位置的自定义布局未正确对齐

在此处输入图片说明

For keeping views square in Recycler view: 为了使视图在“回收者”视图中保持正方形:

to onBind function of ViewHolder I passed width of the column (it was calculated as screen size / number of columns) and inside ViewHolder was 到ViewHolder的onBind函数中,我传递了列的宽度(它被计算为屏幕大小/列数),并且在ViewHolder内部是

itemView.setLayoutParams(new FrameLayout.LayoutParams(width, width));

where itemView is ViewHolder view and width is column width. 其中itemView是ViewHolder视图,而width是列宽。 Of course you need FrameLayout to do that. 当然,您需要使用FrameLayout来执行此操作。

hope this helps. 希望这可以帮助。

cheers 干杯

EDIT: You can use 编辑:您可以使用

itemView.setLayoutParams(new RelativeLayout.LayoutParams(width, width));

if parent is Relative Layout 如果父级是相对布局

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM