Appearance of the Listier list item selector (the pressing state was not deleted when the finger left the screen)

I have a listview element as below

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:orientation="horizontal"
    android:descendantFocusability="blocksDescendants"
    >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/music_list_item_checkbox_bg"
        />
</LinearLayout>

music_list_item_checkbox_bg.xml - This is a selector that will display various available depending on another state.

<selector xmlns:android="http://schemas.android.com/apk/res/android" >

    <item 
          android:state_pressed="false"
          android:state_selected="true"
          android:drawable="@drawable/btn_checkbox_check_untapped" />

    <item android:state_selected="true" 
          android:state_pressed="true"
          android:drawable="@drawable/btn_checkbox_check_tapped" />

    <item android:state_selected="false"  
          android:state_pressed="false"
          android:drawable="@drawable/btn_checkbox_uncheck_untapped" />

    <item android:state_selected="false"  
          android:state_pressed="true"
          android:drawable="@drawable/btn_checkbox_uncheck_tapped" />

</selector>

When I clicked an item in the list and left the touch screen inside the item, then depending on the other state, you can use it.

Question . But if I pressed an element and pulled my finger horizontally out of the object (the touch point X is between the upper and lower parts of the element), then exit the screen, the ejected flag will remain in the pressed state. (If the touch point X is outside the top and bottom of the element, the state is correct).

android:duplicateParentState="true" , , .

, - ?

Edited

onTouch onIntercept , , super.onTouch( MotionEvent). true, , onItemClick . onTouch AbsListview, , , , child (item) false, child.setPressed(false), .

!!!

+3
5

, , , .

MyListView, ListView SDK ListView ACTION_UP ACTION_CANCEL onTouchEvent, false , . .

@Override
public boolean onTouchEvent(MotionEvent event)
{
    boolean handled = super.onTouchEvent(event);
    Log.d("test", "event " + event.toString());
    int action = event.getAction();

    switch (action)
    {
    case MotionEvent.ACTION_DOWN:
        m_downPosition = pointToPosition((int)event.getX(), (int)event.getY());
        Log.d("test", "The down position " + m_downPosition);
        break;

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        int position = m_downPosition - getFirstVisiblePosition();
        View child = INVALID_POSITION != m_downPosition ? getChildAt(position) : null;
        if (null != child)
        {
            child.setPressed(false);
        }
        break;

    default:
        break;
    }
    return handled;
}
+1
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" 
         android:drawable="@drawable/btn_checkbox_check_tapped" />
    <item android:state_selected="true" 
         android:drawable="@drawable/btn_checkbox_check_tapped" />
        <item android:drawable="@drawable/btn_checkbox_uncheck_untapped" />
</selector>

+2

...

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/music_list_item_checkbox_bg"
android:focussableInTouchMode="false"
android:focussable="false"
    />

... , checkBox , ...

+1

,

<item android:drawable="@drawable/btn_checkbox_uncheck_untapped" />

, focussed, xml.

0

Extend LinearLayoutfor your element and override onTouchin it. Handle the ACTION_CANCEL event (return true) and update the pressed and selected states.

class ItemLayout extends LinearLayout {

    @override
    boolean onTouch(MotionEvent ev) {
        switch(ev.getAction()) {
            case ACTION_CANCEL: 
                // Update/reset pressed and selected states of item or checbkox
                return true;
        }
        return super.onTouch(ev);
    }

}

Contact http://groups.google.com/group/android-developers/browse_thread/thread/df75c5b8e4605167#

Hope this helps.

<ItemLayout 
  ....>
    <TextView
       ...
       />

    <CheckBox
       ...
       />
</ItemLayout >
0
source

All Articles