RecyclerView滑动时仿排名榜自动显示与隐藏自己的排名

类似于图1这种排行版,最下面的21是自己的排名,然后随着滑动到21时,变成图2.

上代码:

导包:

//adapter框架

implementation "com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.47",

TestActivity.java:
package com.example.mine.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.http.base.BaseViewJavaModel;
import com.example.mine.R;
import com.example.mine.adapter.LeagueAdapter;
import com.example.mine.bean.LeagueBean;
import com.example.mine.databinding.ActivityProfitsBinding;
import com.example.mine.databinding.ActivityTestBinding;
import com.example.mine.databinding.ItemLeagueBinding;
import com.example.view.base.BaseBindViewHolder;
import com.example.view.base.BaseJavaActivity;

import java.util.ArrayList;
import java.util.List;

public class TestActivity extends BaseJavaActivity<ActivityTestBinding, BaseViewJavaModel>  {

    private LeagueAdapter leagueAdapter;

    private boolean isFirst = true;

    @Override
    protected void initListener() {
        binding.recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                LinearLayoutManager layoutManager = (LinearLayoutManager) binding.recyclerView.getLayoutManager();
                if (layoutManager!=null){

                    int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();//当前页面最后一个可见的
                    int s = Integer.parseInt(binding.mineTvRanking.getText().toString());
                    if (lastVisibleItemPosition!=0 && s>3){
                        if (dy == 0 && lastVisibleItemPosition>(s-3) && isFirst){
                            binding.mineLlBottom.setVisibility(View.GONE);
                            leagueAdapter.notifyItemChanged(s-3);
                            isFirst = false;
                        }
                        if (lastVisibleItemPosition >= (s-3)){//数字大于了当前最后一个item
                            binding.mineLlBottom.setVisibility(View.GONE);
                            if (lastVisibleItemPosition == (s-3)){
                                leagueAdapter.notifyItemChanged(s-3);
                            }
                        }else {
                            binding.mineLlBottom.setVisibility(View.VISIBLE);
                        }

                    }
                }
            }
        });
    }

    @Override
    protected BaseViewJavaModel initViewModel() {
        return null;
    }

    @Override
    protected boolean isARouterInject() {
        return false;
    }

    @Override
    protected boolean useEventBus() {
        return false;
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_test;
    }

    @Override
    public void initData() {
        binding.recyclerView.setNestedScrollingEnabled(false);
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        List<LeagueBean> list = new ArrayList<>();
        list.add(new LeagueBean());
        list.add(new LeagueBean());
        list.add(new LeagueBean());
        list.add(new LeagueBean());
        list.add(new LeagueBean());
        list.add(new LeagueBean());
        list.add(new LeagueBean());
        list.add(new LeagueBean());
        list.add(new LeagueBean());
        list.add(new LeagueBean());
        list.add(new LeagueBean());
        list.add(new LeagueBean());
        list.add(new LeagueBean());
        list.add(new LeagueBean());
        list.add(new LeagueBean());
        list.add(new LeagueBean());
        list.add(new LeagueBean());
        list.add(new LeagueBean());
        list.add(new LeagueBean());
        list.add(new LeagueBean());
        list.add(new LeagueBean());
        list.add(new LeagueBean());
        list.add(new LeagueBean());
        list.add(new LeagueBean());
        list.add(new LeagueBean());
        list.add(new LeagueBean());
        list.add(new LeagueBean());
        list.add(new LeagueBean());
        list.add(new LeagueBean());
        list.add(new LeagueBean());
        binding.recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        leagueAdapter = new LeagueAdapter(list);
        binding.recyclerView.setAdapter(leagueAdapter);
        View head = LayoutInflater.from(this).inflate(R.layout.fragment_league_head, null);
        leagueAdapter.addHeaderView(head);
    }
    class LeagueAdapter extends BaseQuickAdapter<LeagueBean, BaseBindViewHolder> {


        public LeagueAdapter( @Nullable List<LeagueBean> data) {
            super(R.layout.item_league, data);
        }

        @Override
        protected void convert(@NonNull BaseBindViewHolder helper, LeagueBean item) {
            ItemLeagueBinding itembinding = (ItemLeagueBinding) helper.getBinding();
            itembinding.mineTvPosition.setText(helper.getAbsoluteAdapterPosition() + 3 + "");
        }
    }
}

 

 

activity_test.xml:
<?xml version="1.0" encoding="utf-8"?>
<layout>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginStart="10dp"
            android:layout_marginEnd="10dp"/>
        <LinearLayout
            android:id="@+id/mine_ll_bottom"
            android:background="#FFF5EE"
            android:layout_alignParentBottom="true"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            android:gravity="center_vertical">
            <TextView
                android:id="@+id/mine_tv_ranking"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="18dp"
                android:textColor="#999999"
                android:text="21"/>
            <ImageView
                android:layout_width="@dimen/dp_30"
                android:layout_height="@dimen/dp_30"
                android:layout_marginStart="15dp"
                android:src="@mipmap/iv_head"
                android:contentDescription="@string/app_name"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:textColor="@color/c_333333"
                android:text="子非鱼,亦非我"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginEnd="10dp"
                android:text="49,100"
                android:textColor="@color/color_f57f4b"
                android:gravity="end"/>
        </LinearLayout>
    </RelativeLayout>
</layout>
fragment_league_head.xml:
<?xml version="1.0" encoding="utf-8"?>
<layout>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@mipmap/league_bg"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="@dimen/dp_20"
                android:textSize="@dimen/sp_20"
                android:textColor="@color/white"
                android:text="利财铁军Top.100"/>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:layout_marginTop="@dimen/dp_70"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
                    <ImageView
                        android:layout_width="@dimen/dp_80"
                        android:layout_height="@dimen/dp_80"
                        android:contentDescription="@string/app_name"
                        android:src="@mipmap/iv_head"/>
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:contentDescription="@string/app_name"
                        android:src="@mipmap/tj_second"
                        android:layout_marginTop="-10dp"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@color/c_333333"
                        android:textSize="16sp"
                        android:layout_marginTop="10dp"
                        android:text="安之若素"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@color/color_f57f4b"
                        android:textSize="14sp"
                        android:layout_marginTop="2dp"
                        android:text="64,493"/>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:layout_marginTop="@dimen/dp_20"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
                    <ImageView
                        android:layout_width="@dimen/dp_100"
                        android:layout_height="@dimen/dp_100"
                        android:contentDescription="@string/app_name"
                        android:src="@mipmap/iv_head"/>
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:contentDescription="@string/app_name"
                        android:src="@mipmap/tj_first"
                        android:layout_marginTop="-10dp"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@color/c_333333"
                        android:textSize="16sp"
                        android:layout_marginTop="10dp"
                        android:text="安之若素"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@color/color_f57f4b"
                        android:textSize="14sp"
                        android:layout_marginTop="2dp"
                        android:text="64,493"/>
                </LinearLayout>
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:layout_marginTop="@dimen/dp_100"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
                    <ImageView
                        android:layout_width="@dimen/dp_60"
                        android:layout_height="@dimen/dp_60"
                        android:contentDescription="@string/app_name"
                        android:src="@mipmap/iv_head"/>
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:contentDescription="@string/app_name"
                        android:src="@mipmap/tj_third"
                        android:layout_marginTop="-10dp"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@color/c_333333"
                        android:textSize="16sp"
                        android:layout_marginTop="10dp"
                        android:text="安之若素"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@color/color_f57f4b"
                        android:textSize="14sp"
                        android:layout_marginTop="2dp"
                        android:text="64,493"/>
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</layout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值