patternjavaMinor
Custom grid view with different columns
Viewed 0 times
gridcolumnswithviewcustomdifferent
Problem
I am developing a custom grid view with different columns.
ViewPossibilities 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 arr_string = new ArrayList();
@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
ViewPossibilities 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 arr_string = new ArrayList();
@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
Solution
Variables
These variables should be
XML
Considering the pattern of your XML files, you might want to create the layouts dynamically, with code, instead of using XML files.
I don't really see why you're wrapping all your inner LinearLayouts inside an outer LinearLayout in your
in those files without any difference in behavior.
I am not a big fan of your naming scheme for your android IDs, such as
If-else-if-else-if-else...
Your
I believe this part of your code can be simplified a lot by using some mathematics instead of hardcoding the system. I haven't found out the exact solution yet though. But I do believe that the
Edit:
Instead of for your whole sequence of if-else:
And then the code in your activity:
int width = 0;
int position = 0;
ArrayList arr_string = new ArrayList();These variables should be
private final and the ArrayList should be declared as List to allow easier switch to another List implementation.private final int width = 0;
private final int position = 0;
private final List arr_string = new ArrayList();XML
Considering the pattern of your XML files, you might want to create the layouts dynamically, with code, instead of using XML files.
I don't really see why you're wrapping all your inner LinearLayouts inside an outer LinearLayout in your
layout_one-three.xml I think that you can removein those files without any difference in behavior.
I am not a big fan of your naming scheme for your android IDs, such as
android:id="@+id/a22".If-else-if-else-if-else...
Your
if-else-if-else-if-else-if can be switched to a switch (size) statement which would make it a bit more clear what's going on there. Also, it is very easy to miss that the first if's don't use braces, which leads us to the common suggestion always use braces. If you switch to a switch, you won't use braces but break; instead.I believe this part of your code can be simplified a lot by using some mathematics instead of hardcoding the system. I haven't found out the exact solution yet though. But I do believe that the
if (size == 9) { specific case is useless as that is exactly what will happen with your last else-block, which btw can be a bit simplified:int mod = (size % 3 == 0 ? 0 : 1);
for (int i = 0; i < size / 3 + mod; i++) {
girdviewcustom.addView(getViewThree());
}Edit:
Instead of for your whole sequence of if-else:
public static int[] rows(int i) {
final int firstRow;
if (i == 1) {
firstRow = 1;
}
else firstRow = i % 3 == 0 ? 3 : 2;
final int secondRow;
if (i = 7 ? 3 : 0;
return new int[]{ firstRow, secondRow, thirdRow };
}
private void makeRow(int columns) {
switch (columns) {
case 1:
girdviewcustom.addView(getViewOne());
break;
case 2:
girdviewcustom.addView(getViewTwo());
break;
case 3:
girdviewcustom.addView(getViewThree());
break;
default: throw new IllegalArgumentException("Invalid count: " + columns);
}
}And then the code in your activity:
int size = arr_string.size();
int[] rows = rows(size);
for (int i : rows) {
if (i > 0) {
makeRow(i);
}
}Code Snippets
int width = 0;
int position = 0;
ArrayList<String> arr_string = new ArrayList<String>();private final int width = 0;
private final int position = 0;
private final List<String> arr_string = new ArrayList<String>();<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >int mod = (size % 3 == 0 ? 0 : 1);
for (int i = 0; i < size / 3 + mod; i++) {
girdviewcustom.addView(getViewThree());
}public static int[] rows(int i) {
final int firstRow;
if (i == 1) {
firstRow = 1;
}
else firstRow = i % 3 == 0 ? 3 : 2;
final int secondRow;
if (i <= 3) {
secondRow = 0;
}
else secondRow = i % 3 == 1 ? 2 : 3;
final int thirdRow = i >= 7 ? 3 : 0;
return new int[]{ firstRow, secondRow, thirdRow };
}
private void makeRow(int columns) {
switch (columns) {
case 1:
girdviewcustom.addView(getViewOne());
break;
case 2:
girdviewcustom.addView(getViewTwo());
break;
case 3:
girdviewcustom.addView(getViewThree());
break;
default: throw new IllegalArgumentException("Invalid count: " + columns);
}
}Context
StackExchange Code Review Q#56720, answer score: 6
Revisions (0)
No revisions yet.