mardi 4 août 2015

implementing onitemclicklistener with GridView

I have a grid view with custom adapter that contains an image and a TextView holder. The objective is to change color matrix of ImageView with onclick and then use an itemclicklistener to perform another action

here is the clicklistener in adapter

@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("http://ift.tt/wljqS4" + 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;
}

and here is the 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();

        }
    });

when I click the color matrix is applied but toast is not shown and when I comment the clicklistener in adapter the toast is shown. but I want the 2 click actions to be triggered when I click the item

Aucun commentaire:

Enregistrer un commentaire