长按删除

时间:2012-10-01 09:33:38

标签: android gridview long-press

我已经实现了我的代码,可以在长按gridview时删除和更改视图。这是我更新的代码。现在我有一个问题,当我长按然后我必须更改所有项目图像(我在长按时添加一个视图更多 - 即十字按钮)

点击该十字按钮,我想删除该项目。请帮帮我。

 public class Deletable extends Activity
{
    private LayoutInflater inflater = null;

    Boolean checkLong = false;

    Vector<Integer> mThumbsID =  new Vector<Integer>();


    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid_view);

        GridView gridview = (GridView) findViewById(R.id.gridview);
        final ImageAdapter adapter = new ImageAdapter(this);
        gridview.setAdapter(adapter);

        inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        mThumbsID.setSize(9);

        gridview.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View strings,
                    int position, long id) {
                // TODO Auto-generated method stub

                Toast.makeText(Deletable.this, "Deletable "+ position, Toast.LENGTH_SHORT).show();
                checkLong = true;
                //adapter.removeitem(position);
                adapter.notifyDataSetChanged();
                return false;

            }
        });


        /*gridview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub

                checkLong = false;
                adapter.notifyDataSetChanged();

            }
        });*/
    }

    public class ImageAdapter extends BaseAdapter
    {
        private Context mContext;

        public ImageAdapter(Context c)
        {
            mContext = c;
        }

        public int getCount()
        {

            return mThumbsID.size();
        }

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

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

        public void removeitem(int position){
            mThumbsID.remove(position);

        }

        // create a new ImageView for each item referenced by the Adapter
        public View getView(final int position, View convertView, ViewGroup parent)
        {
            View vi = convertView;
            ViewHolder holder =  new ViewHolder();

            if (vi == null) {
                vi = inflater.inflate(R.layout.custom_gridview, null);
            }

            holder.productImage = (ImageView)vi.findViewById(R.id.gv_item);
            holder.delete_icon = (ImageView)vi.findViewById(R.id.imageView1);

            holder.productImage.setImageResource(R.drawable.ic_launcher);


            if(checkLong==true){

                holder.delete_icon.setVisibility(View.VISIBLE);
                holder.delete_icon.setImageResource(R.drawable.cross);
              /*  holder.delete_icon.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub

                        mThumbsID.remove(position);

                    }
                });
*/
            }

            else{

                holder.delete_icon.setVisibility(View.INVISIBLE);

            }

            /*if (convertView == null)
            {
                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(R.drawable.ic_launcher);*/
            return vi;
        }


          class ViewHolder
            {
                //TextView tvBrandName;
                ImageView productImage,delete_icon;
            }



    }

1 个答案:

答案 0 :(得分:2)

您需要从基础适配器(mThumbIds)的数据集中删除相应的对象,然后调用adapter.notifyDataSetChanged()

您可以将数据存储在以下Vector中:

Vector<Integer> mThumbIds = new Vector<Integer>();

将其初始化为:

for (int i = 0; i < mThumbIds.size(); i++) {
            mThumbIds.add(R.drawable.ic_launcher);
        }

removeitem()

public void removeitem(int position){
    mThumbIds.remove(position);

    }

然后删除..

public boolean onItemLongClick(AdapterView<?> parent, View strings,
                int position, long id) {
            // TODO Auto-generated method stub

            Toast.makeText(Deletable.this, "Deletable "+ position, Toast.LENGTH_SHORT).show();
            adapter.removeitem(position);
            adapter.notifyDataSetChanged();
            return false;

        }