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>

adapter代码

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    //第一个吸顶
    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);
        }
    }
}

mianactivity代码

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private TextView tvStickyHeaderView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initListener();
    }

    private void initListener() {

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                View view = recyclerView.findChildViewUnder(0, 0);
                if (view != null && view.getContentDescription() != null) {
                    if (!TextUtils.equals(tvStickyHeaderView.getText(), view.getContentDescription())) {
                        tvStickyHeaderView.setText(view.getContentDescription());
                    }
                }

                View transInfoView = recyclerView.findChildViewUnder(0, tvStickyHeaderView.getHeight() + 1);
                if (transInfoView.getTag() != null) {
                    int transViewStatus = (int) transInfoView.getTag();
                    int top = transInfoView.getTop();
                    if (transViewStatus == MyAdapter.HAS_STICKY_VIEW) {
                        if (top > 0) {
                            int dealtY = top - tvStickyHeaderView.getMeasuredHeight();
                            tvStickyHeaderView.setTranslationY(dealtY);
                        } else {
                            tvStickyHeaderView.setTranslationY(0);
                        }
                    } else if (transViewStatus == MyAdapter.NONE_STICKT_VIEW) {
                        tvStickyHeaderView.setTranslationY(0);
                    }
                }
            }
        });
    }
    /*
     * 初始化view
     * */
    private void initView() {
        recyclerView = findViewById(R.id.recycle);
        tvStickyHeaderView = findViewById(R.id.tv_sticky_header_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        recyclerView.setAdapter(new MyAdapter(this, getData()));
    }

    public List<Bean> getData() {
        List<Bean> stickyExampleModels = new ArrayList<>();
        for (int index = 0; index < 100; index++) {
            if (index < 15) {
                stickyExampleModels.add(new Bean("哈哈哈1", "name" + index, "gender" + index));
            } else if (index < 25) {
                stickyExampleModels.add(new Bean("哈哈哈2", "name" + index, "gender" + index));
            } else if (index < 35) {
                stickyExampleModels.add(new Bean("哈哈哈3", "name" + index, "gender" + index));
            } else {
                stickyExampleModels.add(new Bean("哈哈哈4", "name" + index, "gender" + index));
            }
        }
        return stickyExampleModels;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值