-2

I followed the steps in the accepted answer (from @kcoppock) in the following thread. https://stackoverflow.com/a/15264039/3296263 or Gridview with two columns and auto resized images

Edit: Here is the code. Gridview code:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
        <GridView 
        android:id="@+id/main_category_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:verticalSpacing="0dp"
        android:horizontalSpacing="0dp"
        android:stretchMode="columnWidth"
        android:numColumns="2"/>
</FrameLayout>

Grid Item Layout: main_category_list_content.xml

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

<com.praz.notespal.model.SquareImageView
    android:id="@+id/picture"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="centerCrop"/>

<TextView
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="15dp"
    android:paddingBottom="15dp"
    android:layout_gravity="bottom"
    android:textColor="@android:color/white"
    android:background="#55000000"/>

</FrameLayout>

Custom SquareImageView Class:

 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()); 
    }
}

Adapter Code:

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

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

        mItems.add(new Item("Red",       R.drawable.tile_alevel));
        mItems.add(new Item("Magenta",   R.drawable.tile_grade10));
        mItems.add(new Item("Dark Gray", R.drawable.tile_grade11));
        mItems.add(new Item("Gray",      R.drawable.tile_grade9));
        mItems.add(new Item("Green",     R.drawable.tile_alevel));
        mItems.add(new Item("Cyan",      R.drawable.tile_grade11));
    }

    @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;
        System.out.println("viewing "+ i);

        if (v == null) {
            v = mInflater.inflate(R.layout.main_category_list_content, 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 class Item {
        public final String name;
        public final int drawableId;

        Item(String name, int drawableId) {
            this.name = name;
            this.drawableId = drawableId;
        }
    }
}

Activity Code where i set the adapter

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    GridView gridView = (GridView)findViewById(R.id.main_category_list);
    gridView.setAdapter(new MyAdapter(this));
}

Instead of showing all 6 items, i only see 2. If i increase the 'numColumns' property to 3, then it shows 3. So it only shows the first row. I placed a console out inside the getView() method in the adapter to see the position of the grid that is being called. To my surprise, what i see is the below output.

I/System.out: viewing item 0
I/System.out: viewing item 1
I/System.out: viewing item 0
I/System.out: viewing item 0
I/System.out: viewing item 0
I/System.out: viewing item 0

As you can see, it repeats 0th position instead of moving to 3rd. After alot of searching I still have not been able to figure out what is going on. Any help is highly appreciated.

Please note that i have created a new question, because i cannot reply to his answer because of reputation points shortage.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
PraZ
  • 57
  • 1
  • 8
  • We need to see your code, not just the example you're following. Please [edit] your question to provide a [mcve]. – Mike M. Mar 03 '18 at 08:56
  • Added the code. Please check now – PraZ Mar 03 '18 at 09:40
  • yup, this strange, item is different, but int position(i) back to 0 – Iqbal Rizky Mar 03 '18 at 09:54
  • `private final List mItems = new ArrayList();` if this line change w/o final `private List mItems = new ArrayList();` still have same result? – Iqbal Rizky Mar 03 '18 at 09:58
  • Yeah, removing the 'final' did not change anything – PraZ Mar 03 '18 at 10:29
  • I dropped your code into an empty project, and it works exactly as expected for me. You might try, though, changing the `layout_height` on the `FrameLayout` in `main_category_list_content` to `wrap_content`. – Mike M. Mar 03 '18 at 12:10

1 Answers1

2

I figured out what was wrong (by accident of course). The problem is that My GridView was inside a NestedScrolView. For some reason these 2 do not work well together. The moment I take the GridView out of the NestedScrolView, it works like a charm. I did some further searching on what causes this behavior, but i could not find any lead. Anyway, I don't really want a NestedScrolView, so its all good now. Thanks for everyone who replied. If antyone knows the reason for the above behavior, please explain.

PraZ
  • 57
  • 1
  • 8