當我嘗試將自定義視圖添加到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;
}
}
}
}
}
我想是有了滾動WRAP_CONTENT和match_parent了滾動的孩子應該是相反的,你已經實現了什麼與您的代碼的主要問題。嘗試這樣做!具有相對佈局高度的wrap_content。 – himanshu1496
你能解釋更多關於你的問題,就像你的應用程序崩潰或你的滾動視圖不會滾動,... –
@PhanVănLinh當我嘗試添加自定義視圖,我創建只有相對佈局沒有scrollview作爲其父,addview是工作,但當我嘗試添加它作爲父母的scrollview它不會將自定義視圖添加到relativelayout。 – Rujul1993