I am making an app that requires a large png image to be able to scroll with Checkbox buttons laid on top of the image that will scroll with it. I got help from this link Scrollview with many buttons over an image. Here is what I have so far.
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/svVertical"
android:layout_below="@+id/buildingNameText"
android:layout_alignParentStart="true"
android:layout_marginBottom="60dp" >
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/hsvHorizontal" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:isScrollContainer="true">
<ImageView
android:id="@+id/map_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/dummy_floor_extra_large"
android:scaleType="centerCrop"/>
<CheckBox
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/checkBox"
android:button="@null"
android:background="?android:attr/listChoiceIndicatorMultiple"
android:layout_marginStart="21dp"
android:backgroundTint="#22e300"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="15dp" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkBox2"
android:layout_below="@+id/checkBox"
android:layout_alignStart="@+id/checkBox"
android:layout_marginStart="8dp"
android:buttonTint="#22e300" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioButton"
android:layout_below="@+id/checkBox2"
android:layout_alignStart="@+id/checkBox2"
android:layout_marginTop="7dp"
android:buttonTint="#22e300" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New RadioButton"
android:id="@+id/radioButton2"
android:layout_below="@+id/radioButton"
android:layout_toEndOf="@+id/checkBox"
android:layout_marginStart="150dp"
android:layout_marginTop="58dp" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New CheckBox"
android:id="@+id/checkBox3"
android:layout_below="@+id/radioButton2"
android:layout_alignStart="@+id/radioButton2"
android:layout_marginTop="32dp" />
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Switch"
android:id="@+id/switch1"
android:layout_below="@+id/checkBox3"
android:layout_alignStart="@+id/checkBox3"
android:layout_marginTop="42dp" />
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/switch2"
android:layout_below="@+id/radioButton"
android:layout_marginTop="7dp"
android:layout_alignStart="@+id/radioButton" />
<Button
android:layout_width="50dp"
android:layout_height="45dp"
android:id="@+id/button"
android:layout_above="@+id/checkBox3"
android:layout_toStartOf="@+id/radioButton2" />
</RelativeLayout>
</HorizontalScrollView>
</ScrollView>
And here's the activity:
private ScrollView scrollY;
private HorizontalScrollView scrollYChild;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_floor_view);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.parseColor("#505050"));
}
scrollY = (ScrollView) findViewById(R.id.svVertical);
scrollYChild = (HorizontalScrollView) findViewById(R.id.hsvHorizontal);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
scrollYChild.dispatchTouchEvent(event);
//scrollY.onTouchEvent(event);
return super.dispatchTouchEvent(event);
}
The problem is the checkboxes aren't clicking properly. As a matter of fact, all the buttons aren't working. I put all those buttons in the xml file for test purposes to see which one works and the only one that works is the switches. All the rest don't work properly but I would really prefer checkboxes rather than switches in my app. Any help is appreciated! Thank you.
Just a quick note, I commented out scrollY.onTouchEvent(event); because it was making the scrolling aspect very jumpy and awkward. Commenting it out fixed the jumping and made the scrolling smooth.
Aucun commentaire:
Enregistrer un commentaire