2

I want to set listview to show all items without scroll.
Below is my layout:

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

      <TextView
      android:id="@+id/tv1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="Large Text"
      android:textColor="@android:color/black"
      android:textAppearance="?android:attr/textAppearanceLarge" />

      <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

    <TextView
         android:id="@+id/tv2"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="Large Text"
         android:textColor="@android:color/black"
         android:textAppearance="?android:attr/textAppearanceLarge" />

     </LinearLayout>

The linearlayout is belong to a scrollview.
So I want to set the listview all items and scroll by the parent's scroll.
How can I do it?

Hariharan
  • 24,741
  • 6
  • 50
  • 54
brian
  • 6,802
  • 29
  • 83
  • 124

3 Answers3

3

If the reason for this requirement is similar to what I needed, the class below can help. It is a replacement ListView, one that observes an adapter and its changes and reacts accordingly, but uses a manually populated LinearLayout under the hood, actually. I guess I found this code somewhere on the net but I couldn't locate it now in order to link to it.

My requirements were to use the advantages of a ListView, namely its adapter functionality, to display a very few items (1 to 5) on a page, with possibly more than one such replacement ListView on a single screen. Once displayed, the elements themselves become part of the larger page, so they aren't scrolled separately inside their own ListView, rather with the page as a whole.

public class StaticListView extends LinearLayout {
  protected Adapter adapter;
  protected Observer observer = new Observer(this);

  public StaticListView(Context context) {
    super(context);
  }

  public StaticListView(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public void setAdapter(Adapter adapter) {
    if (this.adapter != null)
      this.adapter.unregisterDataSetObserver(observer);
    this.adapter = adapter;
    adapter.registerDataSetObserver(observer);
    observer.onChanged();
  }

  private class Observer extends DataSetObserver {
    StaticListView context;

    public Observer(StaticListView context) {
      this.context = context;
    }

    @Override
    public void onChanged() {
      List<View> oldViews = new ArrayList<View>(context.getChildCount());
      for (int i = 0; i < context.getChildCount(); i++)
        oldViews.add(context.getChildAt(i));

      Iterator<View> iter = oldViews.iterator();
      context.removeAllViews();
      for (int i = 0; i < context.adapter.getCount(); i++) {
        View convertView = iter.hasNext() ? iter.next() : null;
        context.addView(context.adapter.getView(i, convertView, context));
      }
      super.onChanged();
    }

    @Override
    public void onInvalidated() {
      context.removeAllViews();
      super.onInvalidated();
    }
  }
}
Gábor
  • 9,466
  • 3
  • 65
  • 79
2

Do not put listview inside a scrollview.

http://developer.android.com/reference/android/widget/ScrollView.html

Quoting from docs You should never use a ScrollView with a ListView, because ListView takes care of its own vertical scrolling. Most importantly, doing this defeats all of the important optimizations in ListView for dealing with large lists, since it effectively forces the ListView to display its entire list of items to fill up the infinite container supplied by ScrollView.

You can add your textview's as a header and footer to listview.

http://developer.android.com/reference/android/widget/ListView.html

Check the header and footer methods in the above link

You can have a relative layout add textviews at the top and bottom. Relative to the textviews have the listview between the textview's

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="TextView1" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignParentBottom="true"
        android:text="TextView1" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/textView1"
        android:layout_above="@+id/textView2"
       >

    </ListView>

</RelativeLayout>
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • Using headers/footers works well for simple layouts, but becomes hell if you want to put stateful fields such as edittext in the header/footer – craigrs84 Jul 04 '14 at 05:12
2

I think it is not possible. If we override list view scroll behavior using parent layout scroll,list view did not work properly.

diordna
  • 550
  • 5
  • 14