I am currently developing an Android application that should display a bookshelf where each line contains a maximum of 3 images. I use gridview for this. But I am stuck at one point where I cannot change the background of the whole row in gridview. can anyone tell me how to do this?
Adapter used for gridview
private class SampleGridAdapter extends BaseAdapter{
private Context context;
private int[] images = {"R.drawable.img1","R.drawable.img2","R.drawable.img3","R.drawable.img4","R.drawable.img5","R.drawable.img6","R.drawable.img7"};
public SampleGridAdapter(Context context) {
super();
this.context = context;
}
@Override
public int getCount() {
return images.length;
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
View v = null;
ImageView coverImageView;
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.bookgrid, null);
coverImageView = (ImageView) v.findViewById(R.id.coverImageView);
coverImageView.setImageResource(images[position]);
coverImageView.setScaleType(ImageView.ScaleType.FIT_XY);
return v;
}
}
bookgrid.xml
<?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="match_parent"
android:background="@drawable/shelfimage" >
<ImageView
android:id="@+id/coverImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:paddingBottom="0dp"
android:paddingLeft="25dp"
android:paddingTop="6dp"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
I set the number of columns to 3 in the gridview declaration. Here I set the background in the xml file as android: background = "@ drawable / shelfimage". but he sets this background for each element separately ... I want to set one image as a background for each line (having 3 elements) ....
source
share