我正在尝试创建一个底部工作表,其中包含视图,当按下和拖动时,该视图会改变
BottomSheetBehaviour
状态。 (与bottomSheet
一起移动),在该视图下是recyclerView
,其中包含可以在布局创建后重新添加/删除的项目。
我到目前为止所取得的成就是使
recyclerView
可滚动,但是如果bottomSheet
处于STATE_SETTLING状态(未完全打开,在peekHeight
)它不会一直向下滚动但是当它是 STATE_EXPANDED(在 layout_height)。我试图将 recyclerView 的高度设置为 wrap_content 但它没有效果(我也读过它也不被推荐)。
如何让
recyclerView
在STATE_SETTLING
状态下一直向下滚动?
这是代码:
活动-测试
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_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 适配器 - 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>
默认情况下,
RecyclerView
“将其高度扩展到BottomSheet
的最底端”完全显示其内容在BottomSheet
扩展状态.
但是这里你想在Half Expanded状态下完全显示它的内容;这需要决定在
RecyclerView
底部的展开状态下显示什么;换句话说,RecycerView
底边在展开状态下不会与BottomSheet
底端相同;因为它已经占据了半展开状态。
如果你不想在底部添加任何东西,那么为了在半展开状态下显示全部内容,你必须用一些等于位移展开的填充来填充底部间隙,以便从一半到展开状态。
The height of this gap almost = Bottom sheet height - peekHeight
将其应用到您的底部:
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"
.... />
注意:我没有这个公式的证据,而不是试错法。