OnItemLongClickListener不工作在自定义列表视图 [英] OnItemLongClickListener is not working in a custom listview

查看:162
本文介绍了OnItemLongClickListener不工作在自定义列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示快捷菜单后不久单击自定义列表视图中的项目。我现在用的是 ActionBarSherlock 。用我发现,长按事件不会触发Toast通知尝试调试。如何解决这个问题?

  m_vwJokeLayout =(ListView控件)findViewById(R.id.jokeListViewGroup);
m_vwJokeLayout.setOnItemLongClickListener(新AdapterView.OnItemLongClickListener(){

            @覆盖
            公共布尔onItemLongClick(适配器视图<>母公司视图中查看,
                    INT位置,长的id){
                // TODO自动生成方法存根
                如果(m_vwActionMode!= NULL)
                {
                    Toast.makeText(getApplicationContext(),里面有关于Itemlongclick,Toast.LENGTH_LONG).show();
                    返回false;
                }
                Toast.makeText(getApplicationContext(),里面有关于Itemlongclick,Toast.LENGTH_LONG).show();
                m_vwActionMode = getSherlock()startActionMode(m_vwCallback)。
                view.setSelected(真正的);
                返回true;
            }


        });
 

的XML code在我的的ListView 声明下面给出:

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =match_parent
    机器人:layout_height =FILL_PARENT>
    <的LinearLayout
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:方向=横向
    >

   <按钮
        机器人:ID =@ + ID / addJokeButton
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=@字符串/ add_joke_button
        />
    <的EditText
        机器人:ID =@ + ID / newJokeEditText
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_weight =2
        机器人:提示=@字符串/ enter_text_edittext
        />


    < / LinearLayout中>

    <的ListView
        机器人:ID =@ + ID / jokeListViewGroup
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT>

< / ListView控件>



< / LinearLayout中>
 

的XML code为查看在自定义的ListView 是如下:

 < XML版本=1.0编码=UTF-8&GT?;
    < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:重力=center_vertical
        机器人:方向=横向
        机器人:weightSum =3>

    <的LinearLayout
        机器人:layout_width =200dp
        机器人:layout_height =match_parent
        机器人:layout_weight =2
        机器人:重力=center_vertical
        机器人:方向=垂直>

        <的TextView
            机器人:ID =@ + ID / jokeTextView
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_marginRight =10dp
            机器人:ellipsize =中间
            机器人:重力=中心
            机器人:paddingBottom会=10dp
            机器人:paddingTop =10dp
            机器人:文本=你好
            机器人:TEXTSIZE =16像素


             />

    < / LinearLayout中>

    <的LinearLayout
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =match_parent
        机器人:layout_weight =1
        机器人:重力=center_horizo​​ntal
        机器人:方向=垂直>

        < RadioGroup中
            机器人:ID =@ + ID / ratingRadioGroup
            机器人:layout_width =40dp
            机器人:layout_height =60dp
            机器人:layout_marginRight =10dp
            机器人:重力=中心

           >

            <单选按钮
                机器人:ID =@ + ID / likeButton
                机器人:背景=@可绘制/像
                机器人:按钮=@空
                />

            <单选按钮
                机器人:ID =@ + ID / dislikeButton
                机器人:背景=@可绘制/不喜欢
                机器人:按钮=@空
                />
        < / RadioGroup中>

    < / LinearLayout中>

   < / LinearLayout中>
 

解决方案

它发生是由于单选按钮的列表项的布局可聚焦性。

添加下面一行到你的ListView的项目布局的根布局。

 安卓descendantFocusability =blocksDescendants
 

I want context menu to appear after long clicking an item in a custom list view. I am using the ActionBarSherlock. Trying to debug using Toast notifications I have discovered that Long click event is not fired. How to resolve this issue ?

m_vwJokeLayout=(ListView)findViewById(R.id.jokeListViewGroup);
m_vwJokeLayout.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                if(m_vwActionMode!=null)
                {
                    Toast.makeText(getApplicationContext(), "Inside On Itemlongclick", Toast.LENGTH_LONG).show();
                    return false;
                }
                Toast.makeText(getApplicationContext(), "Inside On Itemlongclick", Toast.LENGTH_LONG).show();
                m_vwActionMode=getSherlock().startActionMode(m_vwCallback);
                view.setSelected(true);
                return true;
            }


        });

The xml code where my ListView is declared is given below:

<?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="fill_parent">
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
    >

   <Button 
        android:id="@+id/addJokeButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/add_joke_button"
        />
    <EditText 
        android:id="@+id/newJokeEditText"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_weight="2"
        android:hint="@string/enter_text_edittext"
        />


    </LinearLayout>

    <ListView
        android:id="@+id/jokeListViewGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

</ListView>



</LinearLayout>

The xml code for the View in the custom ListView is given below:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:weightSum="3" >

    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:gravity="center_vertical"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/jokeTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:ellipsize="middle"
            android:gravity="center"
            android:paddingBottom="10dp"
            android:paddingTop="10dp"
            android:text="Hello,"
            android:textSize="16px"


             />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <RadioGroup
            android:id="@+id/ratingRadioGroup"
            android:layout_width="40dp"
            android:layout_height="60dp"
            android:layout_marginRight="10dp"
            android:gravity="center" 

           >

            <RadioButton
                android:id="@+id/likeButton"
                android:background="@drawable/like"
                android:button="@null"
                />

            <RadioButton
                android:id="@+id/dislikeButton"
                android:background="@drawable/dislike"
                android:button="@null"
                />
        </RadioGroup>

    </LinearLayout>

   </LinearLayout> 

解决方案

It happens due to focusability of radiobutton in listitem layout.

Add below line to root layout of your ListView's Item layout.

android:descendantFocusability="blocksDescendants"

这篇关于OnItemLongClickListener不工作在自定义列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆