標籤:nta 介面 his bool 方法 copy mon tom 說明
一個比較長的介面一般都是Scrollview嵌套RecyclerView來解決.不過這樣的UI並不是我們開發人員想看到的,實際上嵌套之後.因為Scrollview和RecyclerView都是滑動控制項.會有一點滑動上的衝突.導致滑動起來有些卡頓.這個時候.我們重寫一下LayoutManager就行了
例如:
[java] view plain copy
- LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false) {
- @Override
- public boolean canScrollVertically() {
- return false;
- }
- };
- recyclerview.setLayoutManager(linearLayoutManager);
- recyclerview.setAdapter(tempCommonAdapter);
如此.禁止掉RecyclerView的滑動.就能一如既往的流暢了
問題現象:
一個介面有多個RecyclerView或者其他超過一屏顯示的一些內容時,就需要要上下滾動了,就會需要在外面嵌套一個ScrollView,但是滑動過程不是很順暢,有卡頓的感覺。
解決方案:
禁止RecyclerView的滑動。
最簡單便捷的方法就是
[java] view plain copy
- linearLayoutManager = new LinearLayoutManager(context) {
- @Override
- public boolean canScrollVertically() {
- return false;
- }
- };
另外就是重寫LayoutManager了,以Grid模式舉例說明:
[java] view plain copy
- public class ScrollGridLayoutManager extends GridLayoutManager {
- private boolean isScrollEnabled = true;
- public ScrollGridLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
- super(context, attrs, defStyleAttr, defStyleRes);
- }
-
- public ScrollGridLayoutManager(Context context, int spanCount) {
- super(context, spanCount);
- }
-
- public ScrollGridLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout) {
- super(context, spanCount, orientation, reverseLayout);
- }
-
- public void setScrollEnabled(boolean flag) {
- this.isScrollEnabled = flag;
- }
-
- @Override
- public boolean canScrollVertically() {
- //Similarly you can customize "canScrollHorizontally()" for managing horizontal scroll
- return isScrollEnabled && super.canScrollVertically();
- }
- }
Android Scrollview嵌套RecyclerView導致滑動卡頓問題解決