Skip to content Skip to sidebar Skip to footer

Listview Without Scroll On Android

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

Solution 1:

This is how you should set your listview in a scrollView, but just like somebody else answered, you should never put a listview in a scrollview, especially if the listview will contain a lot of items

ListView lv = (ListView)findViewById(R.id.list);  // your listview inside scrollview

lv.setOnTouchListener(new ListView.OnTouchListener() 
{
    @Override
    public boolean onTouch(View v, MotionEvent event) 
    {
        int action = event.getAction();
        switch (action) 
        {
        case MotionEvent.ACTION_DOWN:
            // Disallow ScrollView to intercept touch events.
            v.getParent().requestDisallowInterceptTouchEvent(true);
            break;

        case MotionEvent.ACTION_UP:
            // Allow ScrollView to intercept touch events.
            v.getParent().requestDisallowInterceptTouchEvent(false);
            break;
        }

        // Handle ListView touch events.
        v.onTouchEvent(event);
        returntrue;
    }
});

Solution 2:

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.

publicclassStaticListViewextendsLinearLayout {
  protected Adapter adapter;
  protectedObserverobserver=newObserver(this);

  publicStaticListView(Context context) {
    super(context);
  }

  publicStaticListView(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

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

  privateclassObserverextendsDataSetObserver {
    StaticListView context;

    publicObserver(StaticListView context) {
      this.context = context;
    }

    @OverridepublicvoidonChanged() {
      List<View> oldViews = newArrayList<View>(context.getChildCount());
      for (inti=0; i < context.getChildCount(); i++)
        oldViews.add(context.getChildAt(i));

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

    @OverridepublicvoidonInvalidated() {
      context.removeAllViews();
      super.onInvalidated();
    }
  }
}

Solution 3:

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

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:layout_centerHorizontal="true"android:text="TextView1" /><TextViewandroid: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" /><ListViewandroid: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>

Solution 4:

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

Post a Comment for "Listview Without Scroll On Android"