1. 程式人生 > 其它 >Android中RecyclerView監聽滑動到頂部和底部

Android中RecyclerView監聽滑動到頂部和底部

技術標籤:安卓學習筆記android

思路:onScrollStateChanged 用於監聽RecyclerView滑動狀態

crollState =SCROLL_STATE_IDLE(0) =RecyclerView.SCROLL_STATE_IDLE :表示螢幕已停止。螢幕停止滾動時為0

效果:

核心程式碼

        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            // OnScrollListener.SCROLL_STATE_IDLE; //停止滑動狀態
            // 記錄當前滑動狀態
            if (newState == RecyclerView.SCROLL_STATE_IDLE) { //當前狀態為停止滑動
                Log.d(TAG, "SCROLL_STATE_IDLE=" + (newState == RecyclerView.SCROLL_STATE_IDLE));
                if (!workSelectRv.canScrollVertically(1)) { // 到達底部
                    Log.d(TAG, "到達底部");
                    Toast.makeText(RecyclerViewScrollBottom.this, "我是有底線的", Toast.LENGTH_SHORT).show();

                } else if (!workSelectRv.canScrollVertically(-1)) { // 到達頂部
                    Toast.makeText(recyclerView.getContext(), "到頂了", Toast.LENGTH_SHORT).show();
                    Log.d(TAG, "到頂了");
                }
            }
        }

完整實現程式碼:

package com.example.mvpfour.spn;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.example.mvpfour.R;

public class RecyclerViewScrollBottom extends AppCompatActivity {
    private RecyclerView workSelectRv;
    private LinearLayoutManager linearLayoutManager;
    private static final String TAG = "GsonUtils";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rec_sro_bt);
        initView();
        initRecyclerViewMassage();
    }

    private void initView() {
        workSelectRv = (RecyclerView) findViewById(R.id.work_select_rv);
    }

    private void initRecyclerViewMassage() {
        linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        workSelectRv.setLayoutManager(linearLayoutManager);
        workSelectRv.setAdapter(myAdapterMassage);
        //onScroll 用於監聽RecyclerView螢幕滾動
        workSelectRv.addOnScrollListener(loadingMoreListener);
    }

    RecyclerView.OnScrollListener loadingMoreListener = new RecyclerView.OnScrollListener() {
        //到達頂部和底部監聽
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            // OnScrollListener.SCROLL_STATE_IDLE; //停止滑動狀態
            // 記錄當前滑動狀態
            if (newState == RecyclerView.SCROLL_STATE_IDLE) { //當前狀態為停止滑動
                Log.d(TAG, "SCROLL_STATE_IDLE=" + (newState == RecyclerView.SCROLL_STATE_IDLE));
                if (!workSelectRv.canScrollVertically(1)) { // 到達底部
                    Log.d(TAG, "到達底部");
                    Toast.makeText(RecyclerViewScrollBottom.this, "我是有底線的", Toast.LENGTH_SHORT).show();

                } else if (!workSelectRv.canScrollVertically(-1)) { // 到達頂部
                    Toast.makeText(recyclerView.getContext(), "到頂了", Toast.LENGTH_SHORT).show();
                    Log.d(TAG, "到頂了");
                }
            }
        }

    };

    RecyclerView.Adapter myAdapterMassage = new RecyclerView.Adapter() {
        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.work_select_item, parent,false);
            TextView textView = new TextView(parent.getContext());
            textView.setText("測試資料");
            textView.setHeight(60);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
            return new MyViewHolderBot(textView);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        }

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

    public static class MyViewHolderBot extends RecyclerView.ViewHolder {
        private CheckBox checkBox;
        private TextView work_message;

        public MyViewHolderBot(View itemView) {
            super(itemView);
            checkBox = (CheckBox) itemView.findViewById(R.id.work_check);
            work_message = (TextView) itemView.findViewById(R.id.work_message);
        }
    }


}


佈局檔案work_select_rv.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

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