Listview不在scrollview中滚动

时间:2014-09-25 09:44:49

标签: java android listview scrollview

我在scrollview中有一个listview。这是我的xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff" >

<ScrollView
    android:id="@+id/scrollView2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/viewtop"
    android:layout_marginBottom="10dp"
    android:fadingEdgeLength="0dp"
    android:fillViewport="true"
    android:overScrollMode="never"
    android:scrollbars="none" >

    <RelativeLayout
        android:id="@+id/rel111"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp" >

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:padding="8dip"
            android:text="@string/franchise"
            android:textSize="25dp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView3"
            android:layout_marginLeft="33dp"
            android:layout_marginTop="10dp"
            android:padding="8dip"
            android:text="@string/neworder"
            android:textSize="20dp" />

        <TextView
            android:id="@+id/textView77"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/ettesttube"
            android:layout_below="@+id/input_order"
            android:layout_marginTop="5dp"
            android:text="Tube Count"
            android:textSize="15dp" />

        <ImageButton
            android:id="@+id/ibtdown"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_alignBottom="@+id/ettesttube"
            android:layout_alignLeft="@+id/input_order"
            android:layout_alignTop="@+id/ettesttube"
            android:layout_below="@+id/textView77"
            android:background="@drawable/down" />

        <EditText
            android:id="@+id/ettesttube"
            android:layout_width="80dp"
            android:layout_height="40dp"
            android:layout_below="@+id/textView77"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="3dp"
            android:layout_toRightOf="@+id/ibtdown"
            android:background="@drawable/squarecorner"
            android:ems="10"
            android:inputType="number"
            android:text="1" >
        </EditText>

        <ImageButton
            android:id="@+id/ibtup"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_alignBottom="@+id/ettesttube"
            android:layout_alignTop="@+id/ettesttube"
            android:layout_toRightOf="@+id/ettesttube"
            android:background="@drawable/up" />


        <RelativeLayout
            android:id="@+id/rellist"
            android:layout_width="fill_parent"
            android:layout_height="200dp"
            android:layout_below="@+id/textView6"
            android:layout_marginBottom="6dp"
            android:layout_marginLeft="35dp"
            android:layout_marginRight="40dp"
            android:background="@drawable/roundcorner" >

            <ListView
                android:id="@+id/listView1"
                android:layout_width="fill_parent"
                android:layout_height="200dp"
                android:layout_weight="1"
                android:fadeScrollbars="false"
                android:fastScrollEnabled="true"
                android:longClickable="true" >
            </ListView>
        </RelativeLayout>

        <Button
            android:id="@+id/button_submit"
            android:layout_width="80dp"
            android:layout_height="35dp"
            android:layout_alignLeft="@+id/rellist"
            android:layout_below="@+id/rellist"
            android:layout_marginTop="6dp"
            android:background="@drawable/btn_bg"
            android:text="Submit" />

        <Button
            android:id="@+id/button_no_order"
            android:layout_width="80dp"
            android:layout_height="35dp"
            android:layout_alignBottom="@+id/button_clear"
            android:layout_alignTop="@+id/button_clear"
            android:layout_below="@+id/rellist"
            android:layout_gravity="center"
            android:layout_toRightOf="@+id/button_clear"
            android:background="@drawable/btn_bg"
            android:text="No Order" />

        <View
            android:id="@+id/blank"
            android:layout_width="fill_parent"
            android:layout_height="30dp"
            android:layout_below="@+id/button_clear" />

        <ImageView
            android:id="@+id/showImg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="35dp"
            android:src="@drawable/abc_ab_bottom_solid_dark_holo"
            android:visibility="gone" />
    </RelativeLayout>
</ScrollView>
</RelativeLayout>

问题是Listview没有滚动

我试过了:

public class Utility {
public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter(); 
    if (listAdapter == null) {
        // pre-condition
        return;
    }

    int totalHeight = 0;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
}
}    

在活动类中,我在设置适配器后写了这个:

Utility.setListViewHeightBasedOnChildren(list);

但仍然列表不滚动。

谢谢!

1 个答案:

答案 0 :(得分:1)

    ListView listView;
listView.setOnTouchListener(new ListView.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE)
{
listView.scrollBy(0, 1);
}
return false;
}
});

检查此代码

ListView lv = (ListView)findViewById(R.id.myListView);  
 lv.setOnTouchListener(new ListView.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();
        switch (action) {
        case MotionEvent.ACTION_DOWN:
            // Disallow ScrollView to intercept touch events.
            v.getParent().requestDisallowInterceptTouchEvent(true);
            break;

        case MotionEvent.ACTION_UP:
            // Allow ScrollView to intercept touch events.
            v.getParent().requestDisallowInterceptTouchEvent(false);
            break;
        }

        // Handle ListView touch events.
        v.onTouchEvent(event);
        return true;
    }
});

如果对您有用,请接受答案。