想必大家遇到过在同一个界面中,要展示的内容非常多,完全展示出来高度远超过屏幕高度,这时候通常的做法是布局中使用ScrollView进行嵌套,但如果遇到ScrollView嵌套中包含一个或者多个RecyclerView列表的话,还得自定义RecyclerView以解决滑动冲突。那么今天我们这里有一个现成的控件NestedScrollView,用他替代ScrollView,并且不用自定义RecyclerView等列表控件来解决滑动冲突。因为他的内部已经帮我们解决了子View的滑动冲突。
下面我们来看看他的具体用法:
ScrollView的用法:
xml布局代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.haoyue.notedemos.nestscrollview.NestedScrollViewActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="enterAlways|scroll">
<TextView
android:id="@+id/customTitle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="自定义标题"
android:textColor="@color/toolbar_title"
android:textSize="18sp" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.haoyue.auxiliary.AdaptiveImageView
android:id="@+id/imageView"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="@drawable/zero04" />
<android.support.v7.widget.RecyclerView
android:id="@+id/rvNestedScrollView"
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);
剩余的一切都不变。运行效果如下:
现在来看,滑动效果非常顺溜。完美的解决了滑动冲突。