Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing OnItemLongClick Listeners with a ListView
    text
    copied!<p>I have a <code>ListView</code> which contains entries given by layout file <strong>entry.xml</strong> I want to start with the <code>ImageView</code> visibility set to <code>View.GONE</code> and <code>onItemLongClick</code> change that to <code>View.VISIBLE</code>. The <code>ListView</code> entry that is long clicked would remain highlighted.</p> <p>I then want to add an <code>onClickListener</code> to the <code>ImageView</code> (whilst its visible) so that the clicked <code>ListView</code> entry can be deleted by tapping the image.</p> <p>There also needs to be a second <code>OnClickListener</code> on the <code>ListView</code> entry so that if it has been long clicked and highlighted this can be undone by (short)clicking on the entry. This would restore the <code>ImageView</code> back to <code>View.GONE</code>.</p> <p><strong>How can I achieve this?</strong></p> <hr> <p><strong>listview.xml</strong></p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" &gt; &lt;ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="wrap_content"&gt; &lt;/ListView&gt; &lt;/LinearLayout&gt; </code></pre> <p><strong>entry.xml</strong></p> <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="8dp" &gt; &lt;ImageView android:id="@+id/entry_deleteicon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:adjustViewBounds="true" android:baselineAlignBottom="false" android:maxHeight="40dp" android:maxWidth="45dp" android:src="@drawable/delete" android:visibility="gone" /&gt; &lt;TextView android:id="@+id/entry_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="left" android:text="text" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p><strong>My Custom Adapter</strong></p> <pre><code>private class SaveAdapter extends ArrayAdapter&lt;String&gt; { private int resourceId; private SaveHolder[] holder; public SaveAdapter(Context context, int resource) { super(context, resource); this.resourceId=resource; this.holder = new SaveHolder[5]; } @Override public int getCount() { return 5; } @Override public View getView(final int position, View row, ViewGroup parent) { holder[position] = null; if(row==null){ LayoutInflater inflater = getActivity().getLayoutInflater(); row = inflater.inflate(resourceId, parent, false); holder[position] = new SaveHolder(); holder[position].textName = (TextView) row.findViewById(R.id.entry_name); holder[position].iconDelete = (ImageView) row.findViewById(R.id.entry_deleteicon); row.setTag(holder[position]); }else{ holder[position] = (SaveHolder) row.getTag(); } holder[position].textName.setText("name"); return row; } } static class SaveHolder{ TextView textName; ImageView iconDelete; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload