Skip to content

Commit d913d93

Browse files
committed
Try out workaround code by @florianPOLARSTEPS #105
1 parent d01b2fa commit d913d93

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

sample/src/main/java/me/henrytao/smoothappbarlayoutdemo/activity/SmoothScrollExitUntilCollapsedActivity.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,32 @@
2424
import android.view.LayoutInflater;
2525
import android.view.View;
2626
import android.view.ViewGroup;
27+
import android.widget.Button;
28+
import android.widget.Toast;
2729

2830
import butterknife.Bind;
2931
import butterknife.ButterKnife;
3032
import me.henrytao.recyclerview.SimpleRecyclerViewAdapter;
3133
import me.henrytao.recyclerview.holder.HeaderHolder;
34+
import me.henrytao.smoothappbarlayout.SmoothAppBarLayout;
3235
import me.henrytao.smoothappbarlayoutdemo.R;
3336
import me.henrytao.smoothappbarlayoutdemo.apdater.DynamicAdapter;
3437
import me.henrytao.smoothappbarlayoutdemo.util.Utils;
3538

3639
public class SmoothScrollExitUntilCollapsedActivity extends BaseActivity {
3740

41+
@Bind(R.id.btn_action)
42+
Button vBtnAction;
43+
3844
@Bind(R.id.recycler_view_target)
3945
RecyclerView vRecyclerView;
4046

4147
@Bind(R.id.toolbar)
4248
Toolbar vToolbar;
4349

50+
@Bind(R.id.smooth_app_bar_layout)
51+
SmoothAppBarLayout vSmoothAppBarLayout;
52+
4453
private DynamicAdapter<String> mAdapter;
4554

4655
@Override
@@ -85,5 +94,19 @@ public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
8594
}
8695
});
8796
itemTouchHelper.attachToRecyclerView(vRecyclerView);
97+
98+
vBtnAction.setOnClickListener(new View.OnClickListener() {
99+
@Override
100+
public void onClick(View v) {
101+
Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_SHORT).show();
102+
}
103+
});
104+
105+
vSmoothAppBarLayout.setOnClickListener(new View.OnClickListener() {
106+
@Override
107+
public void onClick(View v) {
108+
Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_SHORT).show();
109+
}
110+
});
88111
}
89112
}

sample/src/main/res/layout/activity_smooth_scroll_exit_until_collapsed.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@
4242
app:layout_collapseMode="pin"
4343
app:navigationIcon="@drawable/ic_menu_arrow_back"
4444
style="@style/AppStyle.MdToolbar" />
45+
46+
<Button
47+
android:id="@+id/btn_action"
48+
android:layout_width="wrap_content"
49+
android:layout_height="wrap_content"
50+
android:text="Click me" />
4551
</android.support.design.widget.CollapsingToolbarLayout>
4652
</me.henrytao.smoothappbarlayout.SmoothAppBarLayout>
4753
</android.support.design.widget.CoordinatorLayout>

smooth-app-bar-layout/src/main/java/me/henrytao/smoothappbarlayout/SmoothAppBarLayout.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import android.support.v7.widget.RecyclerView;
2929
import android.util.AttributeSet;
3030
import android.util.Log;
31+
import android.view.MotionEvent;
3132
import android.view.View;
3233
import android.view.ViewGroup;
3334
import android.view.animation.Interpolator;
@@ -54,6 +55,8 @@ public class SmoothAppBarLayout extends AppBarLayout {
5455

5556
private static final String ARG_SUPER = "ARG_SUPER";
5657

58+
private static final int CUSTOM_EDGE_FLAG = 2023477;
59+
5760
public static boolean DEBUG = false;
5861

5962
protected final List<WeakReference<OnOffsetChangedListener>> mOffsetChangedListeners = new ArrayList<>();
@@ -208,6 +211,32 @@ protected Parcelable onSaveInstanceState() {
208211
return bundle;
209212
}
210213

214+
@Override
215+
public boolean dispatchTouchEvent(MotionEvent ev) {
216+
217+
// We need to check wether we have arrived from our own triggered motion dispatch,
218+
// There are no really appropriate fields on MotionEvents to store custom data, so we abuse edgeflags
219+
if (ev.getEdgeFlags() == CUSTOM_EDGE_FLAG) {
220+
return false;
221+
}
222+
223+
boolean dispatched = super.dispatchTouchEvent(ev);
224+
if (dispatched && ev.getAction() == MotionEvent.ACTION_MOVE) {
225+
// After we know some view in our hierarchy would want to receive the move touch event, we don't want it to have though,
226+
// we create a new motion event which will cancel our current motion event stream and will be disregarded by appbarlayout,
227+
// so CoordinatorLayout.Behaviour can receive the new motion event stream
228+
MotionEvent motionEvent = MotionEvent.obtain(ev);
229+
motionEvent.offsetLocation(getLeft(), getTop());
230+
motionEvent.setAction(MotionEvent.ACTION_DOWN);
231+
motionEvent.setEdgeFlags(CUSTOM_EDGE_FLAG);
232+
233+
// getParent() cannot return null, since well - who would have called this method
234+
((ViewGroup) getParent()).dispatchTouchEvent(motionEvent);
235+
return false;
236+
}
237+
return dispatched;
238+
}
239+
211240
public int getCurrentOffset() {
212241
return -Utils.parseInt(getTag(R.id.tag_current_offset));
213242
}

0 commit comments

Comments
 (0)