效果图如下:
布局文件:
<?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"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/x100"
android:layout_marginStart="@dimen/x20"
android:layout_marginEnd="@dimen/x20"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/login_module_clean_history_num_bg"
android:paddingTop="@dimen/x100"
android:paddingBottom="@dimen/x4"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list_input_histroy"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:cacheColorHint="#DDDDDD"
android:divider="@null"
android:dividerHeight="@dimen/x0"
android:filterTouchesWhenObscured="false"
android:listSelector="@drawable/login_module_history_num_selector"
android:layout_marginStart="@dimen/x34"
android:visibility="visible"
tools:itemCount="3"
tools:listitem="@layout/login_module_login_number"
tools:visibility="visible" />
<RelativeLayout
android:layout_marginStart="@dimen/x24"
android:layout_width="@dimen/x6"
android:layout_height="@dimen/x178"
android:background="@drawable/login_module_scroll_bar_track_v"
>
<View
android:id="@+id/main_line"
android:layout_width="match_parent"
android:layout_height="@dimen/x100"
android:background="@drawable/login_module_scroll_bar_thumb_v"
android:layout_gravity="center_vertical"/>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/user_phoneno_rel"
android:layout_width="match_parent"
android:layout_height="@dimen/x100"
android:background="@drawable/login_module_bottom_ling_bg_new"/>
</RelativeLayout>
</RelativeLayout>
逻辑处理:
package com.lahm.learndaemon.activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.lahm.learndaemon.R;
import com.lahm.learndaemon.adapter.BaseHistoryAdapter;
import java.util.ArrayList;
import java.util.List;
/**
* ================================================
* Description:Recycleview的多种视图类型
* <p>
* Created by mz on 2023/7/19 15:08
* ================================================
*/
public class RecycleviewScrollbarActivity extends BaseActivity{
private RecyclerView listInputHistory;
private BaseHistoryAdapter hisAdapter;
private View main_line;
private float dimension;
List<String> numberData =new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycleview_scrollbar);
listInputHistory = findViewById(R.id.list_input_histroy);
main_line = findViewById(R.id.main_line);
listInputHistory.setLayoutManager(new LinearLayoutManager(this));
numberData.add("18340833784");
numberData.add("18340833782");
numberData.add("18340833781");
numberData.add("18340833783");
numberData.add("18340833783");
numberData.add("18340833783");
numberData.add("18340833783");
dimension = getResources().getDimension(R.dimen.x178);
ViewGroup.LayoutParams layoutParams = main_line.getLayoutParams();
layoutParams.height= (int) (dimension*3/numberData.size());
main_line.setLayoutParams(layoutParams);
if (hisAdapter == null) {
hisAdapter = new BaseHistoryAdapter(numberData,true, new BaseHistoryAdapter.BaseHistoryAdapterClickListener() {
@Override
public void onClear(int position) {
setPhoneNumberHistoryHeight();
}
@Override
public void onClick(String text, int position) {
// listInputHistory.setVisibility(View.GONE);
}
});
listInputHistory.setAdapter(hisAdapter);
} else {
hisAdapter.setList(numberData);
}
setPhoneNumberHistoryHeight();
}
/**
* 设置历史登录账号列表高度
* 1、若总条目小于等于3条,则全部显示
* 2、最多显示三个条目,其余可滑动显示
*/
private void setPhoneNumberHistoryHeight() {
//总条目小于等于3显示全部
if (hisAdapter == null || hisAdapter.getItemCount() <= 3) {
ViewGroup.LayoutParams params = listInputHistory.getLayoutParams();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
listInputHistory.setLayoutParams(params);
return;
}
listInputHistory.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
listInputHistory.getViewTreeObserver().removeOnGlobalLayoutListener(this);
//总条目大于3,只显示三条,其余可滑动查看
int historyHeight = 0;
LinearLayoutManager layoutManager = (LinearLayoutManager) listInputHistory.getLayoutManager();
if (layoutManager != null) {
View childView = layoutManager.findViewByPosition(layoutManager.findFirstVisibleItemPosition());
if (childView != null)
historyHeight = childView.getHeight() * 3;
}
ViewGroup.LayoutParams params = listInputHistory.getLayoutParams();
//由于分割线写在了item里面,所以这里不需要加上分割线高度
params.height = historyHeight;
listInputHistory.setLayoutParams(params);
}
});
listInputHistory.addOnScrollListener(new RecyclerView.OnScrollListener() {
float endX = 0;
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
// LinearLayoutManager layoutManager = (LinearLayoutManager) listInputHistory.getLayoutManager();
// int last = layoutManager.findLastCompletelyVisibleItemPosition();
// float heightScale = (last + 1) / hisAdapter.getItemCount();
// //设置滚动条移动
// float height = heightScale * dimension - dimension / 3;
// main_line.setTranslationY(height);
//需要滑动的高度
int range =listInputHistory.computeVerticalScrollRange()- listInputHistory.getHeight();
//滑动的距离
endX += dy;
//计算比例
float proportion = endX / range;
//计算滚动条高度
int transMaxRange = ((ViewGroup) main_line.getParent()).getHeight() - main_line.getHeight();
Log.d("mz--", "range: "+range+",endX:"+endX+
",proportion:"+proportion+",transMaxRange:"+transMaxRange);
//设置滚动条移动
main_line.setTranslationY(transMaxRange * proportion);
super.onScrolled(recyclerView, dx, dy);
}
});
}
}
BaseHistoryAdapter自己定义一个即可,横向的可以根据本例子对比修改