Gridview不以片段形式显示

时间:2014-08-19 04:45:10

标签: android gridview android-fragments android-gridview

我试图按照这个Gridview with Auto resize image示例并在片段中尝试它。 这是代码:

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

这里的问题是我的片段中没有显示。没有错误,它只是没有显示,突然右侧有一个滚动条..

1 个答案:

答案 0 :(得分:0)

getView方法可能存在问题。它看起来像是视图持有者模式的错误实现。

我们可以尝试没有模式。

你能否尝试确定问题。

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