0

當我嘗試將自定義視圖添加到ScrollView的子項(RelativeLayout)時,我遇到了ScrollView問題。 當我刪除scrollView時,它可以正常工作。Android:向ScrollView的子項添加自定義視圖

這裏的XML文件

<ScrollView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:clickable="true" 
     android:id="@+id/scrollView"> 


    <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_below="@+id/relativeLayout" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" 
      android:layout_alignRight="@+id/relativeLayout" 
      android:layout_alignEnd="@+id/relativeLayout" 
      android:id="@+id/rlSelectable"> 
     <View android:layout_width="match_parent" 
       android:layout_height="120dp" 
       android:id="@+id/view1" 
       android:background="@drawable/drawable_border" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true"/> 
     <View android:layout_width="match_parent" 
       android:layout_height="120dp" 
       android:id="@+id/view2" 
       android:background="@drawable/drawable_border" 
       android:layout_below="@+id/view1" android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true"/> 
     <View android:layout_width="match_parent" 
       android:layout_height="120dp" 
       android:id="@+id/view3" 
       android:background="@drawable/drawable_border" 
       android:layout_below="@+id/view2" android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true"/> 
     <View android:layout_width="match_parent" 
       android:layout_height="120dp" 
       android:id="@+id/view4" 
       android:background="@drawable/drawable_border" 
       android:layout_below="@+id/view3" android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true"/> 
     <View android:layout_width="match_parent" 
       android:layout_height="120dp" 
       android:id="@+id/view5" 
       android:background="@drawable/drawable_border" 
       android:layout_below="@+id/view4" android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true"/> 
     <View android:layout_width="match_parent" 
       android:layout_height="120dp" 
       android:id="@+id/view6" 
       android:background="@drawable/drawable_border" 
       android:layout_below="@+id/view5" android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true"/> 
     <View android:layout_width="match_parent" 
       android:layout_height="120dp" 
       android:id="@+id/view7" 
       android:background="@drawable/drawable_border" 
       android:layout_below="@+id/view6" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true"/> 
     <View android:layout_width="match_parent" 
       android:layout_height="120dp" 
       android:id="@+id/view8" 
       android:background="@drawable/drawable_border" 
       android:layout_below="@+id/view7" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true"/> 
     <View android:layout_width="match_parent" 
       android:layout_height="120dp" 
       android:id="@+id/view9" 
       android:background="@drawable/drawable_border" 
       android:layout_below="@+id/view8" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true"/> 
     <View android:layout_width="match_parent" 
       android:layout_height="120dp" 
       android:id="@+id/view10" 
       android:background="@drawable/drawable_border" 
       android:layout_below="@+id/view9" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true"/> 
     <View android:layout_width="match_parent" 
       android:layout_height="120dp" 
       android:id="@+id/view11" 
       android:background="@drawable/drawable_border" 
       android:layout_below="@+id/view10" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true"/> 
     <View android:layout_width="match_parent" 
       android:layout_height="120dp" 
       android:id="@+id/view12" 
       android:background="@drawable/drawable_border" 
       android:layout_below="@+id/view11" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true"/> 
     <View android:layout_width="match_parent" 
       android:layout_height="120dp" 
       android:id="@+id/view13" 
       android:background="@drawable/drawable_border" 
       android:layout_below="@+id/view12" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true"/> 
     <View android:layout_width="match_parent" 
       android:layout_height="120dp" 
       android:id="@+id/view14" 
       android:background="@drawable/drawable_border" 
       android:layout_below="@+id/view13" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true"/> 
     <View android:layout_width="match_parent" 
       android:layout_height="120dp" 
       android:id="@+id/view15" 
       android:background="@drawable/drawable_border" 
       android:layout_below="@+id/view14" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true"/> 
     <View android:layout_width="match_parent" 
       android:layout_height="120dp" 
       android:id="@+id/view16" 
       android:background="@drawable/drawable_border" 
       android:layout_below="@+id/view6" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true"/> 
    </RelativeLayout> 

</ScrollView> 

注:滾動型是不是在XML佈局父。它被包裹在RelativeLayout中。

這裏的MainActivity代碼:

public class MainActivity extends AppCompatActivity implements Dragger.DraggerInterface { 

GestureDetector detector; 
ArrayList<Dragger> draggers; 
RelativeLayout rlSelectable; 
int width,height,count=0,startsAt=0; 
float centerX; 
ScrollView scrollView; 
String TAG = "MAIN_DRAGGER"; 

View view; 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.calendar_view); 

    view = findViewById(R.id.view1); 
    rlSelectable = (RelativeLayout) findViewById(R.id.rlSelectable); 
    draggers = new ArrayList<Dragger>(); 
    scrollView = (ScrollView)findViewById(R.id.scrollView); 
    scrollView.requestDisallowInterceptTouchEvent(true); 
    detector = new GestureDetector(new GestureDetector.SimpleOnGestureListener(){ 
     @Override 
     public boolean onDown(MotionEvent e) { 
      return true; 
     } 

     @Override 
     public boolean onSingleTapConfirmed(MotionEvent e) { 
      int index = MotionEventCompat.getActionIndex(e); 
      float x = MotionEventCompat.getX(e, index); 
      float y = MotionEventCompat.getY(e, index); 
      addDragger(centerX,y); 
      log("single tapped"); 
      return true; 
     } 
    }); 




    rlSelectable.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      return detector.onTouchEvent(event); 
     } 
    }); 
} 


@Override 
public void onWindowFocusChanged(boolean hasFocus) { 
    super.onWindowFocusChanged(hasFocus); 
    width = rlSelectable.getWidth(); 
    centerX = (rlSelectable.getRight()-rlSelectable.getLeft())/2; 
    height = view.getHeight(); 
    startsAt = view.getTop(); 
} 


public void addDragger(float x,float y) { 
    Dragger dragger = new Dragger(MainActivity.this); 
    dragger.setParams(width,height); 
    dragger.setCenter(x, y); 
    draggers.add(dragger); 
    dragger.setId(count++); 
    dragger.viewStartsAt(startsAt); 
    dragger.addView(this); 
    rlSelectable.addView(dragger); 

} 




private void log(String message){ 
    Log.d(TAG,message); 
} 

@Override 
public void checkBounds(Dragger current) { 
    int currentId = current.getId(); 

    if(draggers.size()>1){ 
     for(Dragger dragger : draggers){ 

      if(currentId !=dragger.getId()){ 
       if(current.getUpState() && current.getRectTop()<dragger.getRectBootom() && current.getRectBootom() > dragger.getRectTop()){ 
        dragger.setBottom(current.getRectBootom()); 
        current.removeView(); 
        draggers.remove(current); 
        break; 
       }else if(!current.getUpState() && current.getRectBootom() > dragger.getRectTop() && current.getRectTop() < dragger.getRectBootom()){ 
        dragger.setTop(current.getRectTop()); 
        current.removeView(); 
        draggers.remove(current); 
        break; 
       } 
      } 
     } 
    } 
} 
+1

我想是有了滾動WRAP_CONTENT和match_parent了滾動的孩子應該是相反的,你已經實現了什麼與您的代碼的主要問題。嘗試這樣做!具有相對佈局高度的wrap_content。 – himanshu1496

+0

你能解釋更多關於你的問題,就像你的應用程序崩潰或你的滾動視圖不會滾動,... –

+0

@PhanVănLinh當我嘗試添加自定義視圖,我創建只有相對佈局沒有scrollview作爲其父,addview是工作,但當我嘗試添加它作爲父母的scrollview它不會將自定義視圖添加到relativelayout。 – Rujul1993

回答

0
<?xml version="1.0" encoding="utf-8"?> <FrameLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/frame_container"> <include 
     android:id="@+id/app_bar_id" 
     layout="@layout/app_bar"></include> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 

     android:id="@+id/fragment_container_id"> 

    </LinearLayout> 

    <ScrollView 

    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:clickable="true" 
    android:id="@+id/scrollView"> 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 



    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/relativeLayout" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignRight="@+id/relativeLayout" 
     android:layout_alignEnd="@+id/relativeLayout" 
     android:id="@+id/rlSelectable"> 
     <View android:layout_width="match_parent" 
      android:layout_height="120dp" 
      android:id="@+id/view1" 
      android:background="@drawable/edit_text_bottom_corner_curved" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true"/> 
     <View android:layout_width="match_parent" 
      android:layout_height="120dp" 
      android:id="@+id/view2" 
      android:background="@drawable/edit_text_bottom_corner_curved" 
      android:layout_below="@+id/view1" android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true"/> 
     <View android:layout_width="match_parent" 
      android:layout_height="120dp" 
      android:id="@+id/view3" 
      android:background="@drawable/edit_text_bottom_corner_curved" 
      android:layout_below="@+id/view2" android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true"/> 
     <View android:layout_width="match_parent" 
      android:layout_height="120dp" 
      android:id="@+id/view4" 
      android:background="@drawable/edit_text_bottom_corner_curved" 
      android:layout_below="@+id/view3" android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true"/> 
     <View android:layout_width="match_parent" 
      android:layout_height="120dp" 
      android:id="@+id/view5" 
      android:background="@drawable/edit_text_bottom_corner_curved" 
      android:layout_below="@+id/view4" android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true"/> 
     <View android:layout_width="match_parent" 
      android:layout_height="120dp" 
      android:id="@+id/view6" 
      android:background="@drawable/edit_text_bottom_corner_curved" 
      android:layout_below="@+id/view5" android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true"/> 
     <View android:layout_width="match_parent" 
      android:layout_height="120dp" 
      android:id="@+id/view7" 
      android:background="@drawable/edit_text_bottom_corner_curved" 
      android:layout_below="@+id/view6" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true"/> 
     <View android:layout_width="match_parent" 
      android:layout_height="120dp" 
      android:id="@+id/view8" 
      android:background="@drawable/edit_text_bottom_corner_curved" 
      android:layout_below="@+id/view7" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true"/> 
     <View android:layout_width="match_parent" 
      android:layout_height="120dp" 
      android:id="@+id/view9" 
      android:background="@drawable/edit_text_bottom_corner_curved" 
      android:layout_below="@+id/view8" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true"/> 
     <View android:layout_width="match_parent" 
      android:layout_height="120dp" 
      android:id="@+id/view10" 
      android:background="@drawable/edit_text_bottom_corner_curved" 
      android:layout_below="@+id/view9" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true"/> 
     <View android:layout_width="match_parent" 
      android:layout_height="120dp" 
      android:id="@+id/view11" 
      android:background="@drawable/edit_text_bottom_corner_curved" 
      android:layout_below="@+id/view10" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true"/> 
     <View android:layout_width="match_parent" 
      android:layout_height="120dp" 
      android:id="@+id/view12" 
      android:background="@drawable/edit_text_bottom_corner_curved" 
      android:layout_below="@+id/view11" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true"/> 
     <View android:layout_width="match_parent" 
      android:layout_height="120dp" 
      android:id="@+id/view13" 
      android:background="@drawable/edit_text_bottom_corner_curved" 
      android:layout_below="@+id/view12" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true"/> 
     <View android:layout_width="match_parent" 
      android:layout_height="120dp" 
      android:id="@+id/view14" 
      android:background="@drawable/edit_text_bottom_corner_curved" 
      android:layout_below="@+id/view13" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true"/> 
     <View android:layout_width="match_parent" 
      android:layout_height="120dp" 
      android:id="@+id/view15" 
      android:background="@drawable/edit_text_bottom_corner_curved" 
      android:layout_below="@+id/view14" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true"/> 
     <View android:layout_width="match_parent" 
      android:layout_height="120dp" 
      android:id="@+id/view16" 
      android:background="@drawable/edit_text_bottom_corner_curved" 
      android:layout_below="@+id/view6" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true"/> 
    </RelativeLayout> 
    </LinearLayout> </ScrollView> 
    </FrameLayout> 
+0

此解決方案不起作用 – Rujul1993

+0

您得到了什麼錯誤,我試着在我的設備上工作。 –

+0

其空引用nullpointerexception布爾android.graphics.RectF.contains(float,float)'在一個空對象引用 – Rujul1993