Android-RecyclerView系列 Item自动吸顶

Posted 彭老希

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android-RecyclerView系列 Item自动吸顶相关的知识,希望对你有一定的参考价值。

一、Activity

public class MainActivity extends AppCompatActivity {

	//加载数据项,自行替换
    private List<Star> starList;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //添加模拟数据
        init();
        
        RecyclerView rv = findViewById(R.id.rv_list);
        //设置布局管理器和适配器
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setAdapter(new StarAdapter(this,starList));
        //自定义分割线
        rv.addItemDecoration(new StarDecoration(this));
        
    }

    //初始化,为star集合添加数据
    private void init() {
        starList = new ArrayList<>();

        for(int i = 0;i < 4;i++){
            for(int j = 0;j < 20;j++){
                if(i % 2 == 0){
                    starList.add(new Star("彭老希" + j,"相信自己" + i));
                }else {
                    starList.add(new Star("欢迎关注!" + j,"天天开心" + i));
                }
            }
        }
    }
}

二、实体类 Bean

//自行定义要处理的数据
public class Star {
    private String name;
    private String group;

    public Star(String name, String group) {
        this.name = name;
        this.group = group;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGroup() {
        return group;
    }

    public void setGroup(String group) {
        this.group = group;
    }
}

三、适配器 XXXAdapter

public class StarAdapter extends RecyclerView.Adapter<StarAdapter.StarViewHolder> {
    private Context context;
    private List<Star> starList;

    public StarAdapter(Context context, List<Star> starList) {
        this.context = context;
        this.starList = starList;
    }
    public boolean isGroupHeader(int position){
        if(position == 0){
            return true;
        }else {
            String currentGroupName = getGroupName(position);
            String preGroupName = getGroupName(position - 1);
            if(currentGroupName.equals(preGroupName)){
                return false;
            }else {
                return true;
            }
        }
    }

    public String getGroupName(int position){
        return starList.get(position).getGroup();
    }
    @NonNull
    @Override
    public StarViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.rv_item_star,null);
        return new StarViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull StarViewHolder holder, int position) {
        holder.tv.setText(starList.get(position).getName());
    }

    @Override
    public int getItemCount() {
        return starList == null ? 0 : starList.size();
    }

    public class StarViewHolder extends RecyclerView.ViewHolder{
        private TextView tv;
        public StarViewHolder(@NonNull View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.tv_star);
        }
    }
}

四、StarDecoration

public class StarDecoration extends RecyclerView.ItemDecoration {
    private int groupHeaderHeight;
    private Paint headPaint;
    private Paint textPaint;
    private Rect textRect;

    @Override
    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.onDraw(c, parent, state);
        if(parent.getAdapter() instanceof StarAdapter){
            StarAdapter adapter = (StarAdapter) parent.getAdapter();
            //首先得到当前屏幕中的itemView的个数
            int childCount = parent.getChildCount();
            int left = parent.getPaddingLeft();
            int right = parent.getWidth() - parent.getPaddingRight();
            for (int i = 0; i < childCount; i++) {
                //得到某一count(不是position)对应的itemView
                View view = parent.getChildAt(i);
                //根据这个itemView得到其position
                int position = parent.getChildLayoutPosition(view);
                //Log.v("ljh","当前position为" + position);
                //判断这个position是否为头部
                boolean isGroupHeader = adapter.isGroupHeader(position);
                if(isGroupHeader && view.getTop() - groupHeaderHeight - parent.getPaddingTop() >= 0){
                    c.drawRect(left,view.getTop() - groupHeaderHeight,right,view.getTop(),headPaint);
                    //得到组名
                    String groupName = adapter.getGroupName(position);
                    textPaint.getTextBounds(groupName,0,groupName.length(),textRect);
                    c.drawText(groupName,left,view.getTop() - groupHeaderHeight/2 + textRect.height()/2,
                            textPaint);
                }else if (view.getTop() - groupHeaderHeight - parent.getPaddingTop() >= 0){
                    //绘制分割线
                    c.drawRect(left,view.getTop() - 1,right,view.getTop(),headPaint);
                }
            }
        }
    }

    @Override
    public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
        if(parent.getAdapter() instanceof StarAdapter){
            StarAdapter adapter = (StarAdapter) parent.getAdapter();
            //返回可见区域内的第一个item的position
            int position = ((LinearLayoutManager) parent.getLayoutManager()).findFirstVisibleItemPosition();
            //获取对应position的view
            View itemView = parent.findViewHolderForAdapterPosition(position).itemView;
            int left = parent.getPaddingLeft();
            int right = parent.getWidth() - parent.getPaddingRight();
            int top = parent.getPaddingTop();

            //这里有个细节
            //如果下面一个组没有顶着上面那个组,那么应该一直参数不变地去绘制
            //如果下面一个组在顶着上面那个组,那么上面那个组应该改变bottom
            //所以我们要进行一个判断,就是判断下面一个组到底有没有顶着上面的组
            //而判断的依据就是当前顶部的itemView的下一个itemView是不是要顶着它的下一个组
            boolean isGroupHeader = adapter.isGroupHeader(position + 1);
            if(isGroupHeader){
                //随时改变底部
                int bottom = Math.min(groupHeaderHeight,itemView.getBottom() - parent.getPaddingTop());
                c.drawRect(left,top,right,top + bottom,headPaint);

                //绘制文字
                //得到组名
                String groupName = adapter.getGroupName(position);
                textPaint.getTextBounds(groupName,0,groupName.length(),textRect);

                // 绘制文字的高度不能超出区域
                c.clipRect(left, top, right, top + bottom);

                c.drawText(groupName,left,top + bottom - groupHeaderHeight/2 + textRect.height()/2,
                        textPaint);
            }else {
                //如果没有被顶着,则一直绘制最上面的组名
                c.drawRect(left,top,right,top + groupHeaderHeight,headPaint);

                //绘制文字
                //得到组名
                String groupName = adapter.getGroupName(position);
                textPaint.getTextBounds(groupName,0,groupName.length(),textRect);
                c.drawText(groupName,left,top + groupHeaderHeight/2 + textRect.height()/2,
                        textPaint);
            }
        }
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        if(parent.getAdapter() instanceof StarAdapter){
            StarAdapter adapter = (StarAdapter) parent.getAdapter();
            //得到position
            int position = parent.getChildLayoutPosition(view);
            //判断是否为头部
            boolean isGroupHeader = adapter.isGroupHeader(position);
            if(isGroupHeader){
                //如果是头部
                outRect.set(0,groupHeaderHeight,0,0);
            }else {
                //虽然上面绘制了分割线,但是这里还是要写
                //因为这里是留出空间,如果这里没有把空间留出来,那么上面根本没法绘制分割线
                outRect.set(0,1,0,0);
            }
        }
    }

    public StarDecoration(Context context) {
        groupHeaderHeight = dp2px(context,100);
        headPaint = new Paint();
        headPaint.setColor(Color.RED);

        textPaint = new Paint();
        textPaint.setTextSize(50);
        textPaint.setColor(Color.WHITE);

        textRect = new Rect();
    }

    private int dp2px(Context context,float dpValue){
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue*scale*0.5f);
    }
}


五、activity_main.xml

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

    <androidx.recyclerview.widget.RecyclerView
        android:background="@color/black"
        android:paddingTop="150dp"
        android:id="@+id/rv_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </androidx.recyclerview.widget.RecyclerView>

</RelativeLayout>

六、rv_item_star.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/purple_500">
    <TextView
        android:id="@+id/tv_star"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:textSize="20sp"
        android:gravity="center"/>
</RelativeLayout>

以上是关于Android-RecyclerView系列 Item自动吸顶的主要内容,如果未能解决你的问题,请参考以下文章

Android-RecyclerView系列 Item自动吸顶

Android-RecyclerView系列 RecyclerView滚动指定位置到屏幕中间

Android-RecyclerView系列 notifyItemChanged() - 实现单选选中状态更新

Android-RecyclerView系列 RecyclerView滑动后数据显示错乱

Android-RecyclerView系列 获取可见与不可见的View(亲测有效)

Android-RecyclerView系列 RecyclerView实现Item居中效果