Android Scrollview仅在Listview中工作

Android Scrollview仅在Listview中工作,android,xml,listview,scrollview,Android,Xml,Listview,Scrollview,我在我的应用程序中定义了以下布局: <?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

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

<?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获得滚动条时,问题就出现了,因此您只能滚动此列表。布局的其余部分固定在屏幕高度上


如何使整个布局可滚动?提前感谢。

除非定义ListView不可滚动,否则不能将ListView放置在ScrollView中。如果您想这样做,您应该创建一个自定义的ListView,如下所示

    public class NonScrollListView extends ListViewCompat {

//Define a public constructor
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int heightMeasureSpec_custom = View.MeasureSpec.makeMeasureSpec(
                Integer.MAX_VALUE >> 2, View.MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();
    }
}

除非定义ListView不可滚动,否则不能将ListView放置在ScrollView中。如果您想这样做,您应该创建一个自定义的ListView,如下所示

    public class NonScrollListView extends ListViewCompat {

//Define a public constructor
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int heightMeasureSpec_custom = View.MeasureSpec.makeMeasureSpec(
                Integer.MAX_VALUE >> 2, View.MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();
    }
}

您不应该在ScroolView中使用ListView,它有自己的滚动条,它们相互干扰。 我建议创建一个列表适配器,使用switch语句从数组中创建所有不同的视图:

    class MyAdapter extends ArrayAdapter{

    public MyAdapter(Context context, int resource, List objects) {
        super(context, resource, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
         ViewItem viewType = getItem(position);

        switch(viewType.type){
            case TEXTVIEW: convertView = LayoutInflater.from(getContext()).inflate(R.layout.textView1, parent, false);

                break;
            case LISTITEM: convertView = LayoutInflater.from(getContext()).inflate(R.layout.listItem, parent, false);

                break;
        }


        return convertView;
    }


}

您不应该在ScroolView中使用ListView,它有自己的滚动条,它们相互干扰。 我建议创建一个列表适配器,使用switch语句从数组中创建所有不同的视图:

    class MyAdapter extends ArrayAdapter{

    public MyAdapter(Context context, int resource, List objects) {
        super(context, resource, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
         ViewItem viewType = getItem(position);

        switch(viewType.type){
            case TEXTVIEW: convertView = LayoutInflater.from(getContext()).inflate(R.layout.textView1, parent, false);

                break;
            case LISTITEM: convertView = LayoutInflater.from(getContext()).inflate(R.layout.listItem, parent, false);

                break;
        }


        return convertView;
    }


}

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

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();
}
}
公共类test1扩展活动{
...
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test1);
...
//这里首先设置列表视图,而不是根据子项设置调用集列表视图高度。。。
根据子项(您的列表视图名称)设置列表视图高度;
...
}
///方法基于子对象设置列表视图高度
公共静态无效集\u列表\u视图\u高度\u基于\u子项(ListView ListView){
ListAdapter ListAdapter=listView.getAdapter();
如果(listAdapter==null)
返回;
int desiredWidth=View.MeasureSpec.makeMeasureSpec(listView.getWidth(),View.MeasureSpec.UNSPECIFIED);
int totalHeight=0;
视图=空;
对于(int i=0;i
我正在处理你的问题。不能直接在滚动视图中使用列表视图。如果你想使用它,那么你必须给高度实用的列表视图,它为我工作

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();
}
}
公共类test1扩展活动{
...
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test1);
...
//这里首先设置列表视图,而不是根据子项设置调用集列表视图高度。。。
根据子项(您的列表视图名称)设置列表视图高度;
...
}
///方法基于子对象设置列表视图高度
公共静态无效集\u列表\u视图\u高度\u基于\u子项(ListView ListView){
ListAdapter ListAdapter=listView.getAdapter();
如果(listAdapter==null)
返回;
int desiredWidth=View.MeasureSpec.makeMeasureSpec(listView.getWidth(),View.MeasureSpec.UNSPECIFIED);
int totalHeight=0;
视图=空;
对于(int i=0;i
ListView
有自己的滚动条。
ListView
有自己的滚动条。