![](/img/trans.png)
[英]How to update LinearLayout when clicking recycler view item in a bottom sheet fragment?
[英]android bottom sheet in fragment not binding data to recycler view
我有一個帶有回收器視圖的底部工作表,當它初始化時,項目列表是空的,一旦在另一個片段用戶選擇一個項目,適配器將被更新並通知更改,但它不起作用,什么也沒有發生
這是類片段
public class HomeFragment extends Fragment {
private BottomSheetBehavior mBottomSheetBehavior;
private RecyclerView mRecyclerView;
private ItemAdapter mAdapter = new ItemAdapter(new ArrayList<Items>());
private ArrayList<Items> billList = new ArrayList<>();
//this is the onCreateView code
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_menu, container, false);
View bottomSheet = root.findViewById(R.id.bottom_sheet);
mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
mBottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
}
});
mRecyclerView = root.findViewById(R.id.rv_cached_order_item);
mAdapter = new ItemAdapter(billList);
mRecyclerView.setAdapter(mAdapter);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(linearLayoutManager);
Button button = root.findViewById(R.id.btn_menu_order);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("TAG", "BottomSheetBehavior");
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
});
initUser(root);
return root;
}
// and this is where I notify the item change
public void addToBill(Items item) {
billList.add(item);
mAdapter.notifyDataSetChanged();
}
}
//XML文件看起來像這樣它包含視圖頁面,在視圖尋呼機上有一個回收器視圖,用戶選擇一個項目,雖然界面我更新了底部表回收器視圖適配器
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Activity.ui.home.HomeFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
tools:context=".MainActivity">
<com.google.android.material.tabs.TabLayout
android:id="@+id/menu_tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:tabBackground="@color/app_background"
app:tabIndicatorColor="@color/colorAccent"
app:tabMode="scrollable"
app:tabTextColor="@color/colorAccent">
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/menu_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/menu_tab" />
</androidx.constraintlayout.widget.ConstraintLayout>
<LinearLayout
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:fitsSystemWindows="true"
android:orientation="vertical"
app:behavior_hideable="true"
app:behavior_peekHeight="250dp"
app:layout_behavior="@string/bottom_sheet_behavior"
app:layout_constraintBottom_toBottomOf="parent">
<Button
android:id="@+id/btn_menu_order"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="0dp"
android:text="@string/order"
android:background="@color/app_background"
android:textColor="@color/colorAccent"
app:layout_constraintBottom_toTopOf="@+id/menu_coordinatorLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/menu_pager"
app:layout_constraintVertical_bias="1.0" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_cached_order_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
解決ItemAdapter靜態成員函數
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.