Scrollview仅适用于Listview

时间:2016-02-10 20:28:05

标签: 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: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获取滚动条时出现问题,因此您只能滚动此列表。剩下的部分是固定在屏幕高度上的。

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

3 个答案:

答案 0 :(得分:2)

我正在解决你的问题。您不能直接在滚动视图中使用列表视图。如果你想使用它,那么你必须以实用的方式为列表视图提供高度,它对我有用。

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();
}
}

答案 1 :(得分:0)

除非您定义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();
    }
}

答案 2 :(得分:0)

你不应该在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;
    }


}