Android底表上的静态页脚视图

问题描述 投票:3回答:1

我正在尝试在Android的底部表格上创建静态页脚。以下是我当前尝试的GIF图片。完全展开时,请注意其底部的footer文本。无论页面处于什么状态,我都希望此页脚文本显示为[[always。(例如,如果仅将页面扩展一半,则页脚仍应显示。当页面正在扩展/折叠时,页脚仍应总是显示等。)如何完成此行为?我看了一下:https://github.com/umano/AndroidSlidingUpPanel,但我希望得到一个不涉及任何外部库的普通android解决方案。

enter image description here

这是我当前的XML。我认为问题在于页脚固定在视图的底部,但是只有在工作表完全展开时,视图的底部才可见。我想看看无论页面状态如何,都可以显示页脚:

<?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:airbnb="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:focusableInTouchMode="true" android:orientation="vertical" android:id="@+id/linear_layout_container"> <TextView android:id="@+id/header_text" android:layout_width="match_parent" android:text="Header" android:layout_height="36dp" android:layout_alignParentTop="true" /> <com.airbnb.epoxy.EpoxyRecyclerView android:id="@+id/bottom_sheet_recycler_view" android:layout_width="match_parent" android:layout_height="wrap_content" airbnb:layoutManager="LinearLayoutManager" android:layout_above="@+id/footer_text" android:layout_below="@+id/header_text" /> <TextView android:id="@+id/footer_text" android:layout_width="match_parent" android:text="Footer" android:layout_height="36dp" android:layout_alignParentBottom="true" airbnb:layout_anchorGravity="bottom" /> </RelativeLayout> </androidx.coordinatorlayout.widget.CoordinatorLayout>

这是我的片段的样子。只是一个空白片段,显示了一个底页:ContainerFragment.kt

import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.Fragment import androidx.recyclerview.widget.LinearLayoutManager import com.airbnb.epoxy.EpoxyRecyclerView import com.google.android.material.bottomsheet.BottomSheetDialog class ContainerFragment : Fragment() { override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { // Inflate the layout for this fragment return inflater.inflate(R.layout.container_fragment_layout, container, false) } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) view.alpha = 0f val bottomSheet = BottomSheetDialog(requireContext()) val bottomSheetView = LayoutInflater.from(context).inflate(R.layout.test_bottom_sheet_layout, null) bottomSheet.setContentView(bottomSheetView) bottomSheet.show() val epoxyRecyclerView : EpoxyRecyclerView = bottomSheetView.findViewById(R.id.bottom_sheet_recycler_view) epoxyRecyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false) epoxyRecyclerView.withModels { (0..100).forEach { basicRow { id(it) title("This is the entry at index $it.") } } } } companion object { fun newInstance() = ContainerFragment() } }

这是我的片段布局:container_fragment_layout.xml

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

android android-layout android-fragments bottom-sheet
1个回答
0
投票
您需要以编程方式进行此操作,但实际上比看起来要简单。只需在BottomSheet滑动时调整视图的位置即可。

    将页脚视图添加为BottomSheet的直接子级
  • <androidx.coordinatorlayout.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/bottom_sheet" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#EECCCCCC" app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"> <TextView android:id="@+id/pinned_bottom" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#500000FF" android:padding="16dp" android:text="Bottom" /> </FrameLayout> </androidx.coordinatorlayout.widget.CoordinatorLayout>
      在onSlide()中添加BottomSheetCallback并调整translationY
  • BottomSheetBehavior.from(bottom_sheet).addBottomSheetCallback( object : BottomSheetBehavior.BottomSheetCallback() { override fun onSlide(bottomSheet: View, slideOffset: Float) { val bottomSheetVisibleHeight = bottomSheet.height - bottomSheet.top pinned_bottom.translationY = (bottomSheetVisibleHeight - pinned_bottom.height).toFloat() } override fun onStateChanged(bottomSheet: View, newState: Int) { } })
    它运行平稳,因为您只是更改了翻译Y。

    您还可以使用此技术将视图固定在BottomSheet的中心:

    pinned_center.translationY = (bottomSheetVisibleHeight - pinned_center.height) / 2f

    我已经在GitHub上创建了一个示例项目来重现两个用例(固定在中心和底部):https://github.com/dadouf/BottomSheetGravity

    Demo

  • © www.soinside.com 2019 - 2024. All rights reserved.