SERG
SERG

Reputation: 3971

How to do RelativeLayout with images like gridview?

I neew something to display images like gridview in scrollview. As i read it is not right to use gridview in scrollview. But i need big image then text, then list ao all images and thet all in one fragment so i need scrollview, like the first one, but just images without any text enter image description here So is there a way to display images programmatically one after another in RelativeLayout(like in gridview)?

here is what i got, but it is not working, the images just overlay each other.

 for (String pic : mFlat.getAdv_pics()){
            i++;
            ImageView imageView = new ImageView(mActivity);
            imageView.setId(i);

            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
            params.addRule(RelativeLayout.RIGHT_OF, i-1);

            imageView.setLayoutParams(params);

            Log.d("!!!!", "" + i + "|" + imageView.getId());

            Log.d("!!!!", "http://****" + pic + "_small.jpg");
            relativeLayout.addView(imageView);
            Picasso.with(mActivity)
                    .load("http://****" + pic + "_small.jpg")
                    .resize(width,width)
                    .centerCrop()
                    .into(imageView);
        }
    }

thanks

Upvotes: 0

Views: 499

Answers (1)

Helmi
Helmi

Reputation: 556

There is a reason gridview exists , creating such layout is far more complex and way harder too handle (events ,positions..) using other views combo. You basically need to display a listview inside a realtivelayout, and the item of your listview will be a the row of your custom grid. for example you want to display 3 images on every row:

Your main xml will look like this:

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

    <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/listView"/>
</RelativeLayout>

and then your listview adapter item will be like this:

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

    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView1"/>
    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView2"/>

    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView3"/>

</RelativeLayout>

Now you need to pass a nested array to your adapter ,each item of array contain 3 images and so on.But the main problem is to handle events and this where you need to rethink about it.

Upvotes: 1

Related Questions