使用GridView实现onitemclicklistener

时间:2015-08-04 10:57:40

标签: android gridview onitemclicklistener

我有一个带有自定义适配器的网格视图,其中包含一个图像和一个TextView持有者。 目标是使用ImageView更改onclick的颜色矩阵,然后使用 itemclicklistener 执行其他操作

这是适配器中的clicklistener

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if(convertView == null){
        vi = inflater.inflate(R.layout.pages_grid,  null);

        if (imageLoader == null)
            imageLoader = AppController.getInstance().getImageLoader();

        final NetworkImageView page_pic = (NetworkImageView) vi.findViewById(R.id.grid_icon);
        TextView page_name = (TextView) vi.findViewById(R.id.page_selected);
        final RelativeLayout rl = (RelativeLayout) vi.findViewById(R.id.selected_bar);
        LinearLayout container = (LinearLayout) vi.findViewById(R.id.page_container);
         ColorMatrix matrix = new ColorMatrix();
            matrix.setSaturation(0);

            final ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
            page_pic.setColorFilter(filter);    
        HashMap<String, String> pages = getItem(position);

        page_pic.setImageUrl("https://graph.facebook.com/" + pages.get("page_id") + "/picture", imageLoader);
        page_name.setText(pages.get("page_name"));
        container.setOnClickListener(new OnClickListener() {
            private boolean isSelected = false;
            @Override
            public void onClick(View v) {
                if(isSelected == false){
                    page_pic.setColorFilter(null);
                    rl.setBackgroundColor(context.getResources().getColor(R.color.cyan));
                    isSelected = true;
                }else{
                    page_pic.setColorFilter(filter);
                    rl.setBackgroundColor(Color.parseColor("#f0CCCCCC"));
                    isSelected = false;
                }

            }
        });
    }
    return vi;
}

,这是on itemclicklistener

gv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {


            String page_id = data.get(position).get("page_id");
            if(pages.contains(page_id))
                {
                pages.remove(page_id);
                }else{              
            pages.add(page_id);
                }
            Toast.makeText(getApplicationContext(),  "" + pages.toString(), Toast.LENGTH_SHORT).show();

        }
    });

当我点击颜色矩阵时,但是没有显示toast,当我在适配器中注释clicklistener时,会显示toast。但是我希望在单击项目

时触发2次单击操作

0 个答案:

没有答案