1

Sorry for my english. I spend many times but i cant fix my problem. I have listView and i want set setOnItemClickListener him. This is my listView:

 <ListView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id = "@+id/listScene"
                    android:clickable="true"
                    android:cacheColorHint="#00000000"
                    android:divider="#adb8c2"
                    android:dividerHeight="1dp"
                    android:scrollingCache="false"
                    android:smoothScrollbar="true"
                    >
                </ListView> 

Its my code

 public void outputListView(ArrayList<String> list) {
        Adapter adapter = new Adapter(getApplicationContext(), R.layout.item, list);
        adapter.notifyDataSetChanged();
        listView.setAdapter(adapter);

        listView.setItemsCanFocus(false);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Toast.makeText(getApplicationContext(), String.valueOf(position), Toast.LENGTH_SHORT).show();
            }
        });
    }

but when i click in item its nothing happens

UPD

My item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:focusable="false"
    android:focusableInTouchMode="false"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <service.SwipeLayout
        android:layout_marginTop="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:orientation="horizontal"
                android:background="#32777777"
                android:layout_width="match_parent"
                android:layout_height="70dp">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#fff"
                    android:layout_weight="0.7"
                    >

                    <TextView
                        android:id="@+id/conditions"
                        android:layout_marginTop="10dp"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="22dp"
                        android:text=""
                        android:textColor="#25ac36"
                        android:layout_centerVertical="true"
                        android:layout_centerHorizontal="true" />

                </RelativeLayout>

                <RelativeLayout
                    android:layout_weight="0.3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <TextView
                        android:id="@+id/description"
                        android:layout_marginLeft="10dp"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="18dp"
                        android:text=""
                        android:textColor="#222222"
                        android:layout_centerVertical="true"
                        />
                </RelativeLayout>

            </LinearLayout>

            <RelativeLayout
                android:background="#66777777"
                android:layout_width="match_parent"
                android:layout_height="40dp">

                <TextView
                    android:id="@+id/rooms"
                    android:layout_marginLeft="10dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="18dp"
                    android:text=""
                    android:textColor="#fff"
                    android:layout_centerVertical="true"
                    />
            </RelativeLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="250dp"
            android:layout_height="match_parent">

            <RelativeLayout
                android:id="@+id/open_b"
                android:clickable="true"
                android:layout_width="78dp"
                android:layout_marginRight="5dp"
                android:layout_marginLeft="5dp"
                android:layout_height="match_parent"
                android:background="#4fcc54">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="18dp"
                    android:text="Open"
                    android:textColor="#fff"
                    android:id="@+id/openText"
                    android:layout_centerVertical="true"
                    android:layout_centerHorizontal="true"
                    />
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/disable_b"
                android:clickable="true"
                android:layout_width="78dp"
                android:layout_marginRight="5dp"
                android:layout_height="match_parent"
                android:background="#8e8e8e">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="18dp"
                    android:text="Disable"
                    android:textColor="#fff"
                    android:id="@+id/disableT"
                    android:layout_centerVertical="true"
                    android:layout_centerHorizontal="true"
                    />
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/delete_b"
                android:clickable="true"
                android:layout_width="78dp"
                android:layout_height="match_parent"
                android:background="#de5454">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="18dp"
                    android:text="Delete"
                    android:textColor="#fff"
                    android:id="@+id/deleteText"
                    android:layout_centerVertical="true"
                    android:layout_centerHorizontal="true"
                    />
            </RelativeLayout>
        </LinearLayout>
    </service.SwipeLayout>

</LinearLayout>

UPD: simple listView

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <ListView
        android:id="@+id/listView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:cacheColorHint="#00000000"
        android:divider="#adb8c2"
        android:dividerHeight="1dp"
        android:scrollingCache="false"
        android:smoothScrollbar="true" />


</RelativeLayout>

my main

  ArrayList<String> asd = new ArrayList<>();
    for(int i = 0; i < 10; i++) {
        asd.add(String.valueOf(i));
    }

    ListView listView = (ListView) findViewById(R.id.listView);
    Adapter adapter = new Adapter(getApplicationContext(), R.layout.item, asd);
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Toast.makeText(getBaseContext(), String.valueOf(position), Toast.LENGTH_LONG).show();
        }
    });

and item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/textView" />
</LinearLayout>

Its good, its work when i click in item listView, Toast is show position. But i need use SwipeLayout then i change code item.xml and add this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:descendantFocusability="blocksDescendants"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.bottom.smart.detetethis.SwipeLayout
        android:layout_marginTop="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:orientation="horizontal"
                android:background="#32777777"
                android:layout_width="match_parent"
                android:layout_height="70dp">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#fff"
                    android:layout_weight="0.7"
                    >

                    <TextView
                        android:id="@+id/conditions"
                        android:layout_marginTop="10dp"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="22dp"
                        android:text="Active"
                        android:textColor="#25ac36"
                        android:layout_centerVertical="true"
                        android:layout_centerHorizontal="true" />

                </RelativeLayout>

                <RelativeLayout
                    android:layout_weight="0.3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <TextView
                        android:id="@+id/description"
                        android:layout_marginLeft="10dp"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="18dp"
                        android:text="Turn light on \n when people arrive"
                        android:textColor="#222222"
                        android:layout_centerVertical="true"
                        />
                </RelativeLayout>

            </LinearLayout>

            <RelativeLayout
                android:background="#66777777"
                android:layout_width="match_parent"
                android:layout_height="40dp">

                <TextView
                    android:id="@+id/rooms"
                    android:layout_marginLeft="10dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="18dp"
                    android:text="Livingroom, Kitechen, Rest room"
                    android:textColor="#fff"
                    android:layout_centerVertical="true"
                    />
            </RelativeLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="250dp"
            android:layout_height="match_parent">

            <RelativeLayout
                android:id="@+id/open_b"
                android:layout_width="78dp"
                android:layout_marginRight="5dp"
                android:layout_marginLeft="5dp"
                android:layout_height="match_parent"
                android:background="#4fcc54">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="18dp"
                    android:text="Open"
                    android:textColor="#fff"
                    android:id="@+id/openText"
                    android:layout_centerVertical="true"
                    android:layout_centerHorizontal="true"
                    />
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/disable_b"
                android:layout_width="78dp"
                android:layout_marginRight="5dp"
                android:layout_height="match_parent"
                android:background="#8e8e8e">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="18dp"
                    android:text="Disable"
                    android:textColor="#fff"
                    android:id="@+id/disableT"
                    android:layout_centerVertical="true"
                    android:layout_centerHorizontal="true"
                    />
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/delete_b"
                android:layout_width="78dp"
                android:layout_height="match_parent"
                android:background="#de5454">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="18dp"
                    android:text="Delete"
                    android:textColor="#fff"
                    android:id="@+id/deleteText"
                    android:layout_centerVertical="true"
                    android:layout_centerHorizontal="true"
                    />
            </RelativeLayout>
        </LinearLayout>
    </com.bottom.smart.detetethis.SwipeLayout>

</LinearLayout>

got sick, its not working when i click item listView. But when I get a finger between the clearance item, its work:

enter image description here

to blame SwipeLayout, code SwipeLayout:

public class SwipeLayout extends LinearLayout {

    private ViewDragHelper viewDragHelper;
    private View contentView;
    private View actionView;
    private int dragDistance;
    private final double AUTO_OPEN_SPEED_LIMIT = 400.0;
    private int draggedX;

    public SwipeLayout(Context context) {
        this(context, null);
    }

    public SwipeLayout(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public SwipeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        viewDragHelper = ViewDragHelper.create(this, new DragHelperCallback());
    }

    @SuppressLint("MissingSuperCall")
    @Override
    protected void onFinishInflate() {
        contentView = getChildAt(0);
        actionView = getChildAt(1);
        actionView.setVisibility(GONE);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        dragDistance = actionView.getMeasuredWidth();
    }

    private class DragHelperCallback extends ViewDragHelper.Callback {

        @Override
        public boolean tryCaptureView(View view, int i) {
            return view == contentView || view == actionView;
        }

        @Override
        public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
            draggedX = left;
            if (changedView == contentView) {
                actionView.offsetLeftAndRight(dx);
            } else {
                contentView.offsetLeftAndRight(dx);
            }
            if (actionView.getVisibility() == View.GONE) {
                actionView.setVisibility(View.VISIBLE);
            }
            invalidate();
        }

        @Override
        public int clampViewPositionHorizontal(View child, int left, int dx) {
            if (child == contentView) {
                final int leftBound = getPaddingLeft();
                final int minLeftBound = -leftBound - dragDistance;
                final int newLeft = Math.min(Math.max(minLeftBound, left), 0);
                return newLeft;
            } else {
                final int minLeftBound = getPaddingLeft() + contentView.getMeasuredWidth() - dragDistance;
                final int maxLeftBound = getPaddingLeft() + contentView.getMeasuredWidth() + getPaddingRight();
                final int newLeft = Math.min(Math.max(left, minLeftBound), maxLeftBound);
                return newLeft;
            }
        }

        @Override
        public int getViewHorizontalDragRange(View child) {
            return dragDistance;
        }

        @Override
        public void onViewReleased(View releasedChild, float xvel, float yvel) {
            super.onViewReleased(releasedChild, xvel, yvel);
            boolean settleToOpen = false;
            if (xvel > AUTO_OPEN_SPEED_LIMIT) {
                settleToOpen = false;
            } else if (xvel < -AUTO_OPEN_SPEED_LIMIT) {
                settleToOpen = true;
            } else if (draggedX <= -dragDistance / 2) {
                settleToOpen = true;
            } else if (draggedX > -dragDistance / 2) {
                settleToOpen = false;
            }

            final int settleDestX = settleToOpen ? -dragDistance : 0;
            viewDragHelper.smoothSlideViewTo(contentView, settleDestX, 0);
            ViewCompat.postInvalidateOnAnimation(SwipeLayout.this);
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if(viewDragHelper.shouldInterceptTouchEvent(ev)) {
            return true;
        }
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        viewDragHelper.processTouchEvent(event);
        return true;
    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        if(viewDragHelper.continueSettling(true)) {
            ViewCompat.postInvalidateOnAnimation(this);
        }
    }
}

I can not delete it, because I need it in the project. I do not know how to fix this situation

g8214435
  • 737
  • 2
  • 6
  • 19
  • 1
    May be because list-item is consuming click event. – Dhaval Patel Oct 24 '15 at 17:14
  • @Blackbelt i update qestion – g8214435 Oct 24 '15 at 17:21
  • @DhavalPatel no i have not – g8214435 Oct 24 '15 at 17:22
  • 1
    have a look [here](http://stackoverflow.com/questions/25033669/listview-onitemclicklistener-does-not-work-android). The property goes in your item.xml – Blackbelt Oct 24 '15 at 17:24
  • 1
    Visit this link http://stackoverflow.com/questions/23602924/android-listview-item-click-is-not-working – Shishupal Shakya Oct 24 '15 at 17:33
  • 3
    Try to add this line in your **MyItem.xml** `android:descendantFocusability="blocksDescendants" ` and also chech [this](http://stackoverflow.com/questions/19155109/android-list-view-setonitemclicklistener-not-working) – pRaNaY Oct 24 '15 at 17:40
  • 1
    Keep your code, but start from the beginning again. Try getting another **simple** listview up and running with a simple item. Expand from there, and see what is the issue. Show us that a simple version can work in your environment – overflowingStack Oct 24 '15 at 19:13
  • I create simple version and described it in my qestion – g8214435 Oct 24 '15 at 19:33
  • 1
    OK, it seems like your `swipeLayout` is capturing your touch event and is NOT propagating that touch event elsewhere. The issue most likely is within `onInterceptTouchEvent` and `onTouchEvent`. In order for the touch to be processed to the actual list item, you have to return `false` somewhere in the method. – overflowingStack Oct 25 '15 at 10:01
  • 1
    Just to test. Comment out the code you have in those 2 methods, and just `return false;` instead. I haven't done code like this before, but play around with this. – overflowingStack Oct 25 '15 at 10:04
  • @overflowingStack thanks, i will try – g8214435 Oct 26 '15 at 19:48

2 Answers2

4

Sometimes the ListView click doesn't work. And at this case you might have to add one more attribute.

android:descendantFocusability="blocksDescendants"

Add this attribute to the top most layout of your XML where you have provided the ListView elements.

Shishupal Shakya
  • 1,632
  • 2
  • 18
  • 41
1

In your ListView remove the xml attribute android:clickable="true"

This is taking your click event and not sending it down to the child views.

Edit

You are passing in getApplicationContext() which is the wrong context. The listener does work, but the Toast is not displaying in the right context. You need to pass in getContext(). However, since this is an Activity, you can just pass in this instead.

public void outputListView(ArrayList<String> list) {
    Adapter adapter = new Adapter(this, R.layout.item, list);
    adapter.notifyDataSetChanged();
    listView.setAdapter(adapter);

    listView.setItemsCanFocus(false);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Toast.makeText(this, String.valueOf(position), Toast.LENGTH_SHORT).show();
        }
    });
}