Why ListView cannot be used in a ScrollView?

2019-08-20 00:48发布

问题:

I found many tutorials and examples butWhy ListView cannot be used in a ScrollView?

Only answers is Using a ListView to make it not scroll is extremely expensive and goes against the whole purpose of ListView. resources.

I have following xml file.

.........
<ScrollView 
   android:id="@+id/sv" 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:layout_below="@+id/textView2">
      <ListView 
         android:id="@+id/list"
         android:layout_width="275dp"
         android:layout_height="200dp">
     </ListView>
</ScrollView>
.........

In this xml file, i have already use

wrap_content instead of 200dp height of listview

   ListView lv = (ListView) findViewById(R.id.list);
   lv.setAdapter(new EfficientAdapter(this));

but doesnot work.

What is the alternative scrolling list item for specific height and weight.

But I have try this:

<ListView 
         android:id="@+id/list"
         android:layout_width="275dp"
         android:layout_height="200dp">
     </ListView>

or

<ScrollView android:layout_width="fill_parent" android:id="@+id/sv" 
        android:layout_height="wrap_content"   android:layout_below="@+id/textView2">   
    <LinearLayout android:layout_width="fill_parent"
        android:orientation="vertical"
        android:layout_height="wrap_content">
        <ListView android:id="@+id/list" android:layout_width="275dp"
            android:layout_height="240dp"></ListView>
    </LinearLayout>
    </ScrollView>

Without scollview but scrolling the listitem is ok but quite not smothly. I have try this but could not get more idea behind this.

Edited:

Is it right Listview have inbuild scrolling capabilities. So not necessary to define listview inside the scrollview. It means scrollview in not necessary?

回答1:

The problem is not the ListView but the ScrollView

Those two views need to takes controll of vertical scrolling, if you have a scrollview and a listview in the same layout and the user scroll down, which one have to take the focus and so the scroll?

This is a common problem that has a common and easy solution (this solution comes from Romain Guy from Google Android team!): don't use a listview in a scrollview!

If you want an interesting video/talk you can watch it from Google I/O talks: The world of ListView I really suggest you to take a look :)



回答2:

It's because both views are scrollable. Have you already tried to search/google for it? There are thousand matches..

http://www.google.de/search?sourceid=chrome&ie=UTF-8&q=android+listview+in+scrollview



回答3:

The ListView has built in capabilities to scroll. You don't need to define it again within a ScrollView



回答4:

ListViews are not supposed to get in ScrollView. Even if you hack, and i mean big hack, both views by extending them and got them to scroll , also be itemclickable etc etc , something will break eventually. Also do not add a ListView to a Scroll View.



回答5:

I have tested with below xml and it works. But still trying to handle the onTouchEvent function between the ScrollView and ListView. You can Click here to see the original solution.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:quilt="http://schemas.android.com/apk/res-auto"
    android:id="@+id/scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

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

        <ListView
            android:id="@+id/inner_listview"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

</ScrollView>


回答6:

EDIT: Yes android doc says don't use listview into scrollview because both have same vertically touch gesture. It will make trouble at the time of scrolling item. And also problem with their adapter size that why we are unable to see whole item.

Finally I got best solution and I want share it with all. Please don't use Listview into Scrollview instead of it use LinearLayout and bind your item using View Infaltor.

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

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

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:text="@string/bottom_item"
            android:layout_gravity="center"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginTop="5dp"
            android:text="@string/androidhub4you" />
    </LinearLayout>
</ScrollView>

And in your java class-

 for (int i = 0; i < mArrayListData.size(); i++) {
                     /**
                      * inflate items/ add items in linear layout instead of listview
                      */
                     LayoutInflater inflater = null;
                     inflater = (LayoutInflater) getApplicationContext()
                                  .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                     View mLinearView = inflater.inflate(R.layout.row, null);
                     /**
                      * getting id of row.xml
                      */
                     TextView mFirstName = (TextView) mLinearView
                                  .findViewById(R.id.textViewName);
                     TextView mLastName = (TextView) mLinearView
                                  .findViewById(R.id.textViewLastName);

                     /**
                      * set item into row
                      */
                     final String fName = mArrayListData.get(i).getmFirstName();
                     final String lName = mArrayListData.get(i).getmLastName();
                     mFirstName.setText(fName);
                     mLastName.setText(lName);

                     /**
                      * add view in top linear
                      */

                     mLinearListView.addView(mLinearView);

                     /**
                      * get item row on click
                      *
                      */
                     mLinearView.setOnClickListener(new OnClickListener() {

                           @Override
                           public void onClick(View v) {
                                  // TODO Auto-generated method stub
                                  Toast.makeText(MainActivity.this, "Clicked item;" + fName,
                                                Toast.LENGTH_SHORT).show();
                           }
                     });
              }

Read more: http://www.androidhub4you.com/2014/03/android-listview-into-scrollview-issue.html#ixzz2xc7Ag74u