Android ListView setOnItemClickListener不工作

Android ListView setOnItemClickListener不工作,android,listview,Android,Listview,对不起我的英语。我花了很多时间,但我不能解决我的问题。我有listView,我想要setsetOnItemClickListenerhim。这是我的列表视图: <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id = "@+id/list

对不起我的英语。我花了很多时间,但我不能解决我的问题。我有listView,我想要set
setOnItemClickListener
him。这是我的列表视图:

 <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> 
生病了,单击项目列表视图时它不工作。但当我在清关项目之间插上一根手指时,它的工作:

归咎于
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);
        }
    }
}
公共类SwipeLayout扩展了LinearLayout{
私人视盘视盘视盘视盘视盘;
私有视图contentView;
私有视图;
私人内部牵引距离;
私人最终双自动打开速度限制=400.0;
私人int draggedX;
公共SwipeLayout(上下文){
这个(上下文,空);
}
公共SwipeLayout(上下文、属性集属性){
这(上下文,attrs,-1);
}
公共SwipeLayout(上下文、属性集属性、int-defStyleAttr){
super(上下文、attrs、defStyleAttr);
viewDragHelper=viewDragHelper.create(这是新的DragHelperCallback());
}
@SuppressLint(“丢失超级调用”)
@凌驾
充气时受保护的空隙(){
contentView=getChildAt(0);
actionView=getChildAt(1);
actionView.setVisibility(已消失);
}
@凌驾
测量时的保护空隙(内部宽度测量等级、内部高度测量等级){
超级测量(宽度测量、高度测量);
dragDistance=actionView.getMeasuredWidth();
}
私有类DragHelperCallback扩展了ViewDragHelper.Callback{
@凌驾
公共布尔tryCaptureView(视图,int i){
返回视图==contentView | |视图==actionView;
}
@凌驾
ViewPositionChanged上的公共无效(视图更改视图、int-left、int-top、int-dx、int-dy){
draggedX=左;
if(changedView==contentView){
actionView.offsetLeftAndRight(dx);
}否则{
contentView.offsetLeftAndRight(dx);
}
if(actionView.getVisibility()==View.GONE){
actionView.setVisibility(View.VISIBLE);
}
使无效();
}
@凌驾
公共int-clampViewPositionHorizontal(视图子对象、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);
返回newLeft;
}否则{
final int minLeftBound=getPaddingLeft()+contentView.getMeasuredWidth()-dragDistance;
final int maxLeftBound=getPaddingLeft()+contentView.getMeasuredWidth()+getPaddingRight();
final int newLeft=Math.min(Math.max(left,minLeftBound),maxlefbound);
返回newLeft;
}
}
@凌驾
公共int getViewHorizontalDragRange(查看子对象){
回拖距离;
}
@凌驾
ViewReleased上的公共无效(View releasedChild、float xvel、float yvel){
super.onViewReleased(releasedChild、xvel、yvel);
boolean settleToOpen=false;
如果(xvel>自动打开速度限制){
结算开放=假;
}否则如果(xvel<-自动打开速度限制){
沉降开放=真;
}否则如果(draggedX-dragDistance/2){
结算开放=假;
}
最终int SELLETEDESTx=沉降打开?-拖动距离:0;
viewDragHelper.smoothSlideViewTo(contentView,settleDestX,0);
ViewCompat.postInvalidateOnAnimation(SwipeLayout.this);
}
}
@凌驾
公共布尔值onInterceptTouchEvent(MotionEvent ev){
if(视图绘制器应插入触点事件(ev)){
返回true;
}
返回超级onInterceptTouchEvent(ev);
}
@凌驾
公共布尔onTouchEvent(运动事件){
viewDragHelper.processTouchEvent(事件);
返回true;
}
@凌驾
public void computeScroll(){
super.computeScroll();
如果(viewDragHelper.ContinueSetting(真)){
ViewCompat.postInvalidateOnAnimation(此);
}
}
}

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

在您的
列表视图中
删除xml属性
android:clickable=“true”

这将获取单击事件,而不是将其发送到子视图

编辑


您正在传入的
getApplicationContext()
是错误的上下文。侦听器确实可以工作,但Toast没有在正确的上下文中显示。您需要传入
getContext()
。但是,由于这是一项活动,因此您只需传入

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();
        }
    });
}
public void outputListView(ArrayList列表){
适配器=新适配器(此,R.layout.item,列表);
adapter.notifyDataSetChanged();
setAdapter(适配器);
setItemsCanFocus(false);
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView父视图、视图v、整型位置、长id){
Toast.makeText(this,String.valueOf(position),Toast.LENGTH_SHORT.show();
}
});
}

在您的
列表视图中
删除xml属性
android:clickable=“true”

这将获取单击事件,而不是将其发送到子视图

编辑


您正在传入的
getApplicationContext()
是错误的上下文。侦听器确实可以工作,但Toast没有在正确的上下文中显示。您需要传入
getContext()
。但是,由于这是一项活动,因此您只需传入

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();
        }
    });
}
public void outputListView(Arr)
  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();
        }
    });
<?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>
<?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>
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);
        }
    }
}
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();
        }
    });
}
android:descendantFocusability="blocksDescendants"