2016-07-28 76 views
1

你好,我是Android開發新手,我已經閱讀了各種線程,談到ListView不應該在Scrollview之內,並且需要以線性或相對佈局封裝。CustomScrollView中的ListView崩潰

但我有一個這樣的佈局

<com.itmind.spac.spacapp.custom_extends.CustomScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/scrollViewreceipt" 
> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 

    <include 
    android:id="@+id/include1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    layout="@layout/toolbar" /> 

    <Spinner 
     android:id="@+id/customers_spinner" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="20dp" 
     android:theme="@android:style/Theme.Holo.Light.DarkActionBar" 
     /> 
    <Spinner 
     android:id="@+id/venues_spinner" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="20dp" 
     android:theme="@android:style/Theme.Holo.Light.DarkActionBar" 
     /> 
    <Spinner 
     android:id="@+id/workgroup_spinner" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="20dp" 
     android:theme="@android:style/Theme.Holo.Light.DarkActionBar" 
     /> 

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
      android:minHeight="150dp"> 
    <ListView 
     android:id="@+id/activityList" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginTop="15dp" 
     android:minHeight="150dp" 
     /> 
    </LinearLayout > 

    <android.gesture.GestureOverlayView 
     android:id="@+id/signaturePad" 
     android:layout_width="match_parent" 
     android:layout_height="80dp" 
     android:layout_weight="5" 
     android:background="#d3d3d3" 
     android:eventsInterceptionEnabled="true" 
     android:fadeEnabled="false" 
     android:gestureColor="#333" 
     android:gestureStrokeLengthThreshold="0.1" 
     android:gestureStrokeType="multiple" 
     android:fadeOffset="5000" 
     android:orientation="vertical" > 
    </android.gesture.GestureOverlayView> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="testImage" 
     android:text="testami"/> 
</LinearLayout> 

這是我custoScrollView

public class CustomScrollView extends ScrollView { 

private boolean enableScrolling = true; 

public boolean isEnableScrolling() { 
    return enableScrolling; 
} 

public void setEnableScrolling(boolean enableScrolling) { 
    this.enableScrolling = enableScrolling; 
} 

public CustomScrollView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
} 

public CustomScrollView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

public CustomScrollView(Context context) { 
    super(context); 
} 

@Override 
public boolean onInterceptTouchEvent(MotionEvent ev) { 

    if (isEnableScrolling()) { 
     return super.onInterceptTouchEvent(ev); 
    } else { 
     return false; 
    } 
} 
@Override 
public boolean onTouchEvent(MotionEvent ev) { 
    if (isEnableScrolling()) { 
     return super.onTouchEvent(ev); 
    } else { 
     return false; 
    } 
} 
} 

我需要customclass滾動視圖東陽我可以設置允許滾動,當我在

所以在我的活動中實現了GestureOverlayView.OnGestureListener我有:

@Override 
public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) { 
    myScrollView.setEnableScrolling(false); 
} 
@Override 
public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) { 
    myScrollView.setEnableScrolling(true); 
} 

這麼多元素,我需要scrollView查看所有元素。 在我的scrollView內我有一個listView,我用db從數據檢索中填充。

我的問題是,我的Listview崩潰,顯示一行,我看到並滾動在一行。

我試過min-height但不起作用,如果我把佈局高度放在Listview,我不能滾動這個列表。

我該怎麼做? 我想在一個長頁面中查看列表,所以我必須使用scrollview或有解決方案嗎?您 的

+0

爲什麼使用自定義滾動視圖類?粘貼其代碼。 –

+0

你好,謝謝你,我使用customScrollview爲 我編輯和粘貼我的課程。 – LorenzoBerti

回答

0

如下定製ListView類中添加代碼,將根據其內部ScrollView內容讓你Listview的高度。使用它而不是ListView在您的xml佈局文件中使用您的CustomScrollView

public class CustomListView extends ListView { 

// private boolean expanded; 

    public CustomListView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public CustomListView(Context context) { 
     super(context); 
    } 

    public CustomListView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, 
       MeasureSpec.AT_MOST); 
     super.onMeasure(widthMeasureSpec, expandSpec); 
    } 
} 

希望這會幫助你。

注: -按照Android開發者網站的鏈接this,我們不應該使用滾動型有一個ListView,因爲ListView控件需要其自身的垂直滾動的照顧。

+0

Thankyou是的,它的工作,它固定動態高度。 我已經閱讀了關於ScrollView的Listview,但如果我有我的數據庫100結果,如何管理沒有listView和ScrollView,所以視圖太長? – LorenzoBerti

+1

您可以使用CoordinatorLayout進行嵌套滾動。 https://developer.android.com/reference/android/support/design/widget/CoordinatorLayout.html –

+0

完成了! :)好吧,我會學習CoordinatorLayout – LorenzoBerti

1

修復高度像

<ListView 
    android:id="@+id/activityList" 
    android:layout_width="match_parent" 
    android:layout_height="250dp" 
    android:layout_marginTop="15dp" 
    /> 

,並在您的活動文件

listView.setOnTouchListener(new View.OnTouchListener() { 
     // Setting on Touch Listener for handling the touch inside ScrollView 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      // Disallow the touch request for parent scroll on touch of child view 
      v.getParent().requestDisallowInterceptTouchEvent(true); 
      return false; 
     } 
    }); 
+0

你好,謝謝,但是如果我修復我的listView高度滾動上的ListView不工作,我想我的listView滾動 – LorenzoBerti

+0

Thankyou,是的,ListView不滾動,即使我把ScrollView和固定高度設置ListView。 – LorenzoBerti