如何创建自定义GridView

时间:2017-05-01 18:50:07

标签: android gridview android-volley baseadapter

我一直在尝试制作自定义gridview,这样我就可以操纵单个网格的外观,但我还没有成功。

我能够创建一个gridview,它可以使用齐射来获取图像和文本。和 我一直试图操纵适配器类并搜索教程,但遗憾的是没有运气。

你能帮助我,我怎么能做到这一点,我做错了什么?提前致谢!

这是我的适配器类GridViewAdaper

public class GridViewAdapter extends BaseAdapter {

    private ImageLoader imageLoader;


    private Context context;

     private ArrayList<String> images;
    private ArrayList<String> names;

    public GridViewAdapter (Context context, ArrayList<String> images, ArrayList<String> names){
        this.context = context;
        this.images = images;
        this.names = names;
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater= (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView=inflater.inflate(R.layout.list_item, null);

        TextView name=(TextView)convertView.findViewById(R.id.textViewName);
        ImageView icon=(NetworkImageView)convertView.findViewById(R.id.imageViewHero);

        imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
        imageLoader.get(images.get(position), ImageLoader.getImageListener(icon, R.mipmap.ic_launcher, android.R.drawable.ic_dialog_alert));

        icon.setImageResource(images.get(position),imageLoader);

        name.setText(names.get(position));

       icon.setScaleType(ImageView.ScaleType.CENTER_CROP);
        icon.setLayoutParams(new GridView.LayoutParams(200,200));

        return convertView;
    }
}

1 个答案:

答案 0 :(得分:0)

我已经解决了问题:D

适配器类:

public class GridViewAdapter extends BaseAdapter {

private ImageLoader imageLoader;

private Context context;

private ArrayList<String> images;
private ArrayList<String> names;

 public GridViewAdapter (Context context,ArrayList<String> images, 
 ArrayList<String> names){
    this.context = context;
    this.images = images;
    this.names = names;
}

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

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View grid;
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    grid = inflater.inflate(R.layout.list_item, null);

    TextView title = (TextView) grid.findViewById(R.id.textViewName);
    NetworkImageView thumbNail = (NetworkImageView) grid.findViewById(R.id.imageViewHero);

    imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
    imageLoader.get(images.get(position), ImageLoader.getImageListener(thumbNail, R.mipmap.ic_launcher, android.R.drawable.ic_dialog_alert));

    title.setText(names.get(position));
    thumbNail.setImageUrl(images.get(position), imageLoader);


    return grid;
}
}