2

Goal

I want to have a list (not a ListView) in a scrollView

Problem

ListView in ScrollView does not work

Possible solution

I think the solution is todo the following:

  1. Create a class that extends AdapterView
  2. Set the class in the XML layout
  3. Fill the adapterView with a adapter.

problems with the solution

If i place the extended AdapterView in my xml layout the app crashes.

Error: 10-04 16:02:14.396: W/ActivityManager(2119): Activity pause timeout for ActivityRecord{422c1838 package/.activities.SelectWorkplaceActivity}

Questions

What is going wrong here?

Are there better ways to create a non-scrollable list with a adapter?

Code

The View:

public class BasicListView extends AdapterView<ListAdapter> {

private String tag = "BasicListView";

private ListAdapter mAdapter;

private DataSetObserver mDataSetObserver;

private Context mContext;

public BasicListView(Context context) {
    super(context);
    mContext = context;

}

public BasicListView(Context context, AttributeSet attrs) {
    super(context, attrs);
    mContext = context;
}

public BasicListView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    mContext = context;
}

@Override
public ListAdapter getAdapter() {
    return mAdapter;
}
@Override
public View getSelectedView() {
    Log.i(tag, "getSelectedView not available");
    return null;
}
@Override
public void setAdapter(ListAdapter adapter) {
    if(mAdapter != null)
    {
        mAdapter.unregisterDataSetObserver(mDataSetObserver);
    }

    mAdapter = adapter; 

    requestLayout();
}

@Override
public void setSelection(int position) {
    Log.i(tag, "setSelection not available");
}
}

And the XML Layout:

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

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2"
     >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/TextView02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Code scannen" />

        <Button
            android:id="@+id/btn_scan_code"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:text="Scan werkplek" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Code handmatig invoeren" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >


            <EditText
                android:id="@+id/et_type_code"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10" >
            </EditText>

            <Button
                android:id="@+id/btn_send_code"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Verzenden" />
        </LinearLayout>

        <TextView
            android:id="@+id/TextView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/label_current_sessions"
            />

        <package.views.BasicListView
            android:id="@+id/current_sessions"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
        </package.views.BasicListView>

        <TextView
            android:id="@+id/TextView02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dip"
            android:text="@string/label_favorite_workplaces"
            />

        <ListView
            android:id="@+id/favorite_workplaces"
            android:layout_width="match_parent"
            android:layout_height="193dp"
            />

    </LinearLayout>
</ScrollView>


<include
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    layout="@layout/actionbar" />

if you want more info please just ask :)

Extranion
  • 588
  • 8
  • 25
  • 4
    solution : don't put a listview in a scrollview. you are not supposed to do that, and it is not supposed to work – njzk2 Oct 04 '12 at 13:48
  • there is always an error. post your logcat output. – njzk2 Oct 04 '12 at 13:49
  • "I want to have a list in a scrollView" is not a goal. it is a technical view of the solution to your goal. which probably is having the top part scroll to give more space to the listview. you can use the headerview in listview for that – njzk2 Oct 04 '12 at 13:50
  • 1
    @njzk2 i know that a listview in a scrollview is not supposed to work, thats why i asked the question. I just want to a have a list (not scrollable) btw i find a error: 10-04 16:02:14.396: W/ActivityManager(2119): Activity pause timeout for ActivityRecord{422c1838 package/.activities.SelectWorkplaceActivity} – Extranion Oct 04 '12 at 14:03
  • Were you able to get this working? I'm also trying to create a non-scrollable list view that extends AdapterView, but I'm having trouble getting the child views to inflate correctly. – murtuza Jul 01 '14 at 13:55

1 Answers1

3

This answer from Romain Guy explains why you should use a LinearLayout rather than a ListView for this purpose: https://stackoverflow.com/a/3496042/1717225

Community
  • 1
  • 1
drewhannay
  • 1,030
  • 6
  • 12