如何从片段中检索gridview中的图像?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何从片段中检索gridview中的图像?相关的知识,希望对你有一定的参考价值。

我只是从drawable检索图像并使用base adapterfragment没有显示任何image。以下是我的代码......有任何问题或逻辑错误......有人能告诉我!!!这是片段代码..

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_all_question, container, false);
    GridView gridview = (GridView) view.findViewById(R.id.gridview);
    imageAdapter = new ImageAdapter(mContext);
    gridview.setAdapter(imageAdapter);

    gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getActivity(), "" + position,
                    Toast.LENGTH_SHORT).show();
        }
    });

    return view;
}

这是适配器代码

public class ImageAdapter extends BaseAdapter{

    private Context mContext;

    public ImageAdapter(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return mThumbIds.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            // if it's not recycled, initialize some attributes

            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }
    // references to our images
    private Integer[] mThumbIds = {
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7
    };
} 
答案

尝试将适配器中的getItem(..)方法更改为此方法并查看其进展情况。

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

以上是关于如何从片段中检索gridview中的图像?的主要内容,如果未能解决你的问题,请参考以下文章

片段中gridview的Android文本和图像适配器

如何在Android中加载带有动画的cardview GridView?

如何从Android片段中的相机获取图像

从适配器中的片段调用元素

从图库中获取图像以在片段中的图像视图中设置? [复制]

从 ViewPager 中检索片段