1

I am developing an android App.

I places LinearLayout on ListView. I would like to scroll LinearLayout united with ListView.

My xml is below.

Could you tell me how to implement above?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/header1"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_margin="3dp"
        android:choiceMode="singleChoice"
        android:orientation="vertical" >

    ・・・

    </LinearLayout>

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:choiceMode="singleChoice" />

</LinearLayout>
supermonkey
  • 631
  • 11
  • 25

3 Answers3

1

You should not put a ListView inside a ScrollView due to it being extremely expensive and defeats the purpose a ListView. Use a LinearLayout/RelativeLayout instead.

If you would like to populate the ListView items with a LinearLayout you will need to create an adapter that will get the data and populate the listView.

Here is some code that takes String values and displays them in the list.

public class MySimpleArrayAdapter extends ArrayAdapter<String> {
  private final Context context;
  private final String[] values;

  public MySimpleArrayAdapter(Context context, String[] values) {
    super(context, R.layout.rowlayout, values);
    this.context = context;
    this.values = values;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.label);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
    textView.setText(values[position]);
    // change the icon for Windows and iPhone
    String s = values[position];
    if (s.startsWith("iPhone")) {
      imageView.setImageResource(R.drawable.no);
    } else {
      imageView.setImageResource(R.drawable.ok);
    }

    return rowView;
  }
} 

Where you inflate the rowlayout in the getView - This will be your LinearLayout.

http://www.vogella.com/tutorials/AndroidListView/article.html#androidlists_adapterintro - For more information.

0

Use ScrollView (example) :

<ScrollView 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"

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <LinearLayout
                android:id="@+id/header1"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_margin="3dp"
                android:choiceMode="singleChoice"
                android:orientation="vertical" >

・・・

        </LinearLayout>

        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:choiceMode="singleChoice" />

        </LinearLayout>
    </RelativeLayout>
</ScrollView>

Remember that ScrollView can only have one child attribute, so you can f.e. use a Relative Layout to encapsulate LinearLayout and ListView

A Honey Bustard
  • 3,433
  • 2
  • 22
  • 38
0

ListView has its own ScrollView, If you want to scroll LinearLayout then follow the this code.

  • Declare Scroll-view as parent and declare only one child Linearlayout inside it (Scrollview contain only one child)
  • Declare what ever you want inside the Linearlayout.

    <ScrollView 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"> 
    <LinearLayout
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:orientation="vertical" >
    
    <LinearLayout
        android:id="@+id/header1"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_margin="3dp"
        android:choiceMode="singleChoice"
        android:orientation="vertical" >
        .
        .
        .
    
    </LinearLayout>
    
    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:choiceMode="singleChoice" />
    </LinearLayout>
    </ScrollView>
    
Hanan
  • 444
  • 4
  • 16