1

I want to create a layout like this:

enter image description here

I think the ScrollView is necessary, maybe like this:

<RelativeLayout>
    <RelativeLayout id="fixed">
    </RelativeLayout>
    <ScrollView>
        <RelativeLayout>
            <Button...
            <ListView....
            <Button...
            </RelatievLayout>
    </ScrollView>
</RelativeLayout>

But it seems that add a ListView inside a ScrollView is not good idea.

Any idea to make it?

BTW, there are not only Button1 and Button2 outside the listview, there are more views, so I do not think add the views as foot or head is a good idea.

hguser
  • 35,079
  • 54
  • 159
  • 293

2 Answers2

1

This is xml code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout android:id="@+id/rlt"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >


        </LinearLayout>
    </ScrollView>



</RelativeLayout>

Java code

LinearLayout llt = (LinearLayout)findViewById(R.id.rlt);
    for(int i=0;i<12;i++)
    {
        final int k =i;
        LinearLayout ll = new LinearLayout(this);
        ll.setId(i);
        ll.setOrientation(LinearLayout.VERTICAL);
        TextView tv = new TextView(this);
        tv.setText(" name:"+i);
        ll.addView(tv);
        ll.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                // TODO Auto-generated method stub
                System.out.println("This is the printed text no"+k);
            }
        });
        //ll.setBackgroundColor(Color.BLACK);
    llt.addView(ll);
     }

You can create your own ListView like this

krishna
  • 4,069
  • 2
  • 29
  • 56
0

First idea (not recommanded): Adding a ListView inside a ScrollView is a bad idea, because both are scrollable by default. You could use a LinearLayout instead of the ListView and create a custom onClickListener for the items.

<Scrollview>
   <Button.../>
   <LinearLayout>
     <items.../>
   </LinearLayout>
</Scrollview>

Second idea (recommanded) is to go with one ListView and create a custom Adapter extending BaseAdapter returning the Views for each row and holding the data. This means adding your Buttons and Items to the ListView and create a custom OnClickListener and Adapter to hold you Model. This Is the way I have used and I would do it again this way.

I your Adapter, you will need to Override:

getViewTypeCount - number of types of different row-layouts.

getItemViewType the type of the row.

getView returning the view for a position, here you need to inflate the layout and set the data.

This is a related question how to create a ListView with different types of rows.

Community
  • 1
  • 1
Simulant
  • 19,190
  • 8
  • 63
  • 98
  • In fact, I am using the second idea, I have my own adapter which extends `ArrayAdapter`,and I only override the `getView` method, I never use the `getViewTypeCount` and `getItemViewType`, I will have a try. But, the `button1` and `button2` in my example have nothing to do with the `listView`, so add them in the custom adapter is a good idea? – hguser Nov 19 '13 at 12:31
  • in this way the buttons are inside the scrollable are. Mixing different content is okay, if your adapter handles it correct. – Simulant Nov 19 '13 at 13:23