문제

I'm trying to make a custom list. In the list adapter, I've implemented this code in the getView(..) method:

        final RelativeLayout layout = (RelativeLayout) row.findViewById(R.id.layout_main);

        layout.setOnTouchListener(new View.OnTouchListener()
        {
            @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                switch (event.getAction())
                    {
                        case MotionEvent.ACTION_DOWN:
                        {
                            layout.setBackgroundColor(context.getResources().getColor(R.color.asia_red_color));
                            return true;
                        }

                        case MotionEvent.ACTION_CANCEL:
                        case MotionEvent.ACTION_UP:
                        {
                            layout.setBackgroundColor(context.getResources().getColor(R.color.white));
                            return true;
                        }
                    }

                return false;
            }
        });

Note that the listener that I've implemented prevents/overrides executing the onItemClickListener that I've implemented in MainActivity.

Any solutions?

도움이 되었습니까?

해결책

Have just OnItemClickListener

Define a selector

bkg.xml in drawable folder

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/white" />
<item android:drawable="@color/yellow" />
</selector>

set the selector to listview. To the custom layout android:background="@drawable/bkg"

Define colors in color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="white">#FFFFFF</color>
    <color name="yellow">#FFFF00</color>
</resources>

Also check if your lsitview row items has buttons. When you click the button may take focus.

다른 팁

I'm not sure what is happening here, but you can try to invoke the OnTouchListener of the super class in the default branch of the switch, something like this:

layout.setOnTouchListener(new View.OnTouchListener()
    {
        @Override
        public boolean onTouch(View v, MotionEvent event)
        {
            switch (event.getAction())
                {
                    case MotionEvent.ACTION_DOWN:
                    {
                        layout.setBackgroundColor(context.getResources().getColor(R.color.asia_red_color));
                        return true;
                    }

                    case MotionEvent.ACTION_CANCEL:
                    case MotionEvent.ACTION_UP:
                    {
                        layout.setBackgroundColor(context.getResources().getColor(R.color.white));
                        return true;
                    }
                    default: return super.OnTouchListener();
                }

        }
    });

Pardon me if the syntax is incorrect, since I am on just the browser at the moment, but you get the idea.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top