Recycler下方跟随显示一个Button,显示内容超出屏幕时,按钮固定在底部显示

在开发中有这样的需求,列表下面跟随显示一个按钮,当列表的item很多时,按钮会超出屏幕显示,这时要求按钮固定在屏幕底部显示。

网上大部分的解决方案是,在用LinearLayout包裹RecyclerView 和 Button,外层LinearLayout 设置android:layout_height=“wrap_content”, 给RecyclerView设置android:layout_weight=“1” android:layout_height=“0dp”,给Button部分设置android:layout_height=“wrap_content”。但是我这样设置,在item条目比较少时,按钮部分还是显示在屏幕底部的。

我找到最佳的解决方案还是通过代码动态控制,首先是解决思路的转变,上述需求的真正目的就是当item少量的时候按钮距离列表要近一点。由此我们可以把按钮设置成一直在屏幕底部,item很少的时候,给按钮设置margin或padding让按钮离列表近一点。

然后问题就分解成:一、什么时候去设置margin或padding?二、margin或padding的值设置成多少?

如下图所示,RecyclerView底部到屏幕顶部的距离是recyclerBottomY,为h1+h2的距离,底部Button顶部到屏幕顶部的距离为layoutButtonTopY,给RecyclerView到Button留一定间隙gapHeight。那么当layoutButtonTopY减去recyclerBottomY 大于gapHeight时,就需要给button部分设置margin或者padding使按钮上移,设置的大小即layoutButtonTopY-recyclerBottomY-gapHeight

请添加图片描述

以下是具体代码:

private fun resetBottomLayout(){
    binding.recyclerView.postDelayed({
        val scrollRange = binding.recyclerView.computeVerticalScrollRange()
        val recyclerIntArray = IntArray(2)
        binding.recyclerView.getLocationOnScreen(recyclerIntArray)
        val recyclerBottomY = recyclerIntArray[1]+scrollRange
        val layoutButtonIntArray = IntArray(2)
        binding.layoutDealButton.getLocationOnScreen(layoutButtonIntArray)
        val layoutButtonTopY = layoutButtonIntArray[1]
        if (layoutButtonTopY-recyclerBottomY>SizeUtils.dp2px(30f)){
            binding.layoutBottomButton.setPadding(0,0,0,layoutButtonTopY-recyclerBottomY-SizeUtils.dp2px(30f))
        }
    }, 300)
}

布局代码为:

<?xml version="1.0" encoding="utf-8"?>

<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/content_color"
    android:orientation="vertical"
    android:paddingStart="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingEnd="@dimen/activity_horizontal_margin"
    android:paddingBottom="@dimen/activity_vertical_margin">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp"/>

    <androidx.appcompat.widget.LinearLayoutCompat
        android:id="@+id/layout_bottom_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/recycler_deal_stream">
        <Button
            android:layout_weight="1"
            android:id="@+id/btnStart"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <Button
            android:layout_weight="2"
            android:id="@+id/btnEnd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </androidx.appcompat.widget.LinearLayoutCompat>
</androidx.appcompat.widget.LinearLayoutCompat>

inearLayoutCompat>
</androidx.appcompat.widget.LinearLayoutCompat>


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
是的,这段 XML 代码中只是定义了页面的布局结构,没有具体的实现逻辑。如果要实现4个button按钮显示,你需要在相应的 Activity 或 Fragment 中编写代码来处理 RecyclerView 的数据和适配器。同样地,你也需要通过代码来实现4个不同的 Fragment 页面,并在点击不同的按钮切换到相应的 Fragment。 以下是示例代码,用于在 RecyclerView显示4个 button,以及在点击不同的 button 切换到不同的 Fragment 页面。请在相应的 Activity 或 Fragment 中添加此代码,以实现完整的页面: ```java public class MainActivity extends AppCompatActivity implements MyAdapter.OnItemClickListener { private RecyclerView recyclerView; private MyAdapter adapter; private List<String> buttonList; private Fragment1 fragment1; private Fragment2 fragment2; private Fragment3 fragment3; private Fragment4 fragment4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 初始化 RecyclerView recyclerView = findViewById(R.id.recycler_view); recyclerView.setLayoutManager(new LinearLayoutManager(this)); // 初始化 Button 数据 buttonList = new ArrayList<>(); buttonList.add("Button 1"); buttonList.add("Button 2"); buttonList.add("Button 3"); buttonList.add("Button 4"); // 初始化 RecyclerView 适配器 adapter = new MyAdapter(buttonList); recyclerView.setAdapter(adapter); adapter.setOnItemClickListener(this); // 初始化 Fragment fragment1 = new Fragment1(); fragment2 = new Fragment2(); fragment3 = new Fragment3(); fragment4 = new Fragment4(); // 默认显示一个 Fragment getSupportFragmentManager().beginTransaction() .replace(R.id.fragment_container, fragment1) .commit(); } @Override public void onItemClick(int position) { // 根据点击的位置切换到不同的 Fragment switch (position) { case 0: getSupportFragmentManager().beginTransaction() .replace(R.id.fragment_container, fragment1) .commit(); break; case 1: getSupportFragmentManager().beginTransaction() .replace(R.id.fragment_container, fragment2) .commit(); break; case 2: getSupportFragmentManager().beginTransaction() .replace(R.id.fragment_container, fragment3) .commit(); break; case 3: getSupportFragmentManager().beginTransaction() .replace(R.id.fragment_container, fragment4) .commit(); break; default: break; } } // RecyclerView 适配器 private class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { private List<String> buttonList; private OnItemClickListener listener; public MyAdapter(List<String> buttonList) { this.buttonList = buttonList; } public void setOnItemClickListener(OnItemClickListener listener) { this.listener = listener; } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.button_item, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(@NonNull ViewHolder holder, int position) { holder.button.setText(buttonList.get(position)); } @Override public int getItemCount() { return buttonList.size(); } public class ViewHolder extends RecyclerView.ViewHolder { public Button button; public ViewHolder(View view) { super(view); button = view.findViewById(R.id.button); button.setOnClickListener(v -> { if (listener != null) { listener.onItemClick(getAdapterPosition()); } }); } } } // RecyclerView 点击监听器 public interface OnItemClickListener { void onItemClick(int position); } } ``` 在上述代码中,我们创建了一个 MyAdapter 适配器,用于将数据绑定到 RecyclerView 上。通过 setOnItemClickListener() 方法,我们还为 MyAdapter 设置了一个点击监听器,用于在点击不同的 button 切换 Fragment。在 onItemClick() 方法中,我们使用 FragmentTransaction 将不同的 Fragment 显示到页面上。 为了方便起见,我们还创建了一个 OnItemClickListener 接口,用于在点击 RecyclerView 中的 item 回调到 Activity 或 Fragment 中。在 MyAdapter 中,我们通过 ViewHolder 中的 button.setOnClickListener() 方法将点击事件与 OnItemClickListener 关联起来。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值