如何在片段类而不是活动类中使用底页? [英] How to use bottom sheet in fragment class instead of Activity Class?

查看:60
本文介绍了如何在片段类而不是活动类中使用底页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在点击侦听器中使用底部表单,但此行出现错误.

I am trying to use the bottom sheet in a click listener but I am getting an error on this line.

bottomSheetFragment.show(getSupportFragmentManager())

无法解析方法'show(?, java.lang.String)'无法解析方法'getSupportFragmentManager()

Cannot resolve method 'show(?, java.lang.String)' Cannot resolve method 'getSupportFragmentManager()

我想在片段类中使用底页.

I want to use the bottom sheet in a fragment class.

SubCategoryDe​​tailFragment.java

  public class SubCategoryDetailFragment extends Fragment {

        TextView txtv_sort;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view= inflater.inflate(R.layout.fragment_sub_category_detail, container, false);
            toolbar = ((MainActivity) getActivity()).findViewById(R.id.toolbar);
            toggle = ((MainActivity) getActivity()).getToggle();
            shimmerContainer = view.findViewById(R.id.shimmer_view_container);
            recyclerView_subcatDetail = view.findViewById(R.id.recycler_view_subCategoryDetail);

           txtv_sort = view.findViewById(R.id.txtv_sort);

            toggle.setDrawerIndicatorEnabled(false);
            toggle.setHomeAsUpIndicator(R.drawable.back);
            toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getActivity().onBackPressed();
                }
            });


         txtv_sort.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BottomSheetFragment bottomSheetFragment = new BottomSheetFragment();
                    bottomSheetFragment.show(getSupportFragmentManager(), bottomSheetFragment.getTag());
                }
            });

            return view;
        }

    }

BottomSheetFragment.java

public class BottomSheetFragment extends Fragment {


    public BottomSheetFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_bottom_sheet, container, false);
    }

}

fragment_bottom_sheet.xml

<LinearLayout 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"
    android:orientation="vertical"
    android:paddingBottom="8dp"
    android:paddingTop="8dp"
    tools:context=".Fragments.BottomSheetFragment">

    <!-- NOTE: This list should be displayed in a list
    instead of nested layouts -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:foreground="?attr/selectableItemBackground"
        android:orientation="horizontal"
        android:paddingBottom="8dp"

        android:paddingTop="8dp">

        <ImageView
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_marginRight="32dp"
            android:src="@drawable/ic_launcher_background"
            android:tint="#737373" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="Preview"
            android:textColor="#737373"
            android:textSize="16sp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:foreground="?attr/selectableItemBackground"
        android:orientation="horizontal"
        android:paddingBottom="8dp"

        android:paddingTop="8dp">

        <ImageView
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_marginRight="32dp"
            android:src="@drawable/ic_launcher_background"
            android:tint="#737373" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="Share"
            android:textColor="#737373"
            android:textSize="16sp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:foreground="?attr/selectableItemBackground"
        android:orientation="horizontal"
        android:paddingBottom="8dp"

        android:paddingTop="8dp">

        <ImageView
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_marginRight="32dp"
            android:src="@drawable/ic_launcher_background"
            android:tint="#737373" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="Get link"
            android:textColor="#737373"
            android:textSize="16sp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:foreground="?attr/selectableItemBackground"
        android:orientation="horizontal"
        android:paddingBottom="8dp"

        android:paddingTop="8dp">

        <ImageView
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_marginRight="32dp"
            android:src="@drawable/ic_launcher_background"
            android:tint="#737373" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="Make a Copy"
            android:textColor="#737373"
            android:textSize="16sp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:foreground="?attr/selectableItemBackground"
        android:orientation="horizontal"
        android:paddingBottom="8dp"

        android:paddingTop="8dp">

        <ImageView
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_marginRight="32dp"
            android:src="@drawable/ic_launcher_background"
            android:tint="#737373" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="Email a Copy"
            android:textColor="#737373"
            android:textSize="16sp" />
    </LinearLayout>
</LinearLayout>

推荐答案

是的,您可以像下面的代码一样,在 Fragment 中使用 BottomSheetDialogFragment

Yes, you can use BottomSheetDialogFragment in Fragment simply like below code,

BottomSheetDialogFragment bottomSheetFragment = new YourBottomSheetFragmentClass();
bottomSheetFragment.show(getFragmentManager(), bottomSheetFragment.getTag());

或者,如果您要将某些数据传递给 BottomSheetDialogFragment ,请使用以下代码,并通过 newInstance 发送和检索数据.

Or if you want to pass some data to BottomSheetDialogFragment use below code, with newInstance you can send and retrieve data.

在您的片段类中:

BottomSheetDialogFragment myBottomSheet = YourBottomSheetFragmentClass.newInstance(SendString);            
myBottomSheet.show(getFragmentManager(),myBottomSheet.getTag());

在您的BottomSheetFragment类中,添加以下行

In your BottomSheetFragment class, add below line

static YourBottomSheetFragmentClass newInstance(String retrieveString) {
        YourBottomSheetFragmentClass f = new YourBottomSheetFragmentClass();
        Bundle args = new Bundle();
        args.putString("getString", retrieveString);
        f.setArguments(args);
        return f;
            return new f();
        }

还通过 BottomSheetDialogFragment

这篇关于如何在片段类而不是活动类中使用底页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆