【问题标题】:How to use bottom sheet in fragment class instead of Activity Class?如何在片段类而不是活动类中使用底页?
【发布时间】:2019-09-20 14:35:25
【问题描述】:

我正在尝试在点击侦听器中使用底部工作表,但在这一行出现错误。

bottomSheetFragment.show(getSupportFragmentManager())

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

我想在片段类中使用底部工作表。

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>

【问题讨论】:

标签: android fragment bottom-sheet


【解决方案1】:

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

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

或者,如果您想将一些数据传递给BottomSheetDialogFragment,请使用以下代码,使用newInstance,您可以发送和检索数据。

在您的片段类中:

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

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

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();
        }

还将您的YourBottomSheetFragmentClass 扩展为BottomSheetDialogFragment

【讨论】:

  • 这里的 FollowupFragment_BottomSheet 是什么?
  • 您可以使用您的 BottomsheetFragment 类名来代替那里。
  • 如果我使用我的 BottomsheetFragment 类,那么新实例 BottomSheetFragment.newInstance() 会出现错误;
  • 答案已编辑,如有任何疑问,请检查并告诉我。
  • 但不推荐使用 getFragmentManager() 方法
【解决方案2】:

根据documentation 为了在父片段和子片段之间传递数据,请执行以下操作: 启动

启动底部工作表:

button.setOnClickListener{
    val sortRecipesBottomSheet = SortRecipesBottomSheet()
    sortRecipesBottomSheet.show(childFragmentManager,sortRecipesBottomSheet.tag)
}

在同一片段中编写此代码以接收来自底部工作表的结果

 childFragmentManager.setFragmentResultListener(
            "requestKey",
            viewLifecycleOwner
        ) { key, bundle ->
            val result = bundle.getString("bundleKey")
            println("heeeeeeere: "+ result)
            // Do something with the result
        }

在您的底页片段中为了将结果发回写下:

 view.findViewById<MaterialButton>(R.id.sortByName).setOnClickListener {
            parentFragmentManager.setFragmentResult(
                "requestKey",
                bundleOf("bundleKey" to SortType.ByName.key)
            )
            dismiss()
        }

show() 方法的父片段中,您正在发送childFragmentManager,并且您正在使用childFragmentManager 来收听结果,在底部表中您正在使用parentFragmentManager 来设置父片段读取的结果。

【讨论】:

    猜你喜欢
    • 2020-07-30
    • 2016-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多