Android, How to add ScrollView into screen which has some list items?

大憨熊 提交于 2020-01-10 04:24:26

问题


in my activity, i have three lists. after running the application, the screen is not scroll-able. i can scroll the list which is on top of others but i can't scroll the whole of page. I tried to add ScrollView into my xml layout but lint says that "The vertically scrolling ScrollView should not contain another vertically scrolling widget (ListView)".

How to make my screen scrollable?


回答1:


Try this way

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

    <LinearLayout android:id="@+id/GlobalLayout" 
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:orientation="vertical" >

        <ListView android:id="@+id/ListView1"
                  android:choiceMode="multipleChoice"
                  android:layout_height="100dip"
                  android:layout_width="fill_parent" />
       <ListView android:id="@+id/ListView2"
                  android:choiceMode="multipleChoice"
                  android:layout_height="100dip"
                  android:layout_width="fill_parent" />
       <ListView android:id="@+id/ListView3"
                  android:choiceMode="multipleChoice"
                  android:layout_height="100dip"
                  android:layout_width="fill_parent" />

    </LinearLayout>

</ScrollView>



回答2:


<ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

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

            <ListView
                android:id="@+id/listView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1" >
            </ListView>

        </LinearLayout>

    </ScrollView>

Scrollview supports one child layout so make the listview inside the scroll so you will get what you want.




回答3:


Use this method and your listview become scrollable inside scrollview:-

   ListView lstNewsOffer.setAdapter(new ViewOfferAdapter(
                            ViewNewsDetail.this, viewOfferList));
                    getListViewSize(lstNewsOffer);

void getListViewSize(ListView myListView) {
    ListAdapter myListAdapter = myListView.getAdapter();
    if (myListAdapter == null) {
        // do nothing return null
        return;
    }
    // set listAdapter in loop for getting final size
    int totalHeight = 0;
    for (int size = 0; size < myListAdapter.getCount(); size++) {
        View listItem = myListAdapter.getView(size, null, myListView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }
    // setting listview item in adapter
    ViewGroup.LayoutParams params = myListView.getLayoutParams();
    params.height = totalHeight
            + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
    myListView.setLayoutParams(params);
    // print height of adapter on log
    Log.i("height of listItem:", String.valueOf(totalHeight));
}



回答4:


It doesn't work because android don't know with which view he should scroll, so putting a listview in a scrollview its not a bright idea. try to use visibilty attribute, use to HIDE the view that you finich to work with and set VISIBLE the new. or use one ListView and try to populate and remove items after finishing and starting an instruction. hope that help you




回答5:


What you can do is that you can add header and footer into your list view by inflating them. Create three xml layououts and add them in listview using header and footer.

    View headerView = View.inflate(this, R.layout.layout_name, null);
    lv.addHeaderView(headerView);

use it before setting adapter. lv is listview.



来源:https://stackoverflow.com/questions/10496804/android-how-to-add-scrollview-into-screen-which-has-some-list-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!