2015-07-20 4 views
0

J'ai essayé de faire défiler ListView vers le haut lorsque l'élément d'événement est plus que l'espace de ListView, mais il échoue. Je voudrais savoir si je peux faire défiler le ListView sans ajouter d'écouteur. Je vous remercie.ListView ne peut pas faire défiler

<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

Ne pas utiliser ListView et ScrollView à la fois – Nabin

+0

@Nabin Même si ListView et ScrollView se trouvent dans des onglets différents? – shisushi

Répondre

1

Si vous ne souhaitez pas utiliser tout auditeur, vous pouvez simplement utiliser un ListView personnalisé comme celui-ci,

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

Ensuite, utilisez dans votre mise en page,

<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

Merci. Mais y a-t-il une solution que je peux simplement ajouter ou modifier quelque chose dans le fichier .xml? – shisushi

+0

Je suppose que non. Si vous utilisez un ListView dans un ScrollView, il n'y a pas d'autre moyen d'empêcher cela en utilisant simplement une balise xml. –

+0

Mais ListView est en dehors de ScrollView et ils sont séparés dans différents onglets – shisushi