【问题标题】:scrollview with listview not working带有列表视图的滚动视图不起作用
【发布时间】:2017-10-18 07:23:57
【问题描述】:

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

        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>

【问题讨论】:

  • 嵌套可滚动视图是一种反模式。您很可能会遇到滚动冲突问题

标签: android listview scrollview


【解决方案1】:

如果您在任何布局内有两个以上的滚动视图,则父滚动视图只会滚动。按照下面的步骤允许孩子的滚动元素滚动。

第 1 步: 创建自定义 ListView。

公共类 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(true);

【讨论】:

    【解决方案2】:

    试试这个

        <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>
    

    【讨论】:

    • 整个列表视图效果很好,但滚动视图不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-09
    • 1970-01-01
    相关资源
    最近更新 更多