Android - 在BottomSheetDialog Fragment

时间:2017-02-06 05:58:45

标签: java android android-fragments android-viewpager bottom-sheet

我有一个 BottomSheetDialogFragment ,TicketDashboard有以下代码

使用ButterKnife注入视图,将适配器设置为ViewPager时出错。错误表示 R.id.ticket_dashboard_view_pager 没有可用的视图。 Stacktrace附在最后。

@Bind(R.id.ticket_dashboard_view_pager)
ViewPager mViewPager;

@Override
public void setupDialog(Dialog dialog, int style) {
    super.setupDialog(dialog, style);
    View contentView = View.inflate(getContext(), R.layout.fragment_ticket_dashboard, null);
    dialog.setContentView(contentView);
    ButterKnife.bind(this, contentView);

    CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) contentView.getParent()).getLayoutParams();
    CoordinatorLayout.Behavior behavior = params.getBehavior();

    if( behavior != null && behavior instanceof BottomSheetBehavior ) {
        mBottomSheetBehavior = (BottomSheetBehavior) behavior;
        mBottomSheetBehavior.setBottomSheetCallback(mBottomSheetBehaviorCallback);
        mBottomSheetBehavior.setPeekHeight(470);
    }
}

@Override
public void onStart(){
    super.onStart();
    mToolBar.setTitle("Ticket# " + ticket.getTicketNumber());
    mToolBar.setNavigationIcon(getActivity().getDrawable(R.drawable.ic_arrow_upward_white_24dp));
    mToolBar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(isExpanded) {
                mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
            }else{
                mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
            }
        }
    });
    setupViewPager();
    mBottomNavigation.getMenu().getItem(0).setChecked(true);
}

private void setupViewPager(){
    setUpPages();
    mPagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());
    **mViewPager.setAdapter(mPagerAdapter);** // Line at which iam getting error
    mViewPager.addOnPageChangeListener(this);
}

XML

<RelativeLayout 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"
xmlns:design="http://schema.android.com/apk/res/android.support.design"
android:layout_width="match_parent"
android:layout_height="match_parent">

//ToolBar

<android.support.design.widget.AppBarLayout
    android:id="@+id/toolbar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="5dp"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/ticket_dashboard_view_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/toolbar_layout"
    android:background="@android:color/transparent"
    android:focusable="true"
    android:focusableInTouchMode="true" />

<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottom_navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    app:itemBackground="@color/colorPrimary"
    app:itemIconTint="@drawable/nav_item_color_state"
    app:itemTextColor="@drawable/nav_item_color_state"
    app:menu="@menu/ticket_dashboard_menu" />
</RelativeLayout>

栈跟踪

  

流程:io.webguru.ticketing,PID:29663                                                                         java.lang.IllegalArgumentException:找不到id 0x7f0f01ca的视图   (io.webguru.ticketing:id / ticket_dashboard_view_pager)片段   TicketOverviewFragment {bb97f0f#4 id = 0x7f0f01ca}

谢谢

2 个答案:

答案 0 :(得分:0)

尝试用getChildFragmentManager()

替换getFragmentManager
private void setupViewPager(){
    setUpPages();
    mPagerAdapter = new ScreenSlidePagerAdapter(getChildFragmentManager());
    **mViewPager.setAdapter(mPagerAdapter);** // Line at which iam getting error
    mViewPager.addOnPageChangeListener(this);
}

答案 1 :(得分:0)

不要在setUpDialog上夸大布局,而是在onCreateView

中进行
var lastNameText = document.querySelector('.last-name').textContent;
var firstName = document.querySelector('.first-name');

firstName.style.fontSize = (120 / lastNameText.length) + "px";