2015-07-20 116 views
0

我曾尝试向上滚动ListView,当事件项目比ListView的空间多但它失败时。我想知道是否可以在不添加任何侦听器的情况下使ListView可滚动。谢谢。ListView无法滚动

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" 
android:clickable="true" 
android:focusableInTouchMode="true" 
tools:context="com.example.ssycorpapp.SecondActivity" > 

<TabHost 
    android:id="@+id/tabhost2" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" > 
     </TabWidget> 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" > 

      <RelativeLayout 
      android:id="@+id/events" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" > 

       <SearchView 
        android:id="@+id/search" 
        android:queryHint="@string/hint" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" > 
       </SearchView> 

       <RelativeLayout 
        android:id="@+id/titleBar" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="20dp" 
        android:layout_marginRight="20dp" 
        android:layout_marginTop="20dp" 
        android:layout_below="@id/search" > 

        <TextView 
         android:id="@+id/resultName" 
         android:layout_width="180dp" 
         android:layout_height="wrap_content" 
         android:layout_alignParentLeft="true" 
         android:layout_alignParentStart="true" 
         android:text="@string/event" 
         android:textAppearance="?android:attr/textAppearanceMedium" /> 

        <TextView 
         android:id="@+id/resultDate" 
         android:layout_width="80dp" 
         android:layout_height="wrap_content" 
         android:layout_toLeftOf="@+id/resultTime" 
         android:layout_toStartOf="@+id/resultTime" 
         android:text="@string/eventDate" 
         android:textAppearance="?android:attr/textAppearanceMedium" /> 

        <TextView 
         android:id="@id/resultTime" 
         android:layout_width="40dp" 
         android:layout_height="wrap_content" 
         android:layout_alignParentRight="true" 
         android:layout_alignParentEnd="true" 
         android:text="@string/eventTime" 
         android:textAppearance="?android:attr/textAppearanceMedium" /> 

       </RelativeLayout> 

       <ListView 
        android:id="@+id/event_list" 
        android:scrollbars="vertical" 
        android:fastScrollEnabled="true" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="20dp" 
        android:layout_marginRight="20dp" 
        android:layout_below="@id/titleBar" 
        android:layout_above="@+id/DelAll" /> 

       <Button 
        android:id="@+id/DelAll" 
        style="?android:attr/buttonStyleSmall" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="10dp" 
        android:layout_marginStart="10dp" 
        android:layout_marginBottom="10dp" 
        android:layout_alignParentBottom="true" 
        android:text="@string/DelAll" /> 

      </RelativeLayout> 

      <ScrollView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" > 

       <LinearLayout 
        android:id="@+id/set" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="vertical" > 

        <TextView 
         android:id="@+id/EventName" 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" 
         android:text="@string/event" /> 

        <EditText 
         android:id="@+id/eveName" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:ems="10" 
         android:inputType="text" > 
        </EditText> 

        <TextView 
         android:id="@+id/EventDate" 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" 
         android:text="@string/eventDate" /> 

        <DatePicker 
         android:id="@+id/datePicker1" 
         android:layout_width="match_parent" 
         android:layout_height="158dp" /> 

        <TextView 
         android:id="@+id/EventTime" 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" 
         android:text="@string/eventTime" /> 

        <TimePicker 
         android:id="@+id/timePicker1" 
         android:layout_width="match_parent" 
         android:layout_height="109dp" /> 

        <Button 
         android:id="@+id/Tim" 
         style="?android:attr/buttonStyleSmall" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="@string/Set" /> 

       </LinearLayout> 

      </ScrollView> 

     </FrameLayout> 

    </LinearLayout> 

</TabHost> 

</LinearLayout> 
+0

不要一次使用ListView和ScrollView – Nabin

+0

@Nabin即使ListView和ScrollView在不同的标签中? – shisushi

回答

1

如果你不希望使用任何监听器,那么你可以简单地使用这样一个自定义的ListView,

public class CustomListView extends ListView implements View.OnTouchListener, AbsListView.OnScrollListener { 

    private static final int MAXIMUM_LIST_ITEMS_VIEWABLE = 99; 
    private int listViewTouchAction; 

    public CustomListView (Context context, AttributeSet attrs) { 
     super(context, attrs); 
     listViewTouchAction = -1; 
     setOnScrollListener(this); 
     setOnTouchListener(this); 
    } 

    @Override 
    public void onScroll(AbsListView view, int firstVisibleItem, 
         int visibleItemCount, int totalItemCount) { 
     if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) { 
      if (listViewTouchAction == MotionEvent.ACTION_MOVE) { 
       scrollBy(0, -1); 
      } 
     } 
    } 

    @Override 
    public void onScrollStateChanged(AbsListView view, int scrollState) { 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

     int newHeight = 0; 
     final int heightMode = MeasureSpec.getMode(heightMeasureSpec); 
     int heightSize = MeasureSpec.getSize(heightMeasureSpec); 
     if (heightMode != MeasureSpec.EXACTLY) { 
      ListAdapter listAdapter = getAdapter(); 
      if (listAdapter != null && !listAdapter.isEmpty()) { 
       int listPosition = 0; 
       for (listPosition = 0; listPosition < listAdapter.getCount() 
         && listPosition < MAXIMUM_LIST_ITEMS_VIEWABLE; listPosition++) { 
        View listItem = listAdapter.getView(listPosition, null, this); 
        //now it will not throw a NPE if listItem is a ViewGroup instance 
        if (listItem instanceof ViewGroup) { 
         listItem.setLayoutParams(new LayoutParams(
           LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
        } 
        listItem.measure(widthMeasureSpec, heightMeasureSpec); 
        newHeight += listItem.getMeasuredHeight(); 
       } 
       newHeight += getDividerHeight() * listPosition; 
      } 
      if ((heightMode == MeasureSpec.AT_MOST) && (newHeight > heightSize)) { 
       if (newHeight > heightSize) { 
        newHeight = heightSize; 
       } 
      } 
     } else { 
      newHeight = getMeasuredHeight(); 
     } 
     setMeasuredDimension(getMeasuredWidth(), newHeight); 
    } 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) { 
      if (listViewTouchAction == MotionEvent.ACTION_MOVE) { 
       scrollBy(0, 1); 
      } 
     } 
     return false; 
    } 
} 

在布局后方可使用,

<CustomListView 
    android:id="@+id/event_list" 
    android:scrollbars="vertical" 
    android:fastScrollEnabled="true" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="20dp" 
    android:layout_marginRight="20dp" 
    android:layout_below="@id/titleBar" 
    android:layout_above="@+id/DelAll" /> 
+0

谢谢。但有没有解决方案,我可以添加或编辑.xml文件中的东西? – shisushi

+0

我猜不。如果您在ScrollView中使用ListView,那么只需使用xml标记就没有其他方法来防止这种情况。 –

+0

但ListView不在ScrollView中,它们在不同的标签中分开 – shisushi