2

I have a Listview inside a Scrollview and manually binding its data on the code behind. My main problem is, when I bind my data, my listview isn't expanding its height. I've tried setting it to wrap_content, fill_parent but nothing works. Here is my XML

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/parentScrollview"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<RelativeLayout
>

<LinearLayout >

    <ImageView
        />

    <LinearLayout
         >

        <TextView />

        <TextView />

        <LinearLayout >

            <TextView/>

            <TextView/>
        </LinearLayout>

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

            <TextView/>

            <TextView />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

<View />

<TextView/>

<ScrollView
    android:id="@+id/childScrollView"
    android:layout_width="match_parent"
    android:layout_height="375dp"
    android:layout_below="@+id/view1"
    android:layout_centerHorizontal="true"
    android:layout_marginLeft="10dip"
    android:layout_marginRight="10dip"
    android:layout_marginTop="18dp"
    android:background="@drawable/scrollviewborder"
    android:scrollbarStyle="insideInset" >

    <ListView
        android:id="@+id/listFriends"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="2dip"
        android:paddingLeft="2dip"
        android:paddingRight="2dip"
        android:paddingTop="2dip"
        >
    </ListView>


</ScrollView>

<Button />

</RelativeLayout>

</ScrollView>

Please see that it is a nested ListView and here is my code behind

    ArrayList<HashMap<String, String>> friendList = new ArrayList<HashMap<String, String>>();
    // adding database values to HashMap key => value
    for (int i = 0; i < friendName.length; i++) {
        // creating new HashMap
        HashMap<String, String> friend = new HashMap<String, String>();
            //merchant.put(KEY_ID, merchantIdArray[i]); 
            friend.put(KEY_FRIEND_NAME, friendName[i]);
            friend.put(KEY_THUMB_URL,imageIDs[i]);
        friendList.add(friend);
    }

    list=(ListView)findViewById(R.id.listFriends);
     adapter =new LazyAdapter(this, friendList);        
     list.setAdapter(adapter);

What seems to be the problem?

EDIT Ive added this code for validating which scroll should be used

    parentScrollView= (ScrollView) findViewById(R.id.parentScrollview);
    parentScrollView.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
             //Log.v(TAG,"PARENT TOUCH");
             findViewById(R.id.childScrollView).getParent().requestDisallowInterceptTouchEvent(false);
            return false;
            }
        });
    childScrollView= (ScrollView) findViewById(R.id.childScrollView);
    childScrollView.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
             //Log.v(TAG,"PARENT TOUCH");
             findViewById(R.id.childScrollView).getParent().requestDisallowInterceptTouchEvent(true);
            return false;
        }
    });

The main reason about this is that I will put my Facebook friendlist inside this Listview that it should be scrollable

Anuj Balan
  • 7,629
  • 23
  • 58
  • 92
Androyd
  • 129
  • 1
  • 1
  • 6
  • 1
    You shouldn't put a ListView inside a ScrollView because the ListView class implements its own scrolling and it just doesn't receive gestures because they all are handled by the parent ScrollView. I strongly recommend you to simplify your layout somehow. For example you can add views you want to be scrolled to the ListView as headers or footers. – VendettaDroid Sep 07 '12 at 06:31
  • Any particular reason, why you're wrapping ListView in ScrollView ? – Marek Sebera Sep 07 '12 at 06:32
  • @Marek Sebera The main reason I am wrapping the listview is simply because I want to have a focus on my Listview – Androyd Sep 07 '12 at 06:41

2 Answers2

1

The problem is the following (as stated here by Romain Guy):

Using a ListView to make it not scroll is extremely expensive and goes against the whole purpose of ListView. You should NOT do this. Just use a LinearLayout instead.

Community
  • 1
  • 1
sandrstar
  • 12,503
  • 8
  • 58
  • 65
  • But sir the main purpose why I add listview inside a scrollview is because I want to have a two scrolling on my layout. I need it because I will put my friendlist on that listview. Please see my updated code above – Androyd Sep 07 '12 at 06:49
  • why not to use ExpandableListView then? – sandrstar Sep 07 '12 at 06:52
  • Sir I am thinking of creating a xml wherein I will put the listview and use the fill parent and call it inside my scrollview.Is that possible? – Androyd Sep 07 '12 at 06:55
  • it's similar in some way. It would let You to have 'sectioned' list (and provide different data for different sections). You'll need to have one adapter for it. – sandrstar Sep 07 '12 at 06:57
0

@Androyd Friend its always better if u dont put listview inside scrollView and also not scrollview inside listview, abd then make listview fill parent or fix some hieght in dps like 300dp or that much height u want...

Manish Sharma
  • 217
  • 1
  • 2
  • 11