scrollview与listview无法正常工作

时间:2017-10-18 07:23:57

标签: android listview scrollview

我在滚动视图中只包含一个直接子项,并且我在java文件中添加了以下代码。当我删除Utility方法时,listview完全显示,但不能作为scrollview

        ScrollView sv_fragment_globe;
        sv_fragment_globe = (ScrollView)rowView.findViewById(R.id.sv_fragment_globe);
        sv_fragment_globe.smoothScrollTo(0,0);
        Utility.setListViewHeightBasedOnChildren(holder.lv_globe);

    

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/sv_fragment_globe"
        android:fillViewport="true"
        >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:orientation="vertical"
            >

            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="200dp"
                android:src="@drawable/home_hospital1"
                android:id="@+id/iv_vp_globe"
                android:scaleType="fitXY"
                />


            <ListView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/lv_globe"
                >


            </ListView>

    </LinearLayout>

</ScrollView>



</android.support.v4.widget.SwipeRefreshLayout>

2 个答案:

答案 0 :(得分:0)

试试这个

    <android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipe_refresh_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
   <ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="1"
        >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:src="@android:drawable/home_hospital"
            android:id="@+id/iv_vp_globe"
            android:scaleType="fitXY"
            />


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


        </ListView>
    </LinearLayout>
    </ScrollView>
    </android.support.v4.widget.SwipeRefreshLayout>

答案 1 :(得分:0)

如果在任何布局中有两个以上的滚动视图,则父滚动视图将仅滚动。按照以下步骤操作,允许孩子的滚动元素滚动。

第1步: 创建自定义ListView。

public class ExpandableHeightListView扩展了ListView {

boolean expanded = false;

public ExpandableHeightListView(Context context)
{
    super(context);
}

public ExpandableHeightListView(Context context, AttributeSet attrs)
{
    super(context, attrs);
}

public ExpandableHeightListView(Context context, AttributeSet attrs,int defStyle)
{
    super(context, attrs, defStyle);
}

public boolean isExpanded()
{
    return expanded;
}

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    // HACK! TAKE THAT ANDROID!
    if (isExpanded())
    {
        // Calculate entire height by providing a very large height hint.
        // But do not use the highest 2 bits of this integer; those are
        // reserved for the MeasureSpec mode.
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);

        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();
    }
    else
    {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

public void setExpanded(boolean expanded)
{
    this.expanded = expanded;
}

}

步骤2:初始化Listview对象后添加以下行。

lv_selected_time_zone.setExpanded(真);