2017-07-31 50 views
3

我有自己的定製ScrollView我的layout,其中包含一個單一FrameLayout和所有其他View放在它裏面的主要View。它看起來像這樣(這是sm_layout.xml):的Java返回「滾動型只能承載一個直接子」的時候,只有一個直接子

<com.effeleven.utils.CustomScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/scrollView" 
    android:background="@color/white" 
    android:clickable="true"> 

    <FrameLayout...> 

</com.effeleven.utils.CustomScrollView> 

我沒有這個例外,當我用默認的Android ScrollView拋出。現在我需要一個自定義的,因爲我需要處理ScrollView(我有一個mapFragment和,我不能取代任何東西,因爲我遵循設計模式)內的滾動事件。什麼可能導致異常,因爲我的CustomScrollView只包含一個FrameLayout,就像前面的ScrollView一樣?

這是我CustomScrollView類:

public class CustomScrollView extends ScrollView { 

public CustomScrollView(Context context) { 
    super(context); 
    LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    layoutInflater.inflate(R.layout.sm_layout, this, true); 
} 

@Override 
public boolean onInterceptTouchEvent(MotionEvent ev) { 
    final int action = ev.getAction(); 
    switch (action) 
    { 
     case MotionEvent.ACTION_DOWN: 
      super.onTouchEvent(ev); 
      break; 

     case MotionEvent.ACTION_MOVE: 
      return false; 

     case MotionEvent.ACTION_CANCEL: 
      super.onTouchEvent(ev); 
      break; 

     case MotionEvent.ACTION_UP: 
      return false; 

    } 

    return false; 
} 

@Override 
public boolean onTouchEvent(MotionEvent ev) { 
    super.onTouchEvent(ev); 
    return true; 
} 
} 
+0

發佈您的'sm_layout.xml' – DeKaNszn

+0

@DeKaNszn sm_layout.xml是我上面發佈的一個。 –

回答

2

閱讀ScrollView

,允許放置在它是 滾動視圖層次結構的圖組。滾動視圖可能只有一個直接的孩子在其中放置 。

原因

layoutInflater.inflate(R.layout.sm_layout, this, true); 

您已經包括多個XML屬性爲滾動型的孩子。您持有sm_layout.xml。您應從類別中刪除此項。

+1

這確實是問題的原因。謝謝。我現在接受哪個答案?兩者基本相同並解決問題。 –

+0

@ G.Rann如你所願。我在這裏分享知識。 –

2

在你CustomScrollView你是包括使用LayoutInflater另一個佈局,這是造成你的問題。嘗試評論佈局,看看它是否有效。

+0

是的。這是問題所在,因爲我正在以這種方式添加另一個佈局。謝謝。 –

+1

如果有幫助,不要忘記upvote和接受答案 –

+0

我做了upvote,但我沒有足夠的聲譽,所以你不能看到upvote :) –

相關問題