在GridView中替换旧图像onTouch的新图像

时间:2014-08-22 08:38:04

标签: android gridview

我有一个自定义网格视图,其中是图像。因此,在触摸图像时,我想用新图像替换图像,以便用户可以知道他触摸了哪些图像。 我在触摸

的int item_position中获得了用户触摸的项目位置

我使用的代码

GridView gv1;
CustomList adapter;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
gv1 = (GridView) findViewById(R.id.gridView1);
adapter = new CustomList(this, s, images);
            gv1.setAdapter(adapter);
            gv1.setOnTouchListener(this);
}

@Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub

x = event.getX();
            y = event.getY();
            item_position = gv1.pointToPosition((int) x, (int) y);
View v1 = (View) gv1.getChildAt(item_position);
                Bitmap b = BitmapFactory.decodeResource(this.getResources(),
                        R.drawable.a1);
                v1.draw(new Canvas(b));

                adapter.notifyDataSetChanged();


}

public class CustomList extends ArrayAdapter<String>

{

    private final Activity context;
    private final String[] web;
    private final Integer[] imageId;

    public CustomList(Activity context, String[] web, Integer[] imageId) {
        super(context, R.layout.list_single, web);
        this.context = context;
        this.web = web;
        this.imageId = imageId;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.list_single, null, true);
        // LinearLayout imageView = (LinearLayout)
        // rowView.findViewById(R.id.img);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
        // TextView txt = (TextView) rowView.findViewById(R.id.txt);
        // txt.setText(web[position]);
        imageView.setImageResource(imageId[position]);
        // imageView.setBackgroundResource(imageId[position]);
        return rowView;
    }
}

0 个答案:

没有答案