recyclerview 悬浮效果

依赖

   implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    testCompile 'junit:junit:4.12'

main布局

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

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

    <TextView
        android:id="@+id/tv_sticky_header_view"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#EFFAE7"
        android:gravity="center"
        android:text="刘路瑶1"/>
</FrameLayout>

item布局

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

    <RelativeLayout
        android:id="@+id/rl_content_parent"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp">

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true" />

        <TextView
            android:id="@+id/auto"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_alignParentBottom="true"
            android:background="#ffffff" />
    </RelativeLayout>

    <TextView
        android:id="@+id/tv_sticky_header_view"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#EFFAE7"
        android:gravity="center"
        android:text="刘路瑶1" />
</FrameLayout>

bean类

 public String name;
    public String autor;
    public String sticky;

    public Bean(String sticky, String name, String autor) {
        this.sticky = sticky;
        this.name = name;
        this.autor = autor;
    }

adapter页面

  //第一个吸顶
    private static final int FLRST_STICKY_VIEW = 1;
    //别的吸顶
    static final int HAS_STICKY_VIEW = 2;
    //正常view
    static final int NONE_STICKT_VIEW = 3;


    private final List<Bean> datas;
    private final LayoutInflater mInflate;

     MyAdapter(Context context, List<Bean> datas) {
        mInflate = LayoutInflater.from(context);
        this.datas = datas;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = mInflate.inflate(R.layout.item, viewGroup, false);
        return new MyViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
        Bean bean = datas.get(i);
        myViewHolder.name.setText(bean.name);
        myViewHolder.auto.setText(bean.autor);

        if (i == 0) {
            myViewHolder.tvStickyHeaderView.setVisibility(View.VISIBLE);
            myViewHolder.tvStickyHeaderView.setText(bean.sticky);
            myViewHolder.itemView.setTag(FLRST_STICKY_VIEW);
        } else {
            if (!TextUtils.equals(bean.sticky, datas.get(i - 1).sticky)) {
                myViewHolder.tvStickyHeaderView.setVisibility(View.VISIBLE);
                myViewHolder.tvStickyHeaderView.setText(bean.sticky);
                myViewHolder.itemView.setTag(HAS_STICKY_VIEW);
            } else {
                myViewHolder.tvStickyHeaderView.setVisibility(View.GONE);
                myViewHolder.itemView.setTag(NONE_STICKT_VIEW);
            }
        }
        //通过此处设置ContentDescripyion,作为内容描述,可以通过getContentDescription取出,功效setTag差不多。
        myViewHolder.itemView.setContentDescription(bean.sticky);
    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tvStickyHeaderView;
        RelativeLayout rlContentParent;
        TextView name;
        TextView auto;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tvStickyHeaderView = itemView.findViewById(R.id.tv_sticky_header_view);
            rlContentParent = itemView.findViewById(R.id.rl_content_parent);
            name = itemView.findViewById(R.id.name);
            auto = itemView.findViewById(R.id.auto);
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_43413728/article/details/86345958