简体   繁体   English

片段中的android底部工作表未将数据绑定到回收站视图

[英]android bottom sheet in fragment not binding data to recycler view

I have a bottom sheet with recycler view and when it initializes the item list is empty and once in another fragment user select an item the adapter will be updated and notify change but it is not working and nothing happens我有一个带有回收器视图的底部工作表,当它初始化时,项目列表是空的,一旦在另一个片段用户选择一个项目,适配器将被更新并通知更改,但它不起作用,什么也没有发生

this is the class fragment这是类片段

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

    }
}

//and the XML file looks like this it containes view page and on the view pager there is a recycler view which the user select an item and though the interface I update the bottom sheet recycler view adapter //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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM