簡體   English   中英

看不到片段內的底部工作表

[英]Can't see Bottom Sheet inside fragment

我有一個viewpager,里面有幾個片段。 我想在其中一個片段中添加一個底頁。 讓我們稱之為report_fragment。 片段和底部工作表的布局是:

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layoutDirection="rtl">

    <include layout="@layout/report_page_content" />

    <!-- include the bottom sheet -->
    <iclude layout="@layout/report_bottom_sheet_table" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

report_page_content 布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/report_page_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
    />
</LinearLayout>

底部工作表布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/report_bottom_sheet"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    app:behavior_hideable="true"
    app:behavior_peekHeight="120dp"
    android:background="@color/report_bottom_sheet_bck_color"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:gravity="center"
        android:text="@string/daily_bs_tests"
        />
</LinearLayout>

片段的java代碼是:

    public class FragmentReportPage extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.report_page_content, container, false);

        // Init other views ....

        // LinearLayout bottomSheetLayout = v.findViewById(R.id.report_bottom_sheet);

        // If i uncomment below line, an exception will arise
        // BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetLayout);
        // bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);

        return v;
    }

不幸的是,片段中沒有顯示底頁。 我做錯了什么?

在你的畢業

implementation 'com.google.android.material:material:1.1.0-alpha09'

風格

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

主要活動.java

public class Main2Activity extends AppCompatActivity {
    private ViewPager mPager;


    private PagerAdapter pagerAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mPager = (ViewPager) findViewById(R.id.vpPager);
        pagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(pagerAdapter);
    }

    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
        public ScreenSlidePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return new ItemFragment();
                default:
                    return new ItemFragment();
            }
        }

        @Override
        public int getCount() {
            return 1;
        }
    }
}

活動_main.xml

<androidx.constraintlayout.widget.ConstraintLayout 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:fitsSystemWindows="true"
    tools:context=".Main2Activity">

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/vpPager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

項目片段.java

public class ItemFragment extends Fragment {

    View view;
    BottomSheetBehavior behavior;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_item_list, container, false);
        LinearLayout bottomSheet = view.findViewById(R.id.botttomsheet);
        behavior = BottomSheetBehavior.from(bottomSheet);
        behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {

            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {

            }
        });
        RecyclerView recyclerView = view.findViewById(R.id.list);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(new MyItemRecyclerViewAdapter(DummyContent.ITEMS));

        return view;
    }

}

fragment_item_list.xml

<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:fitsSystemWindows="true">
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/list"
        android:name="com.andy.faceread.fragment.ItemFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        app:layoutManager="LinearLayoutManager"
        tools:context=".fragment.ItemFragment"
        tools:listitem="@layout/fragment_item" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:id="@+id/botttomsheet"
        app:behavior_hideable="false"
        app:behavior_peekHeight="80dp"
        android:orientation="vertical"
        android:clickable="true"
        android:focusable="true"
        android:background="@color/colorAccent"
        app:layout_behavior="@string/bottom_sheet_behavior">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_marginTop="20dp"
            android:textSize="20dp"
            android:textColor="@android:color/white"
            android:text="bottomsheet"/>
    </LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM