daniel_c05
daniel_c05

Reputation: 11518

Android: Changing Visibility of an item inside all listview rows

I have a ListView in my application which can, after user requests it, be re-arrangeable via the drag of an imageview at the start of each listview's row. I present the user with the listview in a small window, and in order to maximize the space, I set the visibility of the drag view as GONE, and plan on only setting it to Visible when user selects a menu item that calls for Edit List Items.

Here's the layout for a row item:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/activatedBackgroundIndicator"
android:orientation="horizontal" >

<ImageView
    android:id="@id/drag_handle"
    android:layout_width="32dp"
    android:layout_height="32dp"
    android:layout_margin="8dp"
    android:visibility="gone"
    android:src="@drawable/ic_drag_dots_holo_dark" />

<View
    android:layout_width="1dp"
    android:layout_height="match_parent"
    android:layout_marginBottom="16dp"
    android:layout_marginTop="16dp"
    android:background="?android:attr/dividerVertical" />

    <TextView
        android:id="@+id/drag_n_drop_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:maxLines="1"
        android:minHeight="24dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="?android:attr/textColorPrimary" />
</LinearLayout>

Here's the code for the menu selection:

    @Override
public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_edit_playlist:
        if (inEditMode) {
            //If we are already showing the drag view, hide it. 
            inEditMode = false;                             
            findViewById(R.id.drag_handle).setVisibility(View.GONE);
        }
        else {
            //If the drag view is hidden, make it visible. 
            inEditMode = true;
            findViewById(R.id.drag_handle).setVisibility(View.VISIBLE);
        }                       
        return true;
    default:
        return super.onContextItemSelected(item);
    }
}

The problem with this code, is that when I call setVisibility() it only changes the visibility of the drag view on the firstmost item of the listview, and the dragview on all other rows remains hidden.

What would I do to make ALL dragViews on all rows visible in this case?

EDIT Here's an image of how I want it to look after pressing edit, and here's an image of how it looks right now, with the code above.

Thanks!

Upvotes: 1

Views: 7231

Answers (1)

user
user

Reputation: 87064

The problem with this code, is that when I call setVisibility() it only changes the visibility of the drag view on the firstmost item of the listview, and the dragview on all other rows remains hidden.

That behavior it's normal as you're trying to change the visibility of a View from a row using findViewById(), method which will return the first occurrence of a View with that id from the layout. That first occurrence will be the situated in the first visible row of the list(anyway the findViewById method will not work, as you can probably see the views in the ListView's rows will all have the same ids).

What would I do to make ALL dragViews on all rows visible in this case?

The proper way is to let the adapter of the ListView know in which state it's currently in(inEditMode) and show/hide the drag handle in the getView method based on that. Once you do this all you have to do in the onContextItemSelected is to update the inEditMode state variable and call notifyDataSetChanged() method on the adapter.

Upvotes: 3

Related Questions