ListView, mark the position where the user touches

后端 未结 3 1109
谎友^
谎友^ 2021-01-17 00:53

I have a custom listview with a setOnTouchListener

view.setOnTouchListener(new OnTouchListener() {

        @Override
        publi         


        
相关标签:
3条回答
  • 2021-01-17 01:34

    If you want to highlight ListView item on click/touch than it's better to use selectors instead of overiding OnTouchListener().

    If you want to set the color, you need a StateListDrawable. You can set this on your list using the android:listSelector attribute, defining the drawable in XML:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:state_enabled="false" android:state_focused="true"
            android:drawable="@drawable/item_disabled" />
      <item android:state_pressed="true"
            android:drawable="@drawable/item_pressed" />
      <item android:state_focused="true"
            android:drawable="@drawable/item_focused" />
    </selector>
    

    Or you can use the same selector for item of the ListView.

    0 讨论(0)
  • 2021-01-17 01:37

    What is the view in your case? Is it an individual list item or main listview ? If it is a listview, then try to handle the case

    case MotionEvent.ACTION_MOVE : 
    //Check position here, if it is out of your view, then change the color back.
    
    0 讨论(0)
  • 2021-01-17 01:44

    In your case you need either ACTION_UP or ACTION_DOWN event not ACTION_MOVE so to avoid ACTION_MOVE you can do something like this:

    if(event.getAction() == MotionEvent.ACTION_DOWN)
        {
             isDown = false;            
        }
        if(event.getAction() == MotionEvent.ACTION_UP && !isDown)
        {
            // action you want to perform
        }
        if(event.getAction() == MotionEvent.ACTION_MOVE)
        {
            isDown = true;
        }
    

    as far as changing color is concerned, you can store previous view in a global variable and while going for next touch, you can change that global view color to normal.

    0 讨论(0)
提交回复
热议问题