我遇到问题,RecyclerView
StaggeredGridLayoutManager
项目包含imageview
,并且imageview
使用glide
加载了图片。
我面临的问题是,在图像加载后,它们不会交错并且尺寸相同,两列之间也存在很大的差距。如何在列中没有间隙的情况下改变图像的高度?
答案 0 :(得分:6)
我遇到了同样的问题。对我有用的是为我的ImageView设置android:adjustViewBounds="true"
。我甚至没有设置scaleType。
以下是我的布局文件item.xml的完整内容。
<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gif_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorFiller"
android:adjustViewBounds="true"/>
希望这有帮助!
答案 1 :(得分:4)
最后,我能够找到灵魂。我不得不在Staggered Recycler View中创建自定义imageview以改变其纵横比。
mcPiece.addEventListener(MouseEvent.MOUSE_DOWN, fDragMC);
mcPiece.addEventListener(MouseEvent.MOUSE_UP, fDropMC);
function fDragMC(event: MouseEvent): void {
this.startDrag();
}
function fDropMC(event: MouseEvent): void {
this.stopDrag();
}
然后在适配器的onBindViewHolder中,我通过 -
设置图像的纵横比public class DynamicHeightNetworkImageView extends ImageView {
private float mAspectRatio = 1.5f;
public DynamicHeightNetworkImageView(Context context) {
super(context);
}
public DynamicHeightNetworkImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DynamicHeightNetworkImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setAspectRatio(float aspectRatio) {
mAspectRatio = aspectRatio;
requestLayout();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int measuredWidth = getMeasuredWidth();
setMeasuredDimension(measuredWidth, (int) (measuredWidth / mAspectRatio));
}
}
答案 2 :(得分:1)
在项目布局中将android:adjustViewBounds="true"
添加到ImageView并在Glide中使用CenterCrop
和FitCenter
,如下所示:
Glide.with(vh.view.getContext())
.load(item.getUrl())
.centerCrop()
.fitCenter()
.thumbnail(0.3f)
.placeholder(R.drawable.img_placeholder)
.into(vh.image);
答案 3 :(得分:0)
首次初始化您的RecyclerView
ObservableList
然后你的适配器(根据你的需要编辑)
RecyclerView recyclerGallery = (RecyclerView)findViewById(R.id.gallery_recycler_view);
gaggeredGridLayoutManager = new StaggeredGridLayoutManager(3, 1);
recyclerGallery.setLayoutManager(gaggeredGridLayoutManager);
galleryAdapter = new GalleryAdaptor(ProfImageGallery.this, imgsUrl);
recyclerGallery.setAdapter(galleryAdapter);
替换Picaso代码,即
public class GalleryAdaptor extends RecyclerView.Adapter<GalleryAdaptor.ViewHolder> {
private Context mContext;
private ArrayList<String> mDataset;
// Provide a suitable constructor (depends on the kind of dataset)
public GalleryAdaptor(Context mContext, ArrayList<String> mdataList) {
this.mContext = mContext;
this.mDataset = mdataList;
}
// Create new views (invoked by the layout manager)
@Override
public GalleryAdaptor.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.row_gallery, parent, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
String imageUrl = mDataset.get(position);
holder.progressBar.setVisibility(View.VISIBLE);
//load the image url with a callback to a callback method/class
Picasso.with(mContext)
.load(imageUrl)
.into(holder.workImage,
new ImageLoadedCallback(holder.progressBar) {
@Override
public void onSuccess() {
if (holder.progressBar != null) {
holder.progressBar.setVisibility(View.GONE);
}
}
});
}
使用Glide。
Picasso.with(mContext)
.load(imageUrl)
.into(holder.workImage,
new ImageLoadedCallback(holder.progressBar) {
@Override
public void onSuccess() {
if (holder.progressBar != null) {
holder.progressBar.setVisibility(View.GONE);
}
}
});
答案 4 :(得分:0)
对我来说,它正常工作
Picasso.get()
.load(uploadCurrent.getImageUrl())
.placeholder(R.drawable.loading)
.config(Bitmap.Config.RGB_565)
.into(holder.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="wrap_content"
android:layout_margin="2dp"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageView
android:id="@+id/id_imgItem"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"/>
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/id_imgItemGets"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:gravity="center_horizontal"
android:textSize="18sp"/>
</RelativeLayout>