android RecyclerView不会在BottomSheetBehavior内一直向下滚动

egdjgwm8  于 2023-02-27  发布在  Android
关注(0)|答案(1)|浏览(363)

我正在尝试创建一个底部工作表,其中包含按下并拖动时会更改BottomSheetBehaviour状态的视图。(随bottomSheet移动),该视图下是recyclerView,其中包含布局创建后可能会重新添加/删除的项目。
到目前为止,我所实现的是使recyclerView可滚动,但是如果bottomSheet处于STATE_SETTLING状态(在peekHeight时没有完全打开),它不会一直向下滚动,但是当它处于STATE_EXPANDED状态(在layout_height)时,它会向下滚动。我尝试将recyclerView的高度设置为wrap_content,但没有效果(我也读到它也不推荐)。
STATE_SETTLING状态下,如何允许recyclerView一直向下滚动?
这是密码:
活动-测试

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);
}

}
活动布局-活动_测试

<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>

底部表单布局-活动_测试_表单

<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>

回收器视图适配器-回收器视图适配器

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();
}
}

回收商查看项目-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>
7z5jn7bk

7z5jn7bk1#

默认情况下,RecyclerView“将其高度扩展到BottomSheet“的最底端,在BottomSheet扩展状态下完全显示其内容。
但在这里,您希望在半展开状态下完全显示其内容;需要决定RecyclerView底部的Expanded状态下显示什么内容;在另一个意义中,RecycerView底部边缘将与BottomSheet底部末端在膨胀状态下不相同;因为它已经占据了半膨胀状态。
如果你不想在底部添加任何东西,那么要在半展开状态下显示完整的内容,你必须用一些等于位移展开的填充来填充底部的间隙,使其从半展开状态变为展开状态。

The height of this gap almost = Bottom sheet height - peekHeight

把它涂在你的底片上:

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

因此,您需要向RecyclerView添加150 dp的底部填充,以使其内容在BottomSheet半展开状态下完全可见;但要确保禁用clipToPadding

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

注:我没有这个公式的证据,而不是尝试和错误。

相关问题