user467871
user467871

Reputation:

how to change the background color of row onTouch but not onScroll?

I have a listview and I just want to change the background color of row(item) when touch it (MotionEvent.ACTION_DOWN) but prevent this while scrolling the listview. How can I do this ?

Here is my BaseAdapter for ListView

 @Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View row = convertView;
    DataHolder holder = null;

    if (row == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new DataHolder();
        holder.newAppsLayout = (LinearLayout) row.findViewById(R.id.new_apps_layout);             
        holder.imageView1 = (ImageView) row.findViewById(R.id.imageView1);             
        row.setTag(holder);
    } else {
        holder = (DataHolder) row.getTag();
    }

    Data data = data.get(position);
    ImageCacheManager.loadImage(holder.imageView1, item.getIconUrl());

    final DataHolder finalHolder = holder;
    row.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
                  if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    finalHolder.newAppsLayout.setBackgroundColor(Color.GRAY);
                } else {   
                 finalHolder.newAppsLayout.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.content_bg));                    }

            return false;
        }
    });
    return row;


static class DataHolder {
    LinearLayout newAppsLayout;
    ImageView imageView1;
 }

It works but also works while touch for scrolling. How can prevent this ? I mean, works for only touch event but not on scrolling ?

Thanks in advance

Upvotes: 0

Views: 668

Answers (1)

Dirk
Dirk

Reputation: 608

Anytime you touch the screen you get a MotionEvent.ACTION_DOWN. One way to prevent the color from changing for scroll events is to implement a SimpleOnGestureListener that will return the color to its original state if a scroll gesture is detected.

simpleOnGestureListener = new SimpleOnGestureListener(){    
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY){
            //put color code here
            return super.onScroll(e1, e2, distanceX, distanceY);
        }
    };

You must have a GestureDetector instantiated to pay attention to the simpleOnGestureListener .I usually have a TouchSetup() method that creates the simpleOnGestureListener and then assigned mGestureDetector.

mGestureDetector = new GestureDetector(context, simpleOnGestureListener);

Finally, at the end of your OnTouchEvent(MotionEvent ev) function call

return mGestureDetector.onTouchEvent(event);

All of this requires you to change your onTouchListener to a GestureDetector, but you can still pull out any info you need about the touch events from the MotionEvent. In addition the GestureDetector lets you easily detect fling or double taps and respond in whatever way you deem appropriate.

http://developer.android.com/reference/android/view/GestureDetector.SimpleOnGestureListener.html

Upvotes: 1

Related Questions