ListView setOnItemClickListener不起作用

时间:2015-10-24 17:10:26

标签: android listview

抱歉我的英文。我花了很多次但我无法解决我的问题。我有listView,我想要设置setOnItemClickListener他。这是我的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> 

我的代码

 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();
            }
        });
    }

但是当我点击项目时没有任何反应

UPD

我的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:简单列表视图

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>

我的主要

  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();
        }
    });

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>

它很好,它的工作当我点击项目列表视图,Toast是显示位置。但我需要使用SwipeLayout然后我更改代码item.xml并添加:

<?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>

生病了,当我点击项目列表视图时,它无法正常工作。但是当我在清除项目之间进行操作时,它的工作是:

enter image description here

责怪SwipeLayout,代码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);
        }
    }
}

我无法删除它,因为我在项目中需要它。我不知道如何解决这种情况

2 个答案:

答案 0 :(得分:2)

有时ListView点击不起作用。在这种情况下,您可能需要再添加一个属性。

android:descendantFocusability="blocksDescendants"

将此属性添加到您提供ListView元素的XML的最顶层布局。

答案 1 :(得分:1)

ListView删除xml属性android:clickable="true"

这是您的点击事件,而不是将其发送到子视图。

修改

您正在传递getApplicationContext()这是错误的背景。监听器确实有效,但Toast没有在正确的上下文中显示。您需要传递getContext()。但是,由于这是一项活动,您只需传入this

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();
        }
    });
}
相关问题