Make Linearlayout scrollable without using Scrollv

2019-05-31 01:11发布

I have a Linearlayout and I want to make it scrollable without using ScrollView. Is it possible. Any suggestions will be appreciated. Here's the detail: If I wrap the LinearLayout using ScrollView, it is ok but when I used a ListView inside LinearLayout(because it is my clients requirement), it said do not use ListView inside ScrollView. I have to show 50 product list using ListView and I have to put this ListView inside LinearLayout and at the same time whole layout will be scrollable. Is it possible. Here is the skeleton:

<LinearLayout>
  <RelativeLayout>
    <LinearLayout> 
        <TextView>
    </LinearLayout>
    <LinearLayout>
        <TextView>
    </LinearLayout>
    <LinearLayout>
        <TextView>
    </LinearLayout>
    <LinearLayout>
        <TextView>
    </LinearLayout>
    <LinearLayout>
        <TextView>
    </LinearLayout>
    <LinearLayout>
        <TextView>
    </LinearLayout>
    <LinearLayout>
        <TextView>
    </LinearLayout>
    <LinearLayout>
        <TextView>
    </LinearLayout>
    <LinearLayout>
        <TextView>
    </LinearLayout>
    <LinearLayout>
        <TextView>
    </LinearLayout>
    <LinearLayout>
        <TextView>
    </LinearLayout>
    <LinearLayout>
        <TextView>
    </LinearLayout>
    <LinearLayout>
        <TextView>
    </LinearLayout>
    <LinearLayout>
        <TextView>
    </LinearLayout>
    <LinearLayout>
        <ListView>
    </LinearLayout> 
  </RelativeLayout>
</LinearLayout>

Important: See the ListView where I want to add 50 list item. So how can I make this total LinearLayout scrollable.

2条回答
你好瞎i
2楼-- · 2019-05-31 01:46

Actually after doing some research, I come up with a solution for this problem:

At first I want to explain the problem in a very simple way.

  1. LinearLayout will be scrollable. To do this we can use ScrollView but sometimes we need to use ListView inside LinearLayout.
  2. We know that inside ScrollView we cannot use another scrollview like ListView

How to solve that?

ListView is scrollable inherently so we can add header and footer in the ListView. As a conclusion:

  1. Create Layout header.xml and footer.xml and list.xml
  2. Find the ListView reference from list.xml in the main activity and dynamically add header and footer in the ListView reference.
查看更多
Root(大扎)
3楼-- · 2019-05-31 01:53

If you are only using a listview inside a linear layout, then you don't need to use scrollview.Because ListView is scrollable by default.But if you have other components as well then you can separate those in another scrollview.Make sure that ScrollView only uses one direct child layout. Below is a sample code.

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

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="#b5b5b5"
        android:dividerHeight="1dp"
        android:cacheColorHint="#00000000"/> 

</LinearLayout>
查看更多
登录 后发表回答