recycleview点击变色,禁止滑动,获取滑动到某一项

这篇文章接上一篇的recyclview的总结继续写下去 点这里的链接

一.点击变色
在adapter中代码

    private int selected = -1;

    public void setSelected(int selected) {
        this.selected = selected;
        notifyDataSetChanged();
    }
......
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        if (items.size() > 0) {
            //为类型1设置数据
            if (viewHolder instanceof GridViewViewHolder) {
                GridViewViewHolder holder = (GridViewViewHolder) viewHolder;
                holder.itemText.setText(items.get(i).getText());
                //选中后颜色变为白色
                if (selected == i) {
                    holder.itemText.setTextColor(Color.WHITE);
                } else {
                    holder.itemText.setTextColor(Color.BLACK);
                }
            }
        }
    }

在activity中adapter的点击事件中:

       adapter.setOnItemClickListener((v, p) -> {
            Log.d(TAG, "initViews: ----p:" + p);
            Toast.makeText(this, "点击了第" + p + "个", Toast.LENGTH_SHORT).show();

            adapter.setSelected(p);
        });

二.禁止滑动
需要重写layoutManager,其中LinearLayoutManager ,GridviewLayoutManager,和StaggeredGridLayoutManager类似,重写后,直接使用即可;

package com.ytlz.myapplication;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.util.AttributeSet;

/**
 * Created by wyb on 2019-08-27.
 */

public class MyLinearLayoutManager extends LinearLayoutManager {
    private boolean isScrolled = true;

    public MyLinearLayoutManager(Context context) {
        super(context);
    }

    public MyLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
    }

    public MyLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public void setScrolled(boolean scrolled) {
        isScrolled = scrolled;
    }

    @Override
    public boolean canScrollVertically() {
        return isScrolled;
    }
}

然后在activity中

        recyclerView = findViewById(R.id.details_recycle);
        MyLinearLayoutManager manager = new MyLinearLayoutManager(this);
        recyclerView.setLayoutManager(manager);
        wifiAdapter = new WifiAdapter(recyclerView, testLists);
        recyclerView.setAdapter(wifiAdapter);
        //合适的时机调用禁止滑动代码
//        manager.setScrolled(false);

三,获取滑动到某一项

       recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                //总的条目数
                int itemCount = layoutManager.getItemCount();
                //当前界面显示的条目数
                int childCount = layoutManager.getChildCount();
                //第一个完全显示出来的条数位置
                int FirstCompletelyVisibleItemPosition = layoutManager.findFirstCompletelyVisibleItemPosition();
                //第一个显示出来的条数位置
                int FirstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
                //最后一个完全显示出来的条数位置
                int LastCompletelyVisibleItemPosition = layoutManager.findLastCompletelyVisibleItemPosition();
                //最后一个显示出来的条数位置
                int LastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();

                Log.d(TAG, "onScrolled: ------->itemCount:" + itemCount + "\n"
                        + "childCount:" + childCount + "\n"
                        + "FirstCompletelyVisibleItemPosition:" + FirstCompletelyVisibleItemPosition + "\n"
                        + "FirstVisibleItemPosition:" + FirstVisibleItemPosition + "\n"
                        + "LastCompletelyVisibleItemPosition:" + LastCompletelyVisibleItemPosition + "\n"
                        + "LastVisibleItemPosition:" + LastVisibleItemPosition);
            }
        });

你可能感兴趣的:(recycleview,学习笔记,Android)