getView永远不会被称为网格视图适配器,即使我的getCount返回非null值,

时间:2016-02-28 14:08:34

标签: android android-fragments gridview android-arrayadapter baseadapter

getView永远不会被调用,即使getCount返回非空值(因为我为了试用而硬编码)但仍然没有运气,其他每个答案都说getCount可能返回0但是还有其他原因吗可能不会调用getView。

这是gridviewadapter:

public class EmoticonsAdapter extends BaseAdapter {
    private Context context;
    private int layoutResourceId;
    private ArrayList<String> data;

    public EmoticonsAdapter(Context context, int layoutResourceId, ArrayList<String> data) {

        this.context = context;
        this.layoutResourceId = layoutResourceId;
        this.data = data;
    }


    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        View row = view;


        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(R.layout.emoticons_item, viewGroup, false);
        final String path = data.get(position);

        ImageView image = (ImageView) row.findViewById(R.id.item);
        image.setImageBitmap(getImage(path));


        return row;
    }

    @Override
    public int getCount() {
        int size = data.size();
        return 54;
    }

    @Override
    public String getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    private Bitmap getImage(String path) {
        AssetManager mngr = context.getAssets();
        InputStream in = null;

        try {
            in = mngr.open("emoticons/" + path);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //BitmapFactory.Options options = new BitmapFactory.Options();
        //options.inSampleSize = chunks;

        Bitmap temp = BitmapFactory.decodeStream(in, null, null);
        return temp;
    }

}

我在onAttach方法中从片段中实例化它。也尝试使用onCreateView但不起作用。

public class FragmentTab extends Fragment {

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

        setupstuff();

        return v;


    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        //setupstuff();
    }


    void setupstuff() {
        Activity activity = getActivity();
        LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(
                R.layout.fragment_one, null);

        GridView grid = (GridView) layout.findViewById(R.id.emoticons_grid);

        ArrayList<String> paths = new ArrayList<String>();

        for (short i = 1; i <= 54; i++) {
            paths.add(i + ".png");
        }

        EmoticonsAdapter adapter = new EmoticonsAdapter(
                getActivity().getBaseContext(), R.layout.emoticons_item, paths);
        grid.setAdapter(adapter);


    }

}


The emoticons_item has a image view with it "item".

1 个答案:

答案 0 :(得分:0)

问题出在这里

void setupstuff() {
    Activity activity = getActivity();
    LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(
            R.layout.fragment_one, null);

您正在为新视图充气,而您在onCreateView内返回的GridView与夸大的视图不同。

更改设置

void setupstuff(View layout) {
    GridView grid = (GridView) layout.findViewById(R.id.emoticons_grid);
    ArrayList<String> paths = new ArrayList<String>();

    for (short i = 1; i <= 54; i++) {
        paths.add(i + ".png");
    }

    EmoticonsAdapter adapter = new EmoticonsAdapter(
            getActivity().getBaseContext(), R.layout.emoticons_item, paths);
    grid.setAdapter(adapter);
}

并从onCreateView调用它:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_one, container, false);
    setupstuff(v);
    return v;
}