您可能遇到过在同一个界面中,以远远超过屏幕高度的高度显示的内容非常多的情况。 在这种情况下,通常在布局中使用ScrollView进行嵌套,但如果ScrollView嵌套包含一个或多个RecyclerView列表,则必须自定义RecyclerView以解决幻灯片冲突。 今天,这里有现成的控件NestedScrollView。 用他代替ScrollView。 此外,不需要自定义列表控件(如RecyclerView )来解决幻灯片冲突。 因为他内部已经解决了我们子View的滑动冲突。
看看他的具体用法:
使用ScrollView :
xml布局代码:
linearlayoutxmlns 3360 Android=' http://schemas.Android.com/apk/RES/Android ' xmlns 3360 app=' http://schemas.Android ' xmlns 3360 app=' 3358 schemas.Android RES-auto ' xmlns 3360 tools=' 3http://tools ' Android 3360 layout _ width=' match _ parent ' Android 3360 layout _ height=' match _ parent ' Android 360 orient e ty ' Android.support.design.widget.appbarlayoutandroid 3360 id=app bar ' Android : layout _ width=' match _ parend id.suppor oid toolbar ' Android : layout _ width=' match _ parent ' Android : layout _ height=' wrap _ content ' atid 标题'安卓: layout _ width=' wrap _ content '安卓3360 layout _ height=' match _ parent '安卓33666 Android.support.V7.widget.toolbar/Android.support.design.widget.appbarlayoutscrollviewandroid : layout _ width=' match _ parent ' Android 3360 layouth id 3360 layout _ height=' match _ parent ' Android 3360 orientation=' match _ parent ' Android 360 orientation=' andd 3333330 imageview ' Android : layout _ width=' 200 DP ' Android 3360 layout _ height=' wrap _ content ' Android 3360 layout _ gghid RC=' @ drawable/zero 04 '/Android.support.V7.widget.recycle er
android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> </ScrollView></LinearLayout>Java代码:
public class NestedScrollViewActivity extends AppCompatActivity { @BindView(R.id.customTitle) TextView customTitle; @BindView(R.id.toolBar) Toolbar toolBar; @BindView(R.id.rvNestedScrollView) RecyclerView rvNestedScrollView; List<String> integerList; NestedScrollViewAdapter adapter; @BindView(R.id.appBar) AppBarLayout appBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_nested_scroll_view); ButterKnife.bind(this); toolBar.setTitle(""); customTitle.setText("NestedScrollView的练习"); setSupportActionBar(toolBar); integerList = intiData(); intiData(); mainViewListData(); } private List<String> intiData() { return InitData.getInstance().integerList(); } private void mainViewListData() { LinearLayoutManager manager = new LinearLayoutManager(this); manager.setOrientation(LinearLayoutManager.VERTICAL); rvNestedScrollView.setLayoutManager(manager); adapter = new NestedScrollViewAdapter(this, integerList); rvNestedScrollView.setAdapter(adapter); }}integerList数据:
public class InitData { public static InitData instance; public static InitData getInstance(){ if (instance == null) { synchronized (InitData.class){ if (instance == null) { instance = new InitData(); } } } return instance; } public List<String> integerList(){ List<String> integerList = new ArrayList<>(); integerList.add("剑一破"); integerList.add("剑二空"); integerList.add("剑三飞"); integerList.add("剑四灭"); integerList.add("剑五虚"); integerList.add("剑六绝"); integerList.add("剑七真"); integerList.add("清脆的白云玄"); integerList.add("剑九轮回"); integerList.add("剑十天葬"); integerList.add("剑十一涅槃"); integerList.add("剑十二心"); integerList.add("剑一破"); integerList.add("剑二空"); integerList.add("剑三飞"); integerList.add("剑四灭"); integerList.add("剑五虚"); integerList.add("剑六绝"); integerList.add("剑七真"); integerList.add("清脆的白云玄"); integerList.add("剑九轮回"); integerList.add("剑十天葬"); integerList.add("剑十一涅槃"); integerList.add("剑十二心"); integerList.add("剑一破"); integerList.add("剑二空"); integerList.add("剑三飞"); integerList.add("剑四灭"); integerList.add("剑五虚"); integerList.add("剑六绝"); integerList.add("剑七真"); integerList.add("清脆的白云玄"); integerList.add("剑九轮回"); integerList.add("剑十天葬"); integerList.add("剑十一涅槃"); integerList.add("剑十二心"); return integerList; }}运行效果如下:
很明显的,当我们没有自定义RecyclerView以解决滑动冲突的时候,可以很清楚的看到当一屏刷满后,无论怎样滑动屏幕,都无法加载出更多的Item。
现在我们用NestedScrollView代替ScrollView来试试:
用法很简单,在前面贴出的xml文件中,只需要用android.support.v4.widget.NestedScrollView替代ScrollView,并给RecyclerView设置.setNestedScrollingEnabled(false)属性即可:
rvNestedScrollView.setNestedScrollingEnabled(false);剩余的一切都不变。运行效果如下:
现在来看,滑动效果非常顺溜。完美的解决了滑动冲突。