I am developing a custom grid view with different columns.
EDIT: Here View Possibilities Mockup
Here is the full demo. You can directly use it, with Internet uses permission.
This works perfectly but I want to optimize it.
MainActivity.java
package com.example.rtrt;
import java.util.ArrayList;
import com.squareup.picasso.Picasso;
import android.os.Bundle;
import android.app.Activity;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
int width = 0;
int position = 0;
ArrayList<String> arr_string = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout girdviewcustom = (LinearLayout) findViewById(R.id.girdviewcustom);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
width = displaymetrics.widthPixels;
arr_string
.add("http://www.bluesodapromo.com/static/DesignCenter/Image/dc_images/clipart/nature/nature_1.png");
arr_string
.add("http://www.bluesodapromo.com/static/DesignCenter/Image/dc_images/clipart/nature/nature_1.png");
arr_string
.add("http://www.bluesodapromo.com/static/DesignCenter/Image/dc_images/clipart/nature/nature_1.png");
arr_string
.add("http://www.bluesodapromo.com/static/DesignCenter/Image/dc_images/clipart/nature/nature_1.png");
arr_string
.add("http://www.bluesodapromo.com/static/DesignCenter/Image/dc_images/clipart/nature/nature_1.png");
arr_string
.add("http://www.bluesodapromo.com/static/DesignCenter/Image/dc_images/clipart/nature/nature_1.png");
arr_string
.add("http://www.bluesodapromo.com/static/DesignCenter/Image/dc_images/clipart/nature/nature_1.png");
arr_string
.add("http://www.bluesodapromo.com/static/DesignCenter/Image/dc_images/clipart/nature/nature_1.png");
arr_string
.add("http://www.bluesodapromo.com/static/DesignCenter/Image/dc_images/clipart/nature/nature_1.png");
arr_string
.add("http://www.bluesodapromo.com/static/DesignCenter/Image/dc_images/clipart/nature/nature_1.png");
arr_string
.add("http://www.bluesodapromo.com/static/DesignCenter/Image/dc_images/clipart/nature/nature_1.png");
int size = arr_string.size();
if (size == 1)
girdviewcustom.addView(getViewone());
else if (size == 2)
girdviewcustom.addView(getViewTwo());
else if (size == 3)
girdviewcustom.addView(getViewThree());
else if (size == 4) {
girdviewcustom.addView(getViewTwo());
girdviewcustom.addView(getViewTwo());
} else if (size == 5) {
girdviewcustom.addView(getViewTwo());
girdviewcustom.addView(getViewThree());
} else if (size == 6) {
girdviewcustom.addView(getViewThree());
girdviewcustom.addView(getViewThree());
} else if (size == 7) {
girdviewcustom.addView(getViewTwo());
girdviewcustom.addView(getViewTwo());
girdviewcustom.addView(getViewThree());
} else if (size == 8) {
girdviewcustom.addView(getViewTwo());
girdviewcustom.addView(getViewThree());
girdviewcustom.addView(getViewThree());
} else if (size == 9) {
girdviewcustom.addView(getViewThree());
girdviewcustom.addView(getViewThree());
girdviewcustom.addView(getViewThree());
} else {
int mod = (size % 3);
if (mod != 0)
mod = 1;
for (int i = 0; i < ((size / 3) + (mod)); i++) {
girdviewcustom.addView(getViewThree());
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public View getViewone() {
LayoutInflater inflater = getLayoutInflater();
View myView = inflater.inflate(R.layout.layout_one, null);
ImageView a11 = (ImageView) myView.findViewById(R.id.a11);
Picasso.with(MainActivity.this).load(arr_string.get(position))
.resize(width, width).into(a11);
position++;
return myView;
}
public View getViewTwo() {
LayoutInflater inflater = getLayoutInflater();
View myView = inflater.inflate(R.layout.layout_two, null);
ImageView a21 = (ImageView) myView.findViewById(R.id.a21);
ImageView a22 = (ImageView) myView.findViewById(R.id.a22);
Picasso.with(MainActivity.this).load(arr_string.get(position))
.resize(width / 2, width / 2).into(a21);
position++;
Picasso.with(MainActivity.this).load(arr_string.get(position))
.resize(width / 2, width / 2).into(a22);
position++;
return myView;
}
public View getViewThree() {
LayoutInflater inflater = getLayoutInflater();
View myView = inflater.inflate(R.layout.layout_three, null);
ImageView a31 = (ImageView) myView.findViewById(R.id.a31);
ImageView a32 = (ImageView) myView.findViewById(R.id.a32);
ImageView a33 = (ImageView) myView.findViewById(R.id.a33);
Picasso.with(MainActivity.this).load(arr_string.get(position))
.resize(width / 3, width / 3).into(a31);
position++;
if (arr_string.size() > position) {
Picasso.with(MainActivity.this).load(arr_string.get(position))
.resize(width / 3, width / 3).into(a32);
position++;
if (arr_string.size() > position) {
Picasso.with(MainActivity.this).load(arr_string.get(position))
.resize(width / 3, width / 3).into(a33);
position++;
}
}
return myView;
}
}
activity_main.xml
<RelativeLayout 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"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/girdviewcustom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
layout_one.xml
<?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:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/a11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="centerCrop" />
</LinearLayout>
</LinearLayout>
layout_two.xml
<?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:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/a21"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="centerCrop" />
<ImageView
android:id="@+id/a22"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="centerCrop" />
</LinearLayout>
</LinearLayout>
layout_three.xml
<?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:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/a31"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="centerCrop" />
<ImageView
android:id="@+id/a32"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="centerCrop" />
<ImageView
android:id="@+id/a33"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="centerCrop" />
</LinearLayout>
</LinearLayout>
Picasso
class, whatever that is. And why is Internet permissions that important for this code? – Simon André Forsberg Jul 11 at 11:20