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

时间:2015-09-02 07:17:20

标签: android android-recyclerview

我已经实施了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();
}
}

1 个答案:

答案 0 :(得分:1)

与用户聊天后编辑的答案

问题是图像的大小与不同的Android设备太不相同,并且不可能拉伸它们以防止空的空间。鉴于此,我建议使用ViewPager而不是RecyclerView,它具有相同的功能,但可以更好地控制imageView对不同尺寸图像的反应。

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