底部向下滑动行为

时间:2016-12-19 16:06:13

标签: android swipe-gesture bottom-sheet

我已在ListView内成功创建Fragment。从列表中我还成功加载了底页。

  1. 如果我点击“One”列表中的第一项,则会显示底部工作表。

  2. 如果我在底部纸张外面点击,则底部纸张消失。

  3. 如果我点击列表“Two”上的第二项,则会再次显示底页。

  4. 如果我再次做1),然后向下滑动底部纸张并尝试做3),屏幕保持灰色,好像底部纸张仍然显示但不可见...... ??? / p>

  5. 我该如何纠正?

    以下是我的代码。

    SecondFragment.java

    /**
     * A simple {@link Fragment} subclass.
     */
    public class SecondFragment extends Fragment {
        public SecondFragment() {
            // Required empty public constructor
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
    
            View view = inflater.inflate(R.layout.fragment_second, container, false);
    
            String[] faList = { 
                    "One",
                    "Two",
            };
    
            ListView listView = (ListView) view.findViewById(R.id.listFa);
    
            ArrayAdapter<String> listviewAdapter = new ArrayAdapter<String>(
                    getActivity(),
                    android.R.layout.simple_list_item_1,
                    faList
            );
    
            listView.setAdapter(listviewAdapter);
    
            // Inflate the layout for this fragment
            // return view;
    
    
            final BottomSheetDialog bottomsheet = new BottomSheetDialog(this.getContext());
            View bottomSheetView = inflater.inflate(R.layout.bottom_sheet, null);
            bottomsheet.setContentView(bottomSheetView);
    
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    
                    bottomsheet.show();
                }
            });
            return view;
        }
    }
    

    fragment_second.xml

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.ifavaz.talker.SecondFragment">
    
        <!-- TODO: Update blank fragment layout -->
        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/listFa" />
    </FrameLayout>
    

    bottom_sheet.xml

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="right"
        app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
    
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:padding="30dp">
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    android:layout_marginTop="30dp">
    
                    <FrameLayout
                        android:layout_width="280dp"
                        android:layout_height="wrap_content">
    
                        <TextView
                            android:id="@+id/textView2"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="text is my text is my text text is my text is my text text is my text is my text text is my text is my text "
                            android:textSize="18sp"
                            android:textAlignment="textStart"
                            android:paddingRight="30dp" />
                    </FrameLayout>
    
                </LinearLayout>
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
    
                    android:orientation="horizontal"
                    android:layout_marginTop="30dp">
    
                    <FrameLayout
                        android:layout_width="280dp"
                        android:layout_height="wrap_content">
    
                        <TextView
                            android:id="@+id/textView3"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="text is my text is my text text is my text is my text text is my text is my text text is my text is my text "
                            android:textSize="18sp"
                            android:paddingRight="30dp" />
                    </FrameLayout>
    
                </LinearLayout>
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    android:layout_marginTop="30dp">
    
                    <FrameLayout
                        android:layout_width="280dp"
                        android:layout_height="wrap_content">
    
                        <TextView
                            android:id="@+id/textView2"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="text is my text is my text text is my text is my text text is my text is my text text is my text is my text "
                            android:textSize="18sp"
                            android:textAlignment="textStart"
                            android:paddingRight="30dp" />
                    </FrameLayout>
    
                </LinearLayout>
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    android:layout_marginTop="30dp">
    
                    <FrameLayout
                        android:layout_width="280dp"
                        android:layout_height="wrap_content">
    
                        <TextView
                            android:id="@+id/textView2"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="text is my text is my text text is my text is my text text is my text is my text text is my text is my text "
                            android:textSize="18sp"
                            android:textAlignment="textStart"
                            android:paddingRight="30dp" />
                    </FrameLayout>
                </LinearLayout>
            </LinearLayout>
        </ScrollView>
    </FrameLayout>
    

1 个答案:

答案 0 :(得分:1)

我从这个post开始工作,你需要作为你的片段/活动中的根布局一个带有行为的CoordinatorLayout - &gt; “android.support.design.widget.BottomSheetBehavior” ,它的工作方式是扩展BottomSheetDialogFragment并从你的活动/片段中显示它

    TutsPlusBottomSheetDialogFragment fragment = new TutsPlusBottomSheetDialogFragment();
    fragment.show(getSupportFragmentManager(),bottomSheetDialogFragment.getTag());

此自定义DialogFragment的实现是:

    public class TutsPlusBottomSheetDialogFragment extends BottomSheetDialogFragment {

        private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {

        @Override
        public void onStateChanged(@NonNull View bottomSheet, int newState) {
            if (newState == BottomSheetBehavior.STATE_HIDDEN) {
                dismiss();
            }

        }

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

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

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

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

}