当我尝试将自定义视图添加到ScrollView的子(RelativeLayout)时,我遇到了ScrollView的问题。 删除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>
注意: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;
}
}
}
}
}
答案 0 :(得分: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>