【发布时间】: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