1+ package jarvis .com .nestedtouchscrollinglayout ;
2+
3+ import android .content .Context ;
4+ import android .graphics .Canvas ;
5+ import android .graphics .Rect ;
6+ import android .os .Bundle ;
7+ import android .support .annotation .ColorInt ;
8+ import android .support .annotation .NonNull ;
9+ import android .support .annotation .Nullable ;
10+ import android .support .v7 .widget .LinearLayoutManager ;
11+ import android .support .v7 .widget .RecyclerView ;
12+ import android .view .LayoutInflater ;
13+ import android .view .MotionEvent ;
14+ import android .view .View ;
15+ import android .view .ViewGroup ;
16+ import android .widget .TextView ;
17+
18+ import jarvis .com .library .NestedTouchScrollingLayout ;
19+
20+ /**
21+ * @author yyf @ Zhihu Inc.
22+ * @since 12-05-2018
23+ */
24+ public class AppbarLayoutActivity extends BaseActivity {
25+ private int mContainerItemsCount = 20 ;
26+ private int mInnerItemsCount = 30 ;
27+
28+ public static int mHalfWindowHeight = 400 ; // dp
29+
30+ public static int mVelocityYBound = 1300 ;
31+
32+ private RecyclerView mContainerRecycler ;
33+
34+ private NestedTouchScrollingLayout mNestedTouchScrollingLayout ;
35+
36+ @ Override
37+ protected void onCreate (@ Nullable Bundle savedInstanceState ) {
38+ super .onCreate (savedInstanceState );
39+ setContentView (R .layout .activity_applayout );
40+
41+ findViewById (R .id .btn_open ).setOnClickListener (new View .OnClickListener () {
42+ @ Override
43+ public void onClick (View view ) {
44+ mNestedTouchScrollingLayout .expand ();
45+ }
46+ });
47+
48+ mContainerRecycler = findViewById (R .id .container_rv );
49+ mContainerRecycler .setLayoutManager (new LinearLayoutManager (this , LinearLayoutManager .VERTICAL , false ));
50+ mContainerRecycler .setAdapter (new InnerAdapter (this , 0x9966CC ));
51+ mContainerRecycler .addItemDecoration (new RecyclerView .ItemDecoration () {
52+ @ Override
53+ public void onDraw (@ NonNull Canvas c , @ NonNull RecyclerView parent , @ NonNull RecyclerView .State state ) {
54+ super .onDraw (c , parent , state );
55+ }
56+
57+ @ Override
58+ public void onDrawOver (@ NonNull Canvas c , @ NonNull RecyclerView parent , @ NonNull RecyclerView .State state ) {
59+ super .onDrawOver (c , parent , state );
60+ }
61+
62+ @ Override
63+ public void getItemOffsets (@ NonNull Rect outRect , @ NonNull View view , @ NonNull RecyclerView parent , @ NonNull RecyclerView .State state ) {
64+ super .getItemOffsets (outRect , view , parent , state );
65+ outRect .bottom = 30 ;
66+ }
67+ });
68+
69+ mNestedTouchScrollingLayout = findViewById (R .id .wrapper );
70+ mNestedTouchScrollingLayout .registerNestScrollChildCallback (new NestedTouchScrollingLayout .INestChildScrollChange () {
71+ @ Override
72+ public void onNestChildScrollChange (float deltaY ) {
73+
74+ }
75+
76+ @ Override
77+ public void onNestChildScrollRelease (final float deltaY , final int velocityY ) {
78+ int totalYRange = mNestedTouchScrollingLayout .getMeasuredHeight ();
79+
80+ int helfLimit = (totalYRange - DisplayUtils .dpToPixel (AppbarLayoutActivity .this , mHalfWindowHeight )) / 2 ;
81+
82+ int hideLimit = totalYRange - DisplayUtils .dpToPixel (AppbarLayoutActivity .this , mHalfWindowHeight ) / 2 ;
83+
84+ int helfHeight = totalYRange - DisplayUtils .dpToPixel (AppbarLayoutActivity .this , mHalfWindowHeight );
85+
86+ if (velocityY > mVelocityYBound && velocityY > 0 ) {
87+ if (Math .abs (deltaY ) > helfHeight ) {
88+ mNestedTouchScrollingLayout .hiden ();
89+ } else {
90+ mNestedTouchScrollingLayout .peek (mNestedTouchScrollingLayout .getMeasuredHeight () - DisplayUtils .dpToPixel (AppbarLayoutActivity .this ,400 ));
91+ }
92+ } else if (velocityY < -mVelocityYBound && velocityY < 0 ) {
93+ if (Math .abs (deltaY ) < helfHeight ) {
94+ mNestedTouchScrollingLayout .expand ();
95+ } else {
96+ mNestedTouchScrollingLayout .peek (mNestedTouchScrollingLayout .getMeasuredHeight () - DisplayUtils .dpToPixel (AppbarLayoutActivity .this ,400 ));
97+ }
98+ } else {
99+ if (Math .abs (deltaY ) > hideLimit ) {
100+ mNestedTouchScrollingLayout .hiden ();
101+ } else if (Math .abs (deltaY ) > helfLimit ) {
102+ mNestedTouchScrollingLayout .peek (mNestedTouchScrollingLayout .getMeasuredHeight () - DisplayUtils .dpToPixel (AppbarLayoutActivity .this , 400 ));
103+ } else {
104+ mNestedTouchScrollingLayout .expand ();
105+ }
106+ }
107+ }
108+
109+ @ Override
110+ public void onFingerUp (float velocityY ) {
111+
112+ }
113+
114+ @ Override
115+ public void onNestChildHorizationScroll (MotionEvent event , float deltaX , float deltaY ) {
116+
117+ }
118+ });
119+
120+ mNestedTouchScrollingLayout .setSheetDirection (NestedTouchScrollingLayout .SheetDirection .BOTTOM );
121+ mNestedTouchScrollingLayout
122+ .post (new Runnable () {
123+ @ Override
124+ public void run () {
125+ mNestedTouchScrollingLayout .recover (mNestedTouchScrollingLayout .getMeasuredHeight (), null , 0 );
126+ }
127+ });
128+ }
129+
130+ class ContainerAdapter extends RecyclerView .Adapter <AppbarLayoutActivity .ContainerViewHolder > {
131+
132+ private Context mContext ;
133+ private LayoutInflater mInflater ;
134+
135+ public ContainerAdapter (Context context ) {
136+ this .mContext = context ;
137+ this .mInflater = LayoutInflater .from (mContext );
138+ }
139+
140+ @ NonNull
141+ @ Override
142+ public AppbarLayoutActivity .ContainerViewHolder onCreateViewHolder (@ NonNull ViewGroup viewGroup , int i ) {
143+ View view = mInflater .inflate (R .layout .recycer_item , viewGroup , false );
144+ return new AppbarLayoutActivity .ContainerViewHolder (view );
145+ }
146+
147+ @ Override
148+ public void onBindViewHolder (@ NonNull AppbarLayoutActivity .ContainerViewHolder containerViewHolder , int i ) {
149+ containerViewHolder .mRecycler .setLayoutManager (new LinearLayoutManager (mContext , LinearLayoutManager .VERTICAL , false ));
150+ switch (i % 5 ) {
151+ case 0 :
152+ containerViewHolder .mRecycler .setAdapter (new AppbarLayoutActivity .InnerAdapter (mContext , 0xFFCCFF ));
153+ break ;
154+ case 1 :
155+ containerViewHolder .mRecycler .setAdapter (new AppbarLayoutActivity .InnerAdapter (mContext , 0x9966CC ));
156+ break ;
157+ case 2 :
158+ containerViewHolder .mRecycler .setAdapter (new AppbarLayoutActivity .InnerAdapter (mContext , 0x33FF33 ));
159+ break ;
160+ case 3 :
161+ containerViewHolder .mRecycler .setAdapter (new AppbarLayoutActivity .InnerAdapter (mContext , 0x33FFFF ));
162+ break ;
163+ case 4 :
164+ containerViewHolder .mRecycler .setAdapter (new AppbarLayoutActivity .InnerAdapter (mContext , 0xFFFF00 ));
165+ break ;
166+ default :
167+ break ;
168+ }
169+
170+ containerViewHolder .mRecycler .addItemDecoration (new RecyclerView .ItemDecoration () {
171+ @ Override
172+ public void onDraw (@ NonNull Canvas c , @ NonNull RecyclerView parent , @ NonNull RecyclerView .State state ) {
173+ super .onDraw (c , parent , state );
174+ }
175+
176+ @ Override
177+ public void onDrawOver (@ NonNull Canvas c , @ NonNull RecyclerView parent , @ NonNull RecyclerView .State state ) {
178+ super .onDrawOver (c , parent , state );
179+ }
180+
181+ @ Override
182+ public void getItemOffsets (@ NonNull Rect outRect , @ NonNull View view , @ NonNull RecyclerView parent , @ NonNull RecyclerView .State state ) {
183+ super .getItemOffsets (outRect , view , parent , state );
184+ outRect .bottom = 10 ;
185+ }
186+ });
187+
188+ }
189+
190+ @ Override
191+ public int getItemCount () {
192+ return mContainerItemsCount ;
193+ }
194+ }
195+
196+ class ContainerViewHolder extends RecyclerView .ViewHolder {
197+
198+ public RecyclerView mRecycler ;
199+
200+ public ContainerViewHolder (@ NonNull View itemView ) {
201+ super (itemView );
202+ mRecycler = itemView .findViewById (R .id .recycler_id );
203+ }
204+ }
205+
206+
207+
208+ public class InnerAdapter extends RecyclerView .Adapter <AppbarLayoutActivity .InnerViewHolder > {
209+
210+ private Context mContext ;
211+ private LayoutInflater mInflater ;
212+ private @ ColorInt
213+ int mBgColor ;
214+
215+ public InnerAdapter (Context context , @ ColorInt int color ) {
216+ mContext = context ;
217+ mInflater = LayoutInflater .from (mContext );
218+ mBgColor = color ;
219+ }
220+
221+ @ NonNull
222+ @ Override
223+ public AppbarLayoutActivity .InnerViewHolder onCreateViewHolder (@ NonNull ViewGroup viewGroup , int i ) {
224+ View view = mInflater .inflate (R .layout .main_item , viewGroup , false );
225+ return new AppbarLayoutActivity .InnerViewHolder (view );
226+ }
227+
228+ @ Override
229+ public void onBindViewHolder (@ NonNull AppbarLayoutActivity .InnerViewHolder innerViewHolder , int i ) {
230+ innerViewHolder .tv .setText ("Jarvis ----> " + i );
231+ }
232+
233+ @ Override
234+ public int getItemCount () {
235+ return mInnerItemsCount ;
236+ }
237+ }
238+
239+ public class InnerViewHolder extends RecyclerView .ViewHolder {
240+
241+ public TextView tv ;
242+
243+ public InnerViewHolder (@ NonNull View itemView ) {
244+ super (itemView );
245+ tv = itemView .findViewById (R .id .main_tv );
246+ }
247+ }
248+ }
0 commit comments