Sougata Mandal February 2016

onTouchListener not working with custom view

I have a custom adapter class which extends base adapter. I'm trying to implement onTouchListener on view items. But it is not working. Please help.

Here is the code segment

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View cellView=convertView;
    ViewHolder viewHolder;

    if(cellView==null) {

        LayoutInflater layoutInflater = (LayoutInflater) context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        cellView = layoutInflater.inflate(R.layout.grid_topics, parent, false);
        viewHolder = new ViewHolder();
        viewHolder.icon = (ImageView) cellView.findViewById(R.id.cellImageView);
        viewHolder.text = (TextView) cellView.findViewById(R.id.cellTextView);
        cellView.setTag(viewHolder);

    } else {
        viewHolder = (ViewHolder) cellView.getTag();
    }

    viewHolder.icon.setImageBitmap(list.get(position).image);
    viewHolder.text.setText(list.get(position).text);

    cellView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_UP) {
                Toast.makeText(context, list.get(position).text, Toast.LENGTH_SHORT).show();
                return true;
            }

            return false;
        }
    });
    return cellView;
}

Answers


Georgi Koemdzhiev February 2016

Try to follow this approach when dealing with BaseAdapter. See how I am implementing the onTouch listener there in the if statement. That should work.

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;

    LayoutInflater mInflater = (LayoutInflater) 
        context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_item, null);
        holder = new ViewHolder();
        holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
        holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
        holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
        convertView.setTag(holder);
        convertView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_UP) {
            Toast.makeText(context, list.get(position).text, Toast.LENGTH_SHORT).show();
            return true;
        }

        return false;
    }
});
    }
    else {
        holder = (ViewHolder) convertView.getTag();
    }

    RowItem rowItem = (RowItem) getItem(position);

    holder.txtDesc.setText(rowItem.getDesc());
    holder.txtTitle.setText(rowItem.getTitle());
    holder.imageView.setImageResource(rowItem.getImageId());

    return convertView;
}

Post Status

Asked in February 2016
Viewed 3,595 times
Voted 11
Answered 1 times

Search




Leave an answer