1

How to make a listView with diffente items content, I would like add simple imageView for specifics items and animated gif for other specifics items. How can I do?

Thanks

sawing
  • 11
  • 1
  • 3

2 Answers2

0

here are couple of tutorials:

Link 1

Link 2

Link 3

You will nedd to have an activity with list view in it. activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_row_selector" />

</RelativeLayout>

and a list view row xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_row_selector"
    android:padding="8dp" >

    <!-- Thumbnail Image -->
    <com.android.volley.toolbox.NetworkImageView
        android:id="@+id/thumbnail"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="8dp" />

    <!-- Movie Title -->
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/thumbnail"
        android:layout_toRightOf="@+id/thumbnail"
        android:textSize="@dimen/title"
        android:textStyle="bold" />

    <!-- Rating -->
    <TextView
        android:id="@+id/rating"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/title"
        android:layout_marginTop="1dip"
        android:layout_toRightOf="@+id/thumbnail"
        android:textSize="@dimen/rating" />

    <!-- Genre -->
    <TextView
        android:id="@+id/genre"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/rating"
        android:layout_marginTop="5dp"
        android:layout_toRightOf="@+id/thumbnail"
        android:textColor="@color/genre"
        android:textSize="@dimen/genre" />

    <!-- Release Year -->
    <TextView
        android:id="@+id/releaseYear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:textColor="@color/year"
        android:textSize="@dimen/year" />

</RelativeLayout>
Sagar Pilkhwal
  • 3,998
  • 2
  • 25
  • 77
  • Thanks for you reply, but I know how to make a custom listview. My question was how, for exemple, put in my first item a animated gif, for next 10th items a simple imageview, and again next one a animated gif etc...? – sawing Sep 17 '14 at 12:56
  • Take a look at this [SO post](http://stackoverflow.com/a/11736861/3326331) for adding gif images – Sagar Pilkhwal Sep 17 '14 at 13:40
  • thanks, my problem isn't how play my gif (thanks for your post), but how in my listView have items with a simple jpg image and sometimes only my gif? – sawing Sep 22 '14 at 07:38
  • you can put up some logic to add jpg/gif in you adapter class when you are adding list items to your list view. – Sagar Pilkhwal Sep 22 '14 at 07:41
0

Try to add a custom adapter for your ListView. For example:

public class MyAdapter extends BaseAdapter {


    @Override
    public View getView(int positon, View view, ViewGroup viewGroup) {
        return null; //**return the custom View you want here**.
        //if (positon == firstKindOfViewPosition) {
        //    return firstKindOfView;
        //} else if (positon == secondKindOfViewPosition) {
        //    return secondKindOfView;
        //}
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public int getCount() {
        return 0;
    }

}

Then you can add this adapter to your ListView to show whatever View you want.

The BaseAdapter API.You can get more information here.

zz-m
  • 446
  • 5
  • 15
  • I already have a custom adapter, and in my json string I have elements for my listView (image, text...) and sometimes just an animated gif. So I know how put animated gif or simple image and text in my custom adapter, but I don't know how make my listView with different content item? – sawing Sep 22 '14 at 07:36
  • I wrote the comment for 20 mins, then I found that the solution provide by the comment under your question is much better than my answer. Have you try that? [link from that comment](http://stackoverflow.com/questions/4777272/android-listview-with-different-layout-for-each-row/4777306#4777306) – zz-m Sep 22 '14 at 09:27
  • Everything works fine I found the solution, I use two layout for my gif and my simple image for each custom items. Thanks a lot for your help – sawing Sep 23 '14 at 07:35