Android滑动窗格布局关闭窗格()不再工作

xdnvmnnf  于 2023-03-06  发布在  Android
关注(0)|答案(1)|浏览(137)

我最近更新了几年前完成的一个项目。这个应用程序有一个片段和一个带有项目列表的滑动窗格,按下它会触发某个操作并关闭窗格。一旦片段打开,窗格也会关闭。

public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        performanceViewModel = new ViewModelProvider(this).get(Model_Performance.class);
        View root = inflater.inflate(R.layout.performance_fragment_layout, container, false);

        //setHasOptionsMenu(true); //DEPRECATED

        // Create a Sliding Pane
        slidingPane = root.findViewById(R.id.performance_sliding_pane_layout);

        slidingPane.addPanelSlideListener(new SlidingPaneLayout.SimplePanelSlideListener(){
            @Override
            public void onPanelOpened(View panel) {
                //Toast.makeText(panel.getContext(), "Opened", Toast.LENGTH_SHORT).show();
                panelOpened();
            }

            @Override
            public void onPanelClosed(View panel) {
                //Toast.makeText(panel.getContext(), "Closed", Toast.LENGTH_SHORT).show();
                panelClosed();
            }

            @Override
            public void onPanelSlide(View view, float v) { }
        });

        if (slidingPane != null) {
            if (slidingPane.isOpen())
            {
                Log.i("SlidePane","is closing........");
                slidingPane.closePane();
            }
        }

        view = root;

        // +++++++++++++++++++++++++++++++
        // Left sliding panel
        // +++++++++++++++++++++++++++++++

        // Lookup the recyclerview in activity layout
        rvPerformance = root.findViewById(R.id.rvPerformanceLeft);

        adapter = new PerformanceTableAdapter(appSingleton.getListOfPirepItemsShared(), this);
        // Attach the adapter to the recyclerview to populate items
        rvPerformance.setAdapter(adapter);
        // Set layout manager to position the items
        rvPerformance.setLayoutManager(new LinearLayoutManager(root.getContext()));
        // Add horizontal dividers
        rvPerformance.addItemDecoration(new DividerItemDecoration(root.getContext(), DividerItemDecoration.HORIZONTAL));

        txtPerformance = root.findViewById(R.id.txtPerformance);

        return root;
    }

问题是slidingPane.closePane();从来没有被调用过。这是我在控制台中得到的消息:

I/SlidePane: is closing........
W/System: A resource failed to call close.

有趣的是,当我试图用菜单按钮关闭它时,就像这样:

public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        MenuHost menuHost = requireActivity();

        menuHost.addMenuProvider(new MenuProvider() {
            @Override
            public void onCreateMenu(@NonNull Menu menu, @NonNull MenuInflater menuInflater) {
                menuInflater.inflate(R.menu.top_nav_menu_performance, menu);
            }

            @Override
            public boolean onMenuItemSelected(@NonNull MenuItem menuItem) {
                if(menuItem.getItemId()==android.R.id.home) {
                    // Opening and closing the Sliding Pane
                    if (slidingPane != null) {
                        if (!slidingPane.isOpen()) { slidingPane.openPane(); }
                        else { slidingPane.closePane(); }
                    }
                }
                return true;
            }
        }, getViewLifecycleOwner(), Lifecycle.State.RESUMED);
    }

......窗玻璃关得很好。以前什么都能用。
先谢了!
编辑:
closePane();只是在片段的onCreateView或者onViewCreated里面没有调用,一个按钮点击就调用的很好,快把我逼疯了......以防万一,布局如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.slidingpanelayout.widget.SlidingPaneLayout
    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:background="@color/background_dark_gray"
    android:id="@+id/performance_sliding_pane_layout"
    tools:context=".ui.performance.PerformanceFragment">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        tools:context=".ui.performance.PerformanceFragment">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rvPerformanceLeft"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginTop="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
    </androidx.constraintlayout.widget.ConstraintLayout>

    <ScrollView
        android:id="@+id/scrollviewPerformance"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:fillViewport="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="end" >

            <TextView
                android:id="@+id/txtPerformance"
                android:textSize="16sp"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="20dp"
                android:layout_marginTop="30dp"
                android:layout_marginEnd="20dp"
                android:layout_marginBottom="20dp"
                android:layout_weight="1"
                android:textColor="@color/white"
                android:text="@string/not_available" />
        </LinearLayout>
    </ScrollView>

</androidx.slidingpanelayout.widget.SlidingPaneLayout>
s71maibg

s71maibg1#

找到了问题所在。实际上,主滑动窗格是右边的窗格,即包含详细信息内容的窗格。要用列表关闭左窗格,需要调用slidingPane.open(),而不是slidingPane.closePane()

相关问题