简体   繁体   English

android中ScrollView里面的ListView

[英]ListView inside ScrollView in android

I'm having the listview inside the scrollview when I'm running the app list is scrolling limited to the first item.我在该listviewscrollview ,当我运行的应用程序列表滚动限制的第一个项目。 I think the Scrillview is overriding scrolling of listview here is my code:我认为 Scrillview 正在覆盖列表视图的滚动,这是我的代码:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.7"
    android:padding="10dp">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <Button
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:id="@+id/client_detail"
                android:text="CLIENT INFORMATION"
                android:textColor="@color/colorPrimaryDark"
                android:background="@drawable/login_selector"/>

                <RelativeLayout
                    android:id="@+id/rl1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:visibility="gone"
                    android:layout_below="@+id/client_detail">

                    /* HERE IS MY SOME OTHER LAYOUT */

                </RelativeLayout>
                <Button
                    android:text="CONTACT DETAILS"
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:background="@drawable/login_selector"
                    android:textColor="@color/colorPrimaryDark"
                    android:layout_below="@+id/rl1"
                    android:layout_marginTop="10dp"
                    android:layout_centerHorizontal="true"
                    android:id="@+id/contactDetails" />

                <ListView
                    android:id="@+id/listView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/contactDetails"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="10dp" />

        </RelativeLayout>
    </ScrollView>    
</RelativeLayout>

Please need some help, thanks in advance!请需要一些帮助,提前致谢!

You need to use ExpandableHeightListView.您需要使用 ExpandableHeightListView。

https://github.com/paolorotolo/ExpandableHeightListView https://github.com/paolorotolo/ExpandableHeightListView

when you use ListView with ScrollView , only one row of the ListView will be visible, so you have to set the height of ListView pro-grammatically depending on the elements inside ListView .You can make a function like this-当您将ListViewScrollView一起使用时,只有一行ListView可见,因此您必须根据ListView的元素以编程方式设置 ListView 的高度。您可以制作这样的功能 -

  public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        return;
    }
    int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.AT_MOST);
    int totalHeight = 0;
    View view = null;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        view = listAdapter.getView(i, view, listView);
        if (i == 0) {
            view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
        }
        view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
        totalHeight += view.getMeasuredHeight();
    }
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();
}

Then you use this method on the listview created-然后你在创建的列表视图上使用这个方法 -

  HourAdapter adapter1=new HourAdapter(getActivity(),day,start,end,x);
                    listview.setAdapter(adapter1);
                    setListViewHeightBasedOnChildren(listview);

It works even if the rows of the ListView have variable height即使ListView的行具有可变高度,它也能工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM