kevz
kevz

Reputation: 2737

How to reduce space between items of RecyclerView in Android?

I have implemented Horizontal RecyclerView for Images. Horizontal navigation work fine but unwanted space is drawn between two items of RecyclerView.

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

RecyclerView item layout-

<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>

My RecyclerView layout-

<?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 Setup-

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());

My RecyclerView Adapter-

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();
}
}

Upvotes: 2

Views: 6014

Answers (1)

CptEric
CptEric

Reputation: 907

edited answer after chat with user

the problem was the size of the images being too different to the different android devices, and the impossibility to stretch them to prevent the empty spaces. given that, i recommend the use of a ViewPager instead of a RecyclerView, having that it will provide the same functionality buth with a better controll on how the imageView will react with different sized images.

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

Upvotes: 1

Related Questions