I have the following view Hierarchy:
So Let's say I have the following:
I programmatically inflate my MyCoordinatorLayout as a child of the LinearLayout. So somewhere this happens:
linearLayout.addView(myCoordinatorLayout, ..., ...);
Now I have the following code:
public class MyCoordinatorLayout extends CoordinatorLayout{
public void inflate() {
LayoutInflater.from(getContext()).inflate(R.layout.merge_header_custom_layout, this, true);
WebView newWebView = new WebView(getContext());
addView(newWebView, 0, new ViewGroup.LayoutParams.MATCH_PARENT, MATCH_PARENT);
}
}
public class MyCustomLayout extends LinearLayout{
@Override
public void onAttachedToWindow() {
Log.v(TAG, "onAttachedToWindow() called, now adding swipe behavior"); //This is called as expected!
addSwipeBehavior();
}
private void addSwipeBehavior() {
final SwipeDismissBehavior<View> swipe = new SwipeDismissBehavior<View>();
swipe.setSwipeDirection(SWIPE_DIRECTION_ANY);
swipe.setListener(new SwipeDismissBehavior.OnDismissListener() {
@Override
public void onDismiss(View view) {
Log.v(TAG, "onDismiss called!");
}
@Override
public void onDragStateChanged(int i) {
Log.v(TAG, "onDragStateChanged()");
}
}
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) get LayoutParams();
params.setBehavior(swipe);
Log.v(TAG, "behavior=" + params.getBehavior); //This shows its set.
}
}
Here is the problem. The swipe to dismiss does not work at all. In fact, those log statements I added are never printed out. Is it probably because my CoordinatorLayout was added programmatically, not existing in the XML beforehand like the examples I have seen?
I would really like my behavior to work, but it is just not being triggered. Also, I checked the MyCustomLayout
and saw that its onTouchEvent(MotionEvent e) is triggered.... However, the behavior is still never triggered. I also did a getLayoutParams()
and getBehavior()
and it returns the correct behavior so I know the CoordinatorLayout has the correct Behavior, but the behavior isn't just working at all... Not sure if I missed something! Would be great to get some help or point me in the right direction!