滚动型只适用于列表视图 [英] Scrollview only Works in Listview

查看:175
本文介绍了滚动型只适用于列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序定义了以下布局:

I have the following layout defined in my app:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/disco_photo"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:scaleType="centerCrop" />

        <TextView
            android:id="@+id/disco_name"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_below="@id/disco_photo"
            android:layout_alignParentLeft="true"
            android:layout_gravity="left"
            android:layout_margin="8dp"
            android:gravity="end"
            android:textSize="20sp"
            android:textStyle="italic" />

        <TextView
            android:id="@+id/disco_times"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_below="@id/disco_name"
            android:layout_alignParentLeft="true"
            android:layout_gravity="left"
            android:layout_margin="8dp"
            android:gravity="end"
            android:textSize="20sp"
            android:textStyle="italic"
            android:visibility="gone" />

        <ListView
            android:id="@+id/disco_parties"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/disco_times"
            android:layout_alignParentLeft="true"
            android:layout_gravity="left"
            android:layout_margin="8dp"
            android:text="@string/map_view"
            android:gravity="end"
            android:textSize="20sp"
            android:textStyle="italic" />

        <RatingBar
            android:id="@+id/disco_rating"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_below="@id/disco_parties"
            android:numStars="5"
            android:stepSize="1.0"
            android:rating="3.0"
            android:visibility="gone" />

        <TextView
            android:id="@+id/disco_map"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_below="@id/disco_rating"
            android:layout_alignParentLeft="true"
            android:layout_gravity="left"
            android:layout_margin="8dp"
            android:text="@string/map_view"
            android:gravity="end"
            android:textSize="20sp"
            android:textStyle="italic" />

    </RelativeLayout>
</ScrollView>

我想获得滚动整个布局,但当时只有ListView中越来越滚动条,所以你只能滚动此列表中的问题来了。该layour的其余部分被固定到屏幕高度。

I want to get scroll in the whole layout but the problems comes when only ListView is getting the scrollbar, so you can only scroll this list. The rest of the layour is fixed to the screen height.

我怎样才能使整个布局是滚动?先谢谢了。

How can I make the whole layout be scrollable? Thanks in advance.

推荐答案

我要通过你的问题。你不能使用滚动视图直接在列表视图。如果你想使用它,那么你必须给高度务实列表视图,它为我工作。

I am going through your problem. You can not use list view inside the scroll-view directly. If you want to use it, then you have to give height pragmatically to list view and it work for me.

public class test1 extends Activity {
...
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test1);
...

//here 1st set your list view than call  set_List_View_Height_Based_On_Children...

set_List_View_Height_Based_On_Children(your_Listview_name); 
...
}
///method set_List_View_Height_Based_On_Children
public static void set_List_View_Height_Based_On_Children(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null)
        return;

    int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
    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();
}
}

这篇关于滚动型只适用于列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆