gpt4 book ai didi

android - 如何减少 Android 中 RecyclerView 项目之间的空间?

转载 作者:行者123 更新时间:2023-11-30 01:55:17 24 4
gpt4 key购买 nike

我已经实现了 Horizo​​ntal RecyclerView for Images。水平导航工作正常,但在 RecyclerView 的两个项目之间绘制了不需要的空间。

Unwanted space is der on Left side of image item in RecyclerView Unwanted space between two images in RecyclerView

RecyclerView 项目布局-

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


<ImageView
android:id="@+id/imgView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/ic_launcher" />

</LinearLayout>

我的 RecyclerView 布局-

<?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="match_parent">

<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>

RecyclerView 设置-

recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
HlistViewAdapter adapter = new HlistViewAdapter(pArray);
recyclerView.setAdapter(adapter);
recyclerView.setItemAnimator(new DefaultItemAnimator());

我的 RecyclerView 适配器-

public class HlistViewAdapter extends RecyclerView.Adapter<HlistViewAdapter.ViewHolder>{

ArrayList<String> pathArray;

public HlistViewAdapter(ArrayList<String> pathArray){
this.pathArray = pathArray;
}

@Override
public HlistViewAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

View itemLayoutView = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.hlist_item_layout, null);

// create ViewHolder

ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}

@Override
public void onBindViewHolder(HlistViewAdapter.ViewHolder viewHolder, int i) {
viewHolder.imgViewIcon.setImageBitmap(BitmapFactory.decodeFile(pathArray.get(i)));
}

public static class ViewHolder extends RecyclerView.ViewHolder {

public ImageView imgViewIcon;

public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.imgView);
}
}

@Override
public int getItemCount() {
return pathArray.size();
}
}

最佳答案

与用户聊天后编辑的答案

问题是图像的大小对于不同的 android 设备来说差异太大,并且无法拉伸(stretch)它们以防止空白。鉴于此,我建议使用 ViewPager 而不是 RecyclerView,因为它将提供相同的功能,但可以更好地控制 imageView 如何对不同大小的图像使用react。

http://codetheory.in/android-image-slideshow-using-viewpager-pageradapter/

关于android - 如何减少 Android 中 RecyclerView 项目之间的空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32346813/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com