SmartRefreshLayout 嵌套CoordinatorLayout NestedScrollView RecyclerView实现滑动置顶,加载更多遇到加载更多时tab被划出屏幕

 xml布局如下:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:id="@+id/activity_swipe_refresh_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.CoordinatorLayout
            android:id="@+id/coordinatorlayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.design.widget.AppBarLayout
                android:id="@+id/appbar_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/white"
                app:elevation="0dp">

                <android.support.design.widget.CollapsingToolbarLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:layout_scrollFlags="scroll|exitUntilCollapsed">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:focusable="true"
                        android:focusableInTouchMode="true"
                        android:orientation="vertical">
                      自定义view
                    </LinearLayout>

                </android.support.design.widget.CollapsingToolbarLayout>

                <Tabview   自定义tab组件
                    android:id="@+id/tab
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </android.support.design.widget.AppBarLayout>

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

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/recycler_view"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scrollbars="none" />
            </android.support.v4.widget.NestedScrollView>
        </android.support.design.widget.CoordinatorLayout>
    </com.scwang.smartrefresh.layout.SmartRefreshLayout>
</FrameLayout>

java文件

 LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);

        adapter = new ActivityAdapter(this);
        recyclerView.setAdapter(adapter);
        smartRefreshLayout.setOnRefreshListener(refreshLayout -> bindData(false));
        smartRefreshLayout.setOnLoadMoreListener(refreshLayout -> bindData(true));

设置完成后刷新正常,但是加载更多时,自定义的悬浮在顶部的tab会被滑出屏幕,

SmartRefreshLayout的setOnLoadMoreListener 和 CoordinatorLayout有滑动冲突

修改办法 禁用SmartRefreshLayout的加载更多 ,在adapter中设置footer加载更多
smartRefreshLayout.setEnableLoadMore(false)  

代码如下 adapter
class Adapter(context: Context) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    val TYPE_ITEM = 0
    val TYPE_FOOTER = 1

    //加载更多
    val LOADING_MORE = 1
    //数据加载完成(隐藏加载布局)
    val LOAD_FINISH = -1
    //没有更多
    val NO_MORE = 2

    var context: Context = context

    var footerView: View? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if (viewType == TYPE_FOOTER) {
            footerView = LayoutInflater.from(context).inflate(R.layout.load_more, parent, false)
            return FootViewHolder(footerView!!)
        }
        val view = LayoutInflater.from(context).inflate(R.layout.activity_item, parent, false)
        return ItemViewHolder(view)
    }

    override fun getItemCount(): Int {
        if (activityInfoList == null) {
            return 0
        }
        return activityInfoList!!.size + 1
    }


    override fun getItemViewType(position: Int): Int {
        return if (position + 1 == itemCount) {
            TYPE_FOOTER
        } else {
            TYPE_ITEM
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is ItemViewHolder) {
            if (activityInfoList == null || activityInfoList!!.size == 0) {
                return
            }
            val activityInfo = activityInfoList!!.get(position)
           
绑定数据
} } fun refreshFooter(state: Int) { if (footerView == null) { return } val loadMoreProgressBar = footerView!!.findViewById<ProgressBar>(R.id.load_more_progress_bar) val loadMoreTextView = footerView!!.findViewById<TextView>(R.id.load_more_text_view) when (state) { //根据状态来让脚布局发生改变 LOADING_MORE -> { footerView!!.setVisibility(View.VISIBLE) loadMoreProgressBar.setVisibility(View.VISIBLE) loadMoreTextView.setVisibility(View.VISIBLE) loadMoreTextView.setText(context.resources.getString(R.string.new_loading)) } NO_MORE -> { footerView!!.setVisibility(View.VISIBLE) loadMoreProgressBar.setVisibility(View.GONE) loadMoreTextView.setVisibility(View.VISIBLE) loadMoreTextView.setText(context.resources.getString(R.string.new_load_over)) } LOAD_FINISH -> { footerView!!.setVisibility(View.GONE) } } } class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { //初始化数据 } class FootViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) }

加载

    nested_scrollView.setOnScrollChangeListener(object : NestedScrollView.OnScrollChangeListener {
            override fun onScrollChange(view: NestedScrollView, scrollX: Int, scrollY: Int, oldScrollX: Int, oldScrollY: Int) {
                if (scrollY == (view.getChildAt(0).getMeasuredHeight() - view.getMeasuredHeight())) {
                    //滑动到底了
                    if (currentPage == 3) {
                        adapter!!.refreshFooter(adapter!!.NO_MORE)
                    } else {
                        adapter!!.refreshFooter(adapter!!.LOADING_MORE)
                        requestData(true)
                    }
                }
            }
        })
        adapter!!.refreshFooter(adapter!!.LOADING_MORE)




加载完成后隐藏
 adapter!!.refreshFooter(activityAdapter!!.LOAD_FINISH)

 

posted @ 2019-04-30 15:40  你好and程序员  阅读(10057)  评论(0编辑  收藏  举报