0

我在片段中有gridView

     @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        //
        Bundle savedInstanceState) {




    if (container == null) {
        Log.e("Null container", "Null");

        return null;
    }
    Log.e("fragment1", "fragment1 reached!");
    View view =  inflater.inflate(R.layout.menu1_fragment, container, false);
    GridView gridView = (GridView)view.findViewById(R.id.gridview1);
    gridView.setAdapter(new ImageAdapter(this.getActivity()));


    Log.e("fragment1", "fragment1 reached!");

    return (GridView)inflater.inflate(R.layout.menu1_fragment, container, false);
}

但是适配器不起作用,它没有达到覆盖的 getView 方法。

构造函数:

public ImageAdapter(Context c) {
    mContext = c;
    Log.e("ImageAdapter", "constructor");
}

获取视图方法:

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
    Log.e("ImageAdapter", "dsfdsf");
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        if (DefinedValues.width/3 < DefinedValues.height/4)
        imageView.setLayoutParams(new GridView.LayoutParams
                (DefinedValues.height/4, DefinedValues.height/4));
        else
            imageView.setLayoutParams(new GridView.LayoutParams
                    (DefinedValues.width/3-5, DefinedValues.width/3-5));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}
4

2 回答 2

0

在 中onCreateView,您将 xml 膨胀两次,一次正在编辑,然后返回未编辑的 ..

public View onCreateView(LayoutInflater inflater, ViewGroup container,
    //
    Bundle savedInstanceState) {




if (container == null) {
    Log.e("Null container", "Null");

    return null;
}
Log.e("fragment1", "fragment1 reached!");
View view =  inflater.inflate(R.layout.menu1_fragment, container, false);
GridView gridView = (GridView)view.findViewById(R.id.gridview1);
gridView.setAdapter(new ImageAdapter(this.getActivity()));


Log.e("fragment1", "fragment1 reached!");

return view;
}
于 2012-12-11T11:20:40.170 回答
-1

我想你需要变成

public ImageAdapter(Context c) {
    this.mContext = c;
    Log.e("ImageAdapter", "constructor");
}

检查这个。

于 2012-12-11T11:22:19.853 回答