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)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM