0

Im trying to follow this Gridview with Auto resize image example and tried it in a Fragment. Here is the code:

FragmentOne

public class FragmentOne extends Fragment{ GridView gridView;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
     View view = inflater.inflate(R.layout.fragment_one, container, false);

    gridView = (GridView) view.findViewById(R.id.gvList);
    gridView.setAdapter(new Top_MyAdapter(getActivity().getApplicationContext()));


    return view;
}


private class Top_MyAdapter extends BaseAdapter {
    private List<Item> items = new ArrayList<Item>();
    private LayoutInflater inflater;


    public Top_MyAdapter(Context c) {
        inflater = LayoutInflater.from(c);

        items.add(new Item("Image 1", R.drawable.image_placeone));
        items.add(new Item("Image 2", R.drawable.image_placetwo));
        items.add(new Item("Image 3", R.drawable.image_placethree));
        items.add(new Item("Image 4", R.drawable.image_placefour));
        items.add(new Item("Image 5", R.drawable.image_placefive));
    }

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

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return items.get(position).drawableId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        ImageView picture;
        TextView name;

        if(v == null){
            v = inflater.inflate(R.layout.gridviewitem, parent, false);
            v.setTag(R.id.gvItemImage, v.findViewById(R.id.gvItemImage));
            v.setTag(R.id.gvItemText, v.findViewById(R.id.gvItemText));
        }

        picture = (ImageView) v.getTag(R.id.gvItemImage);
        name = (TextView) v.getTag(R.id.gvItemText);

        Item item = (Item) getItem(position);

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

        return v;

    }
    private class Item
    {
        final String name;
        final int drawableId;

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

}

fragmentone

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

gridviewitem

<FrameLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<com.Sample.SquareImageView
    android:id="@+id/gvItemImage"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="centerCrop"
    />
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id = "@+id/gvItemText"
    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>

SquareImageView

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
}
 }

The problem here is that is is not displaying in my Fragment. There's no error it's just not displaying, and suddenly there is a scrollbar on the rightside ..

John Powell
  • 12,253
  • 6
  • 59
  • 67
VLDCNDN
  • 692
  • 1
  • 7
  • 19

1 Answers1

0

There may be problem in getView method. It looks like a false implementation of view holder pattern.

We can try without pattern.

Can you try this for being sure to identify problem.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    convertView = inflater.inflate(R.layout.gridviewitem, parent, false);

    ImageView image = (ImageView) convertView.findViewById(R.id.gvItemImage);
    TextView text = (TextView) convertView.findViewById(R.id.gvItemText);

    image.setImageResource(getItem(position).drawableId);
    text.setText(getItem(position).name);

    return convertView;
}


static class Item
{
    final String name;
    final int drawableId;

    Item(String name, int drawableId)
    {
        this.name = name;
        this.drawableId = drawableId;
    }
}
Emre Aktürk
  • 3,306
  • 2
  • 18
  • 30
  • image.setImageResource(getItem(position).drawableId); text.setText(getItem(position).name); The .drawableId and .name is Cannot resolve Symbol – VLDCNDN Aug 19 '14 at 06:39
  • there's still an error, it says **Inner classes cannot have static declarations** – VLDCNDN Aug 19 '14 at 06:57