Robert Simoes Robert Simoes - 1 month ago 11
Android Question

Long Click Stopping Click Action on ListView

I've read through all the longclick + click listener SO posts for ListView but cannot find a solution to this issue. I am sure that I am doing things right based on the posts I've read.

Problem

My click gesture used to register but after attaching the LongClickListener to my List view, only the Long Click gesture is registering in the ListView. Any thoughts on what I've done ?

Code

TheStreamActivity.java (ListView)

public class TheStreamActivity extends AppCompatActivity{
private ListView listView;

/** UI Actions and Set up */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_the_stream);

// Attach the adapter to a ListView
listView = (ListView) findViewById(R.id.stream_feed);

if (savedInstanceState!=null && !savedInstanceState.isEmpty())
{
lvContent = savedInstanceState.getParcelableArrayList(RESTORED_USER_FLOWS);
manager = savedInstanceState.getParcelable(RESTORED_MANAGER_UTIL);
} else {
lvContent = new ArrayList<>();
manager = new DataManagerUtil(this);
}


}


/** Sets up each of the individual list view items to be clicked and launch an
* new activity based on selected Flow Object.
*
*/
private void setItemOnClicks() {
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

/* Passes Flow but passes the memory address of the childFlowElements
instead of the actual object containing the
*/
Flow selectedFlow = (Flow) listView.getItemAtPosition(position);

Intent i = new Intent(TheStreamActivity.this, FlowSandBoxActivity.class);

i.putExtra("selectedFlow", selectedFlow);
// Parcels the Flow Object to@ be passed to new activity
startActivity(i);
}

});

listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
showPopUpMenu(TheStreamActivity.this, view);
return true;
}
});
}

public void showPopUpMenu(Context ctx, View v) {
PopupMenu popup = new PopupMenu(ctx, v);

// This activity implements OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId())
{
case R.id.menu_delete:
return true;
default:
return false;
}

}
});
popup.inflate(R.menu.menu_flow_popup);
popup.show();
}

@Override
protected void onResume() {
super.onResume();
setItemOnClicks();
}


List Item

flow_item_in_stream.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="75dp"\
android:longClickable="true"
>
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:textAppearance=
"?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="12dp"
android:id="@+id/item_flow_name" />
</RelativeLayout>

</android.support.v7.widget.CardView>

</LinearLayout>


List View

stream_feed.xml

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="2"
android:columnCount="1">

<include layout="@layout/stream_toolbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnSpan="1"
android:layout_gravity="fill_horizontal" />

<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/stream_feed"
android:clipToPadding="false"
android:layout_columnSpan="1"
android:layout_gravity="fill_horizontal"
android:layout_row="1">
</ListView>

</GridLayout>

Answer

I have found the solution, and it seems quite intuitive/silly now. Maybe someone can verify my hypothesis?

It appears as though if you enable or android:clickable="true" android:LongClick="true" on an XML level, it will prevent the programmatic onClick Listeners from functioning

This makes some sense to me because I am thinking that on an XML level we have the onClick attribute who's method is blank when clicks are implemented programatically the view is looking for a blank method (ie. do nothing) instead of the onClick listener method or perhaps the view is rendered before the onClick listeners are set?

Not too sure but here was my fix (either way my lesson learned will be use either programatic or XML based clickables)

private void setItemOnClicks() {
        // TODO On Click Listeners not working
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    /* Passes Flow but passes the memory address of the childFlowElements
                     instead of the actual object containing the
                      */
                Toast.makeText(TheStreamActivity.this, "SELected", Toast.LENGTH_LONG).show();
                Flow selectedFlow = (Flow) listView.getItemAtPosition(position);

                Intent i = new Intent(TheStreamActivity.this, FlowSandBoxActivity.class);

                i.putExtra("selectedFlow", selectedFlow);
                    // Parcels the Flow Object to@ be passed to new activity
                startActivity(i);
            }

        });

        listView.setLongClickable(true);

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                showPopUpMenu(TheStreamActivity.this, view, position);
                return true;
            }
        });


    }

List Item (see that there is no clickable or longclickable attribute)

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="75dp"
    android:divider="@android:color/transparent"
    android:dividerHeight="15dp">
<android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        card_view:cardCornerRadius="8dp"
        card_view:cardElevation="8dp"
        android:foreground="?android:attr/selectableItemBackground">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <TextView
            android:textAppearance=
                "?android:attr/textAppearanceMedium"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="12dp"
            android:id="@+id/item_flow_name" />
        <!-- Flow Symbol Icon item_flow_name-->


        <ImageView
            android:contentDescription="@string/task_flag_description"
            android:id="@+id/item_element_icon"
            android:src="@drawable/flag_small_black_outline"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_marginEnd="125dp"
            android:layout_centerVertical="true"
            android:layout_alignParentEnd="true" />
            <!-- Insert Flow Element Icon -->

        <TextView
            android:id="@+id/item_element_count"
            android:textAppearance=
                "?android:attr/textAppearanceSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_alignLeft="@+id/item_element_icon"
            android:layout_marginLeft="25dp" />

        <ImageView
            android:contentDescription="@string/timer_description"
            android:id="@+id/item_time_icon"
            android:src="@drawable/timer_black_48dp"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_centerVertical="true"
            android:layout_alignStart="@+id/item_element_icon"
            android:layout_toRightOf="@+id/item_element_icon"
            android:layout_marginLeft="55dp" />
        <!-- Insert Flow Element Icon -->

        <TextView
            android:id="@+id/item_total_time"
            android:textAppearance=
                "?android:attr/textAppearanceSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_alignLeft="@+id/item_time_icon"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="2dp" />

        <TextView
            android:id="@+id/item_time_units"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/item_time_icon"
            android:layout_marginLeft="50dp"
            android:layout_alignLeft="@+id/item_time_icon"
            android:layout_marginTop="2dp" />
        </RelativeLayout>

    </android.support.v7.widget.CardView>

</LinearLayout>
Comments