1

Hi i'm trying to add a relative view to a merge adapter but it's currently scrolling separetly to the lists so does anyone know how i can add a relative layout with an image view and a text view to a merge adapter?

my aim is to have it look like this

header(relativelyout red bar and title);
List
header(relativelyout red bar and title);
List
header(relativelyout red bar and title);
List

and to have this all scroll as if it was all one list

heres my attempt so far

 arrayAdapter = new ArrayAdapter<String>(this, R.layout.single_item, newsList);
    arrayAdapter2 = new ArrayAdapter<String>(this, R.layout.single_item, newsList2);
    arrayAdapter3 = new ArrayAdapter<String>(this, R.layout.single_item, newsList3);


        ListView list = getListView();
           list.setTextFilterEnabled(true);

           LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE );
            View header = inflater.inflate( R.layout.redcell, list, false);


    setListAdapter (arrayAdapter);


        adapter = new MergeAdapter();
        adapter.addView(header);
        adapter.addAdapter(arrayAdapter);
        adapter.addView(header);
        adapter.addAdapter(arrayAdapter2);
        adapter.addView(header);
        adapter.addAdapter(arrayAdapter3);
        setListAdapter(adapter);

    }   

redcell.xml

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



    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:src="@drawable/titlerect" />

    <TextView
        android:id="@+id/redheadertext"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:gravity="left"
        android:paddingLeft="15dp"
        android:paddingTop="3dp"
        android:textColor="@color/white"
        android:textSize="15dp"
        android:textStyle="bold"/>




</RelativeLayout>
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
Luke Batley
  • 2,384
  • 10
  • 44
  • 76

2 Answers2

2

Instead of creating them in the program, create an xml file for that view, inflate it, then add it to the adapter.

Like this:

LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View header1 = inflater.inflate(R.layout.redbar_title);
View header2 = inflater.inflate(R.layout.redbar_title);
View header3 = inflater.inflate(R.layout.redbar_title);

adapter = new MergeAdapter();
adapter.addView(header1);
adapter.addAdapter(arrayAdapter);
adapter.addView(header2);
adapter.addAdapter(arrayAdapter2);
adapter.addView(header3);
adapter.addAdapter(arrayAdapter3);
setListAdapter(adapter);
Barak
  • 16,318
  • 9
  • 52
  • 84
  • this makes sense however i'm get an error in eclippse undder the getLayoutInflator its teling me to create a local variable for it – Luke Batley May 15 '12 at 15:05
  • Oh, you need to declare an instance of the LayoutInflater. Edited my answer. Take a look. – Barak May 15 '12 at 15:14
  • hi i have done this and it's still scrolling the headers separatly to the list so the header bar dissappears and reappears snaps to top etc i have amended the code above hopefuly it's a bit clearer because i have no idea whats causing it. also only one header is showing – Luke Batley May 15 '12 at 15:31
  • Did you get rid of this line `setListAdapter (arrayAdapter);`? It might be causing you issues... setting two adapters to the same list... – Barak May 15 '12 at 15:35
  • thank you thats the effect i'm wanting only problem i'm having now is that header2 is appearing below the second list instead of above – Luke Batley May 15 '12 at 15:50
  • Did you add them in the proper order to the MergeAdapter? That's the only reason I can think of for that to happen. – Barak May 15 '12 at 15:54
  • yeah i did which is why i'm confused as to why it could do it – Luke Batley May 15 '12 at 16:06
  • Maybe it's confused since you add the same view three times. Try creating them like in my answer (1, 2, and 3) and see if that makes any difference (I have no clue why it would, but I'm grasping at straws now). If nothing else post it as a new question (it is a new/different issue after all) and maybe it will catch Commonware's attention and he can help you out. – Barak May 15 '12 at 16:13
  • i figured it out it was the way i was creating my arrayadapters was adding a list twice to one of them so one list had two items and the other none. thank you so much for your help your a legend. been trying to get that for weeks – Luke Batley May 15 '12 at 16:31
0

Set each list layout_height to wrap_content, and wrap everything within a ScrollView.

Perhaps it would be easier if you could make your headers part of the adapter itself, as separators, and use a single list instead of 3.

K-ballo
  • 80,396
  • 20
  • 159
  • 169
  • it won't let me do that because it doesn't like lists inside scrollviews because a list already scrolls – Luke Batley May 15 '12 at 14:48
  • What do you mean by "it won't let me do that"? If you set the height to `wrap_content` then the list would do no scrolling as it would be large enough to display all its items. – K-ballo May 15 '12 at 14:50