I have a Gallery whose Adapter creates several LinearLayout instances. Those linear layout instances have buttons, however, and when someone clicks the buttons, they can’t drag the gallery.
My idea is having a menu that the user can scroll through. The kind of thing that would normally be done with a ScrollView, but because I want the scrolled view to ‘Snap’ to the current button pages, a Gallery works better.
This question is similar to this one: Android gallery of LinearLayouts
However, while I’ve fixed the ‘buttons appear clicked’ when dragging issue, I can’t seem to make it work like a ScrollView does, by having the buttons work as part of the dragging area.
Any hints?
Not sure if the code is relevant, but here it is.
Layout that contains the gallery:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Gallery
android:id="@+id/gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:fadingEdge="none"
android:spacing="0dp"/>
</FrameLayout>
The test activity that populates the gallery:
import com.zehfernando.display.widgets.ZGallery;
public class ScrollTestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scrolltest);
Gallery gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new LayoutAdapter(this));
}
public class LayoutAdapter extends BaseAdapter {
private Context mContext;
public LayoutAdapter(Context c) {
mContext = c;
}
public int getCount() {
return 3;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.scrolllayout, null);
v.setMinimumWidth(getWindowManager().getDefaultDisplay().getWidth());
return v;
}
}
}
The layout for the frames that go inside the gallery:
<?xml version="1.0" encoding="utf-8"?>
<com.zehfernando.display.widgets.ScrollableLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/buttonRegister"
android:layout_width="200dp"
android:layout_height="72dp"
android:text="REGISTER"/>
<Button
android:id="@+id/buttonUnregister"
android:layout_width="match_parent"
android:layout_height="72dp"
android:text="UNREGISTER" />
</com.zehfernando.display.widgets.ScrollableLinearLayout>
“ScrollableLinearLayout” is just my class that extends LinearLayout to override onPressed.
Ok, I think I got it, so here it is in case anyone runs into this in the future.
I didn’t know very well how touch events propagated down the display list, so this took a lot more trial and error than I’d like to admit, but basically: one can intercept touch events on a parent and not let it propagate to children, basically turning buttons useless (allowing users to click and drag it, sending the events to the parent’s
onTouchEventinstead). This is done with theonInterceptTouchEventmethod.So instead of having a
Gallery, I’ve extended it (calling it aZGalleryfor now). This is enough to make the contained buttons useless:But of course, I wanted to make sure the buttons worked (that they were clickable), while also allowing drag.
There’s probably a more clever way to do this, but what I do is intercept the touch event on my new Gallery (like above), but allowing it to go through (returning
false) until the user has moved the ‘cursor’ by a given threshold – then I interpret it as a dragging intention, start properly intercepting the touch event. This causes touch events to be sent to my own gallery, working as intended.You can modify it to make it work for vertical or horizontal drags only too.
So anyway, this is a simplified version of a
Galleryclass that allows dragging on any element inside of it:It seems to work well. Hopefully it’ll be helpful to someone else!