getView从未调用过

时间:2014-05-26 12:12:52

标签: android gridview fragment custom-adapter

我正在实现一个网格视图,它是一个带有自定义适配器的片段。但是自定义适配器的getView()从未调用过。我设置了如下的网格视图。

public class MainFragment extends Fragment {
ArrayList<ObjectPack> objects;
GridView gridView;
ThumbnailGridViewAdapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    objects = new ArrayList<ObjectPack>();
    objects.add(createMockObjectPack());
    gridView = (GridView)getActivity().getLayoutInflater().inflate(R.layout.fragment_story_main, null).findViewById(R.id.gridView_Main);
    adapter = new ThumbnailGridViewAdapter(this.getActivity(), R.layout.item_thumbnail, objects);
    gridView.setAdapter(adapter);
}

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

    return view;
}

private ObjectPack createMockObjectPack() {
    ObjectPack mock;
    int id = 1;
    String title = "test";
    String description = "TEST";
    int iconId = R.drawable.title;
    ArrayList<Integer> imoticonIds = null;

    mock = new ObjectPack(id, title, description, iconId, imoticonIds);

    return mock;
}
}

接下来是片段的布局xml。文本视图显示良好,但网格视图不是。

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

<TextView
    android:id="@+id/textView_storyMain"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/message_storyMain" />

<GridView
    android:id="@+id/gridView_storyMain_theme"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:horizontalSpacing="10dp"
    android:numColumns="4"
    android:paddingBottom="10dp"
    android:paddingTop="10dp"
    android:verticalSpacing="10dp" >
</GridView>

</LinearLayout>

每当我尝试调试时,onCreate和onCreateView上的断点都会命中四次,为什么会这样?

请帮助我,任何建议都会受到很多赞赏!!!

================添加======================

下面是我的适配器代码,根据x90的建议,getView()被调用一次但屏幕现在全黑。我是android的新手,请帮助我,谢谢!

public class ThumbnailGridViewAdapter extends BaseAdapter {
Context context;
int itemLayoutId;
List<ObjectPack> objects;
LayoutInflater inflater;

BitmapLoader loader;

public ThumbnailGridViewAdapter(Context context, int itemLayoutId, List<ObjectPack> objects) {
    super();

    this.context = context;
    this.itemLayoutId = itemLayoutId;
    this.objects= objects;

    this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    loader = new BitmapLoader(context.getResources(), R.drawable.loading);
}

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

@Override
public Object getItem(int index) {
    return objects.get(index);
}

@Override
public long getItemId(int index) {
    return objects.get(index).getId();
}

@Override
public View getView(int index, View convertView, ViewGroup parent) {
    if(convertView == null) {
        convertView = inflater.inflate(itemLayoutId, parent, false);
    }
    else {
        ImageView imageView = (ImageView)convertView.findViewById(R.id.imageView_item_thumbnail);
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageView.setScaleType(ScaleType.FIT_XY);
        loader.LoadBitmap(objects.get(index).getIconId(), imageView);

        TextView text = (TextView)convertView.findViewById(R.id.textView_item_thumbnail);
        text.setText(objects.get(index).getTitle());
    }

    return convertView;
}

}

1 个答案:

答案 0 :(得分:2)

onCreateView中的


GridView gridView = (GridView)view.findViewById(R.id.gridView_storyMain_theme);
gridView.setAdapter(adapter);
adapter.notifyDataSetChanged();


并在onCreate方法中删除GridView创建。 如果您的适配器很好 - 将出现网格。