OnItemLongClickListener is not working in a custom listview

扶醉桌前 提交于 2019-11-30 02:56:44

问题


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> 

回答1:


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"



回答2:


You should use the onItemLongClickListener of the listview as follows:

lv.setOnItemLongClickListener(new OnItemLongClickListener() {

        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int pos, long id) {
            // TODO Auto-generated method stub

            Log.v("long clicked","pos"+" "+pos);

            return true;
        }
    }); 

Note that the listener isnt a derivative from the AdapterView class!!!!



来源:https://stackoverflow.com/questions/18312197/onitemlongclicklistener-is-not-working-in-a-custom-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!