【问题标题】:RecyclerView won't scroll all the way down inside BottomSheetBehaviorRecyclerView 不会在 BottomSheetBehavior 中一直向下滚动
【发布时间】:2023-02-24 04:57:31
【问题描述】:

I am trying to create a bottom sheet that will contain view which when pressed and dragged changes bottomSheetBehaviour state. (moves with bottomSheet), under that view is recyclerView which contains items that may be readded/deleted after the layout was already created.

What I've achieved so far is to make the recyclerView scrollable, however it won't scroll all the way down if the bottomSheet is in STATE_SETTLING state (not fully open, at peekHeight) it will however when it's STATE_EXPANDED (at layout_height). I tried to set the height of recyclerView to wrap_content but it had no effect (also I've read it's not reccomended as well).

How do I allow the recyclerView to scroll all the way down while in STATE_SETTLING state?

This is the code :

Activity - Testing

public class Testing extends AppCompatActivity {

private RecyclerView recyclerView;
private LinearLayout bottomSheetLayout;
private BottomSheetBehavior<LinearLayout> sheetBehavior;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);

    recyclerView = findViewById(R.id.recyclerView);
    bottomSheetLayout = findViewById(R.id.bottomSheetLayout);
    sheetBehavior= BottomSheetBehavior.from(bottomSheetLayout);

    RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter();
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);

    recyclerView.setLayoutManager(mLayoutManager);

    recyclerView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                sheetBehavior.setDraggable(false);
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                sheetBehavior.setDraggable(true);
            }

            v.onTouchEvent(event);
            return true;
        }
    });
    recyclerView.setAdapter(recyclerViewAdapter);
}

}

Activity layout - activity_test

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include layout="@layout/activity_test_sheet"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

Bottom Sheet layout - activity_test_sheet

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bottomSheetLayout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="450dp"
    app:behavior_hideable="false"
    app:behavior_peekHeight="300dp"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

    <View
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/grey_25"
        android:padding="5dp"/>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:nestedScrollingEnabled="true"
        android:overScrollMode="never"
        android:scrollbars="none"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</LinearLayout>

RecyclerView Adapter - RecyclerViewAdapter

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private final List<Integer> dataList;

public RecyclerViewAdapter() {
    this.dataList = Arrays.asList(1,1,1,1,1,1,1,1,1);
}

public static class GeneralViewHolder extends RecyclerView.ViewHolder {
    private LinearLayout linearLayout;
    public GeneralViewHolder(@NonNull View itemView) {
        super(itemView);
        linearLayout = itemView.findViewById(R.id.linearLayout);
    }
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.test_rv_item,parent,false);
    return new GeneralViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
    setLayout((GeneralViewHolder) holder);
}

private void setLayout(GeneralViewHolder holder) {
    holder.linearLayout.setBackgroundResource(R.color.red_06);
}

@Override
public int getItemCount() {
    return dataList.size();
}
}

RecyclerView item - test_rv_item

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:orientation="vertical">

    <View
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:background="@color/grey_30"
        android:layout_gravity="top" />

    <View
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:background="@color/grey_40"
        android:layout_gravity="bottom"/>

</LinearLayout>

    标签: android android-layout android-recyclerview bottom-sheet


    【解决方案1】:

    默认情况下,RecyclerView“将其高度扩展到BottomSheet的最底端”完全显示其在BottomSheet中的内容展开状态.

    但是这里你想在Half Expanded状态下完全显示它的内容;这需要决定在展开状态下将显示什么;换句话说,RecycerView 底部边缘与展开状态下的 BottomSheet 底部边缘不同。

    如果你不想在底部添加任何东西,那么要在半展开状态下显示全部内容,你必须用一些等于位移展开的填充来填充底部间隙,以使其从一半展开到展开状态状态。

    半展开状态为 50%

    The height of this gap almost = Bottom sheet height - peak height.
    

    将其应用到您的底部工作表:

    The height of this gap almost = 450dp - 300dp = 150dp
    

    所以,你需要给RecyclerView添加一个150dp的bottom padding,让它的内容在BottomSheet半展开状态下完全可见;但一定要禁用clipToPadding

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:paddingBottom="150dp"
        android:clipToPadding="false"
        .... />
    

    注意:我没有这个公式的证据而不是反复试验。

    【讨论】:

      猜你喜欢
      • 2020-08-11
      • 1970-01-01
      • 2021-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多