1

IT USED TO BE:

enter image description here

then i did't like the design of the GridView so i used the function from here, I followed kcoppock's answer and adjust a little bit, finally i somehow got this result

NOW

enter image description here

the layout totally covers the status bar!!! What the...

I thought the problem probably in the XML files but i just couldn't figure out.

fragment2.xml containing the GridView

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical" >

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:numColumns="2"
    android:verticalSpacing="1dp"
    android:horizontalSpacing="1dp"
    android:stretchMode="columnWidth"
    android:gravity="center"

    />
</RelativeLayout>

grid_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<my.com.impressor.demo2.SquareImageView
    android:id="@+id/picture"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="centerCrop"/>


</RelativeLayout>

Fragment2.java

public class Fragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment2, container, false);
    GridView gridView = (GridView) view.findViewById(R.id.gridview);
    gridView.setAdapter(new             ImageAdapter(getActivity().getApplicationContext()));

    //Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.p1);
    return view;
}
}

SquareImageView related to grid_item.xml

public class SquareImageView extends ImageView {
public SquareImageView(Context context) {
    super(context);
}

public SquareImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); //Snap to width
}
}

ImageAdapter.java

public class ImageAdapter extends BaseAdapter {
private final List<Item> mItems = new ArrayList<Item>();
private final LayoutInflater mInflater;

public ImageAdapter(Context context) {
    mInflater = LayoutInflater.from(context);

    /*mItems.add(new Item("Red",       R.drawable.t1));
    mItems.add(new Item("Magenta",   R.drawable.t2));
    mItems.add(new Item("Dark Gray", R.drawable.t3));
    mItems.add(new Item("Gray",      R.drawable.t4));
    mItems.add(new Item("Green",     R.drawable.t5));
    mItems.add(new Item("Cyan",      R.drawable.t6));*/  //end here

    mItems.add(new Item(R.drawable.t1));
    mItems.add(new Item(R.drawable.t2));
    mItems.add(new Item(R.drawable.t3));
    mItems.add(new Item(R.drawable.t4));
    mItems.add(new Item(R.drawable.t5));
    mItems.add(new Item(R.drawable.t6));
    mItems.add(new Item(R.drawable.t7));
    mItems.add(new Item(R.drawable.t8));
    mItems.add(new Item(R.drawable.t9));
    mItems.add(new Item(R.drawable.t10));
    mItems.add(new Item(R.drawable.t11));
    mItems.add(new Item(R.drawable.t12));
    mItems.add(new Item(R.drawable.t13));
    mItems.add(new Item(R.drawable.t14));
    mItems.add(new Item(R.drawable.t15));
    mItems.add(new Item(R.drawable.t16));
    mItems.add(new Item(R.drawable.t17));
    mItems.add(new Item(R.drawable.t18));
    mItems.add(new Item(R.drawable.t19));
    mItems.add(new Item(R.drawable.t20));
    mItems.add(new Item(R.drawable.t21));
    mItems.add(new Item(R.drawable.t22));
}

@Override
public int getCount() {
    return mItems.size();
}

@Override
public Item getItem(int i) {
    return mItems.get(i);
}

@Override
public long getItemId(int i) {
    return mItems.get(i).drawableId;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    View v = view;
    ImageView picture;
    //TextView name;

    if (v == null) {
        v = mInflater.inflate(R.layout.grid_item, viewGroup, false);
        v.setTag(R.id.picture, v.findViewById(R.id.picture));
        //v.setTag(R.id.text, v.findViewById(R.id.text));
    }

    picture = (ImageView) v.getTag(R.id.picture);
    //name = (TextView) v.getTag(R.id.text);

    Item item = getItem(i);

    picture.setImageResource(item.drawableId);
    //name.setText(item.name);

    return v;
}

private static class Item {
    //public final String name;
    public final int drawableId;

    Item(int drawableId) {
        //this.name = name;
        this.drawableId = drawableId;
    }
}
}
Community
  • 1
  • 1
plood
  • 25
  • 5

1 Answers1

0

You should use RecyclerView with GridLayoutManager instead GridView. That will solve all your problems and improve flexibility.

Pasha Shkaran
  • 1,433
  • 2
  • 23
  • 41
  • 1
    thank you for reply! sir i will try RecyclerView but i just don't understant why my result is different from [the answer](http://stackoverflow.com/questions/15261088/gridview-with-two-columns-and-auto-resized-images) since i have almost copied his code – plood Jan 22 '16 at 14:15
  • @plood here is http://www.vogella.com/tutorials/AndroidRecyclerView/article.html maybe will help you – Pasha Shkaran Jan 22 '16 at 14:16