Scrollview+ListView冲突解决
2019獨角獸企業重金招聘Python工程師標準>>>
最近做一個功能,要使用Scrollview+Listview+ ListView,,經過找demo,找到了一個例子,自己也實現了一個例子來理解實現過程。先分享下,望大家指點、
?思路 :
? ? ? ? 現在又scrollview,一個上面的listView 我們叫做topListView, 一個在下面的GridView 我們叫做bottomListView, 我讓topListView重寫onMeasure()方法,讓topListView完全顯示,不再具有滾動功能。
?
@Override public?void?onMeasure(int?widthMeasureSpec,?int?heightMeasureSpec)?{int?expandSpec?=?MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE?>>?2,?MeasureSpec.AT_MOST);super.onMeasure(widthMeasureSpec,?expandSpec); }? ?這樣topListview和ScrollView沖突問題得以解決。
? ?bottomListView和Scrollview和沖突我們通過以下解決:我們給tottomListView設置一個固定高度,這個高度往往是ScrollView顯示的高度,這樣解決了顯示的問題。
post(new?Runnable()?{@Overridepublic?void?run()?{int?height?=?mScrollView.getHeight();int?height2?=?topLayout.getHeight();Log.i("TEST",?"?post?--->?height?:?"?+?height);Log.i("TEST",?"?post?--->?height2?:?"?+?height2);Log.i("TEST",?"?post?--->?getHeight?:?"?+?rootView.getHeight());recommendGrid.getLayoutParams().height?=?height;recommendGrid.setSwitchHeight(height2);recommendGrid.invalidate();mScrollView.invalidate();} },?200);其次是滑動的沖突,沖突的關鍵是listView和Scrollview放在一起時listVIew接收不到屏幕的touch事件,解決它。
我們的解決方法是: 當Scrollview向上滾動時當滾動到topListView和bottom的分界點,即bottomListView滾動到屏幕頂端,我們就主動將touch叫給bottomListView,所以上面代碼同時設置了topLIstView的高度。
recommendGrid.setSwitchHeight(height2);然后判斷?
mScrollView.setOnMyScrollListener(new?PullScrollView.OnMyScrollListener()?{@Overridepublic?void?onScroll(int?l,?int?t,?int?oldl,?int?oldt)?{if?(recommendGrid.isOnTop())?{recommendGrid.setParentIdle(true);}?else?{recommendGrid.setParentIdle(false);}} });其中?
recommendGrid.setParentIdle();用來設置父scrollView是否接收touch事件,true :不接收,即傳給子元素ListView,false : 相反,內部方法是:
public?void?setParentIdle(boolean?flag)?{if?(parentView?!=?null)?{parentView.requestDisallowInterceptTouchEvent(flag);} }下滑時,需要將touch事件控制權再交給ScrollView,這時需要判斷,當bottom的第一個元素完全展示出來時才可以將控制權移交,
private?boolean?isFirstItemOnTop()?{boolean?flag?=?getFirstVisiblePosition()?==?0?&&?getChildCount()?>?0?&&?getChildAt(0).getTop()?>=?0;Log.d("TEST",?"?isFirstItemOnTop?--->?getFirstVisiblePosition?:?"?+?(getFirstVisiblePosition()?==?0));if?(getChildCount()?>?0)?{Log.d("TEST",?"?isFirstItemOnTop?--->?getChildAt(0).getTop()?:?"?+?getChildAt(0).getTop());}return?flag; }ok,基本思路在這里,具體代碼整理后在上傳,謝謝。
PS :? ?由于topListView是完全展示,所以getView()不會再被重用,且會重復執行很多次,所以不建議用來實現每個itemView特別復雜的功能,其實上面完全可以將topListView替換掉,用任一ViewGroup來實現其他功能,方法還是一樣的。
轉載于:https://my.oschina.net/u/559847/blog/480309
總結
以上是生活随笔為你收集整理的Scrollview+ListView冲突解决的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 组件名简单改了
- 下一篇: 15 sql base line 工作机