有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Listview在按下项时不触发

所有我的视图和非聚焦和不可点击,除了开关是可点击的,但使其不可点击仍然不会使列表视图启动

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
        xmlns:tools="http://schemas.安卓.com/tools"
        安卓:id="@+id/timed_events_list"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:orientation="horizontal"
        安卓:clickable="false"
        安卓:descendantFocusability="blocksDescendants"
        安卓:focusable="false"
        安卓:focusableInTouchMode="false"
         >

        <LinearLayout
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:orientation="vertical"
             >

            <TextView
            安卓:id="@+id/event_name"
            安卓:layout_width="wrap_content"
            安卓:layout_height="25dp"
            安卓:textSize="20sp"
            安卓:textStyle="bold"
            安卓:singleLine="true"
            安卓:focusable="false"
            安卓:focusableInTouchMode="false"
            安卓:clickable="false"
            />
            <TextView
            安卓:id="@+id/event_time"
            安卓:layout_width="wrap_content"
            安卓:layout_height="26dp"
            安卓:singleLine="true"
            安卓:focusable="false"
            安卓:focusableInTouchMode="false"
            安卓:clickable="false"
            />
        </LinearLayout>

        <Switch
            安卓:id="@+id/state"
            安卓:layout_width="wrap_content"
            安卓:layout_height="wrap_content"
            安卓:layout_alignParentRight="true"
            安卓:layout_alignParentTop="true"
            安卓:onClick="isActivated"
            安卓:focusable="false"
            安卓:focusableInTouchMode="false"  
            />

    </RelativeLayout>

下面是onCreate方法中的OnItemSelectedListener

listView.setOnItemSelectedListener( new OnItemSelectedListener(){
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
                Toast.makeText(ActivityContext, "its working", Toast.LENGTH_LONG).show();
                Intent intent ;
                if(listAdapter.getItemViewType(position) == 1){
                    intent = new Intent(ActivityContext,Volume.class);
                    Volume.currentPref((SoundDetails) timers.get(position),position);
                } else{
                    intent = new Intent(ActivityContext,Message.class);
                    Message.currentMessage((MessageListDetails) timers.get(position),position);
                }
                startActivity(intent); 
            }
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }});

共 (1) 个答案

  1. # 1 楼答案

    对于ListView,您需要设置onItemClickListener,而不是onItemSelectedListener 像这样的变化

    listView.setOnItemClickListener(new OnItemClickListener() {
    
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // perform your operation
    
            }
        });