【问题标题】:Listview not firing when item is pressed按下项目时列表视图未触发
【发布时间】:2014-07-23 13:04:24
【问题描述】:

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

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

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

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

        <Switch
            android:id="@+id/state"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:onClick="isActivated"
            android:focusable="false"
            android: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

            }});

【问题讨论】:

标签: java android


【解决方案1】:

对于ListView,您需要设置onItemClickListener 而不是onItemSelectedListener 改成这样..

listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // perform your operation

        }
    });

【讨论】:

  • eclipse 给我一个错误 AdapterView 类型中的方法 setOnItemClickListener(AdapterView.OnItemClickListener) 不适用于参数(新 OnItemClickListener(){})
  • @user3553551 按 ctrl+shift+o 导入语句
  • @user3553551 或将此行添加到您的重要语句“import android.widget.AdapterView.OnItemClickListener;”
  • 我忘记导入方法了。这就是它显示错误的原因。非常感谢它的工作
猜你喜欢
  • 2012-01-29
  • 2015-08-07
  • 2019-03-03
  • 2018-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多