Junnel Gallemaso
Junnel Gallemaso

Reputation: 881

ImageView and Listview - Android

Good Day!!!

I have a problem with the layout specially in the landscape orientation in android.

When I am going to change the orientation to landscape, it seems that the ImageView is in a fixed position and I can only see scrollbar in the listview...I just want to have a layout that also the imageview can be scrolled so that I can see clearly the list items.

Here's my layout 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="fill_parent" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"
    android:layout_height="wrap_content"
    android:contentDescription="@string/app_name"
    android:src="@drawable/banner" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/imageView1"
        android:drawSelectorOnTop="true" />

</RelativeLayout>  

Upvotes: 2

Views: 2346

Answers (2)

Khan
Khan

Reputation: 7605

I think using ListView in ScrollView is not a good thing If u want that image should also scroll than do like this:

1) For Listview you need a custome adapter in which in custom adapter class's getview() method u have to something like this

 public View getView(int position, View convertView, ViewGroup parent)
    {

    LayoutInflater inflater = getLayoutInflater();
    View row;

    row = inflater.inflate(R.layout.listview_rowlayout, parent, false);

    TextView textview = (TextView) row.findViewById(R.id.tv_list);
    ImageView imageview = (ImageView) row.findViewById(R.id.iv_list);

    textview.setText(data_text[position]);
    if(position==0){
    imageview.setImageResource(R.drawable.YourimageName);
    }else{
     imageview.setVisibility(View.GONE);
    }
  return (row);

}  

where your listview_rowlayout.xml is something like this

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical" >

    <ImageView
        android:id="@+id/iv_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParenttop="true"
        android:layout_marginLeft="10dp"
       />

    <TextView
        android:id="@+id/tv_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:textColor="#000000"
        android:layout_alignBottom="@+id/imageView1"
        android:text="" />


</RelativeLayout>

and your main.xml in which u have added listview should be something like this

<?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="fill_parent" >
<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:drawSelectorOnTop="true" />

</RelativeLayout>  

Upvotes: 1

Benito Bertoli
Benito Bertoli

Reputation: 25803

This is a bit tricky because you can't put a listview inside a scrollview but you can make it work if you create a custom adapter which can have different types of rows (e.g. A row with text only, a row with an ImageView, etc.).

Then have your first row in that ListView always be an image.

Check out this tutorial (Section: Different list items’ layouts) to learn how to make different types of rows.

Upvotes: 4

Related Questions