NestedScrollview使用---优雅的代替ScrollView内嵌ListView或者RecyclerView

以前,在开发中,类似商品详情这样的页面,首页这样的页面,拿商品详情页面来说,有这么一个特点:

  1. 订单中的商品以列表的形式存在,那就需要使用ListView或者RecyclerView
  2. 订单以外的内容(一般在商品上边或者商品下边),内容多的话需要滚动

一般的做法:

1. ScrollView内嵌ListView或者RecyclerView,内嵌的ListView或者RecyclerView一般
    需要重写onMeasure,把listView撑到最大,等于是没有复用listItem也屏蔽了Listview的滚动
2.将订单的相关信息分别当做头布局和脚布局 总觉得多创建了几个布局文件不太好
3.RecyclerView+MultiTypeAdapter虽然可以实现复杂布局,但是感觉有点大材小用


下面介绍一种方式,可以优雅的解决这一问题——NestedScrollview
NestedScrollview是support-v4包提供的控件,继承至FrameLayout,
并实现了NestedScrollingParent,NestedScrollingChild, ScrollingView接口.
它的作用类似于android.widget.ScrollView,不同点在于NestedScrollView支持嵌套滑动.

使用:
1.导入VecycleView,support:design

compile 'com.android.support:design:26.1.0'
implementation 'com.android.support:recyclerview-v7:26.0.0-alpha1'

2.在布局文件中使用

     <android.support.v4.widget.NestedScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusable="true"
        android:focusableInTouchMode="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="顶部标题" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:gravity="center"
                android:text="内容1" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            </android.support.v7.widget.RecyclerView>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src="@mipmap/pic8" />
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>

注意: NestedScrollView中的 android:focusable="true" android:focusableInTouchMode="true"
是为了解决NestedScrollView中嵌套其他布局和RecyclerView后,不会滚动到屏幕顶部

2.MainActivity中

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        recyclerView.addItemDecoration(new DividerItemDecoration(this, 1));
        recyclerView.setAdapter(new MyAdapter());

        recyclerView.setNestedScrollingEnabled(false);//解决卡顿
    }

    class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MyViewHolder(LayoutInflater.from(MainActivity.this).inflate(R.layout.item_recyclerview, null));
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            holder.text.setText("position=" + position);
        }

        @Override
        public int getItemCount() {
            return 10;
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView text;

        public MyViewHolder(View itemView) {
            super(itemView);
            text = itemView.findViewById(R.id.text);
        }
    }

注意: recyclerView.setNestedScrollingEnabled(false);是为了解决滑动卡顿(RecycleView滑动惯性消失)问题

3.item布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:src="@mipmap/pic11" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

浏览下效果:
这里写图片描述
点击这里下载源码

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值