0

I have this custom layout, row.xml in my ListView rows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:orientation="horizontal"
    android:weightSum="10">

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="9"
        android:id="@+id/rowTextView"
        android:typeface="serif"
        android:gravity="center_vertical"
        android:textSize="15sp"
        android:layout_marginLeft="5dp" />

    <ImageView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/swipeImageView"
        android:src="@mipmap/swipe"
        android:alpha="0.5"
        android:padding="11dp" />

</LinearLayout>

Now I want to change row.xml for this layout clicked_row.xml when using onListItemClick():

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:orientation="horizontal"
    android:weightSum="10">

    <EditText
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="9"
        android:id="@+id/rowTextView"
        android:typeface="serif"
        android:gravity="center_vertical"
        android:textSize="15sp"
        android:layout_marginLeft="5dp" />

    <ImageView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/saveImageView"
        android:src="@mipmap/trash"
        android:alpha="0.5"
        android:padding="11dp" />

</LinearLayout>

I was trying to use this answer; but they seem to be manipulating the visibility of views inside the layouts rather than replacing one layout for the other. I'm not sure what I should use to replace the row.xml for clicked_row.xml:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);


     //Code for replacing row.xml with clicked_row.xml


}

Thanks on any help!

Community
  • 1
  • 1
CommonSenseCode
  • 23,522
  • 33
  • 131
  • 186
  • I think changing the visibility itself is sufficient for the problem. What is the exact need to switch the resource files? – Sadeshkumar Periyasamy Jul 02 '15 at 15:40
  • @SadeshkumarPeriyasamy, the thing is I plan to include more views on **clicked_row.xml** so I though maybe it was easier to replace xmls than setting invisible 2 views and then visible other 10 views. – CommonSenseCode Jul 02 '15 at 15:44
  • In that case, you can override **getItemViewTypeCount()** and **getItemViewType(int position)** of your Adapter class to have two different views. In the **onListItemClick** you can switch the type of modal and refresh the listview. – Sadeshkumar Periyasamy Jul 02 '15 at 15:49

2 Answers2

1

I think that better approach will be to merge the 2 files:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:orientation="horizontal"
    android:weightSum="10">

<TextView
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="9"
    android:id="@+id/rowTextView"
    android:typeface="serif"
    android:gravity="center_vertical"
    android:textSize="15sp"
    android:layout_marginLeft="5dp" />

<EditText
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="9"
    android:id="@+id/rowEditText"
    android:typeface="serif"
    android:gravity="center_vertical"
    android:textSize="15sp"
    android:layout_marginLeft="5dp"
    android:visibility="gone"/>

<ImageView
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:id="@+id/swipeImageView"
    android:src="@mipmap/swipe"
    android:alpha="0.5"
    android:padding="11dp" />

</LinearLayout>

And then:

@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        ((ViewGroup)v).findViewById(R.id.rowTextView).setVisibility(View.GONE);
        ((ViewGroup)v).findViewById(R.id.rowEditText).setVisibility(View.VISIBLE);
        ImageView imageView = (ImageView)((ViewGroup)v).findViewById(R.id.swipeImageView);
        imageView.setImageDrawable(imageView.getContext().getResources().getDrawable(R.mipmap.trash));



        //Code for replacing row.xml with clicked_row.xml


    }
yshahak
  • 4,996
  • 1
  • 31
  • 37
  • Thanks for the example this is what I also though at the beginning. I'll give it 1 or 2 days more if nobody else gives a better answer I'll acept yours anyways thanks and have your +1 – CommonSenseCode Jul 03 '15 at 01:46
0

You can add both TextView and ImageView to the same layout. You can hide the visibility of one on onListItemClick

But this will cause the problems when you scroll the list since Android reuses the list item views. So instead of accessing the ListItemView directly, in your model class, add a property like selected.

In the Adapater.getView() hide/show view elements according to this selected field value.

This post will help you to understand how Android ListView works: How ListView's recycling mechanism works

Community
  • 1
  • 1
Sadeshkumar Periyasamy
  • 4,848
  • 1
  • 26
  • 31