猿问

从 RecyclerViewAdapter 启动 BottomSheet

我在布局中有一个卡片视图,它有两个文本视图和一个图像视图,我有一个使用卡片视图设置项目的回收器视图。我想要实现的是在点击 Card View 时启动 Bottom Sheet


这是主要活动中的代码


列表代码


List<Item> lstItem;


    lstItem = new ArrayList<>();


    lstItem.add(new Item("Samsung","Smartphones","QualitlstItem = new ArrayList<>();y Samsung Smartphones",R.drawable.samsung,"21000"));

    lstItem.add(new Item("Lenovo","Smartphones","Quality Lenovo smartphones",R.drawable.lenovo,"15000"));

将 Recycler View Adapter 附加到 Main Activity 的代码


        FragmentManager fragmentManager = getSupportFragmentManager();

        RecyclerViewAdapter2 mainAdapter2 = new RecyclerViewAdapter2(lstItem,fragmentManager);

        mainRecyclerView.setLayoutManager(new GridLayoutManager(this,3));

        mainRecyclerView.setAdapter(mainAdapter2);

Recycler View Adapter 2代码


        import android.content.Context;

        import android.view.LayoutInflater;

        import android.view.View;

        import android.view.ViewGroup;

        import android.widget.ImageView;

        import android.widget.TextView;



        import androidx.cardview.widget.CardView;

        import androidx.fragment.app.FragmentManager;

        import androidx.recyclerview.widget.RecyclerView;



        import java.util.List;


        public class RecyclerViewAdapter2 extends RecyclerView.Adapter<RecyclerViewAdapter2.MyViewHolder> {


        private Context context;

        private List<Item> data;

        private FragmentManager fragmentManager;


        public RecyclerViewAdapter2(List<Item> data, FragmentManager fragmentManager) {

        this.data = data;

        this.fragmentManager = fragmentManager;

    }



        @Override

        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

          View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_items, parent, false);

          return new MyViewHolder(view);

        }



}


慕标琳琳
浏览 154回答 3
3回答

小怪兽爱吃肉

在您的 onClick 侦听器中,您需要使用fragmentManager传递给适配器的 。相反,你使用getSupportFragmentManager(). 更改后它对我有用。holder.cardView.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; BottomSheetFragment bottomSheetFragment = new BottomSheetFragment();&nbsp; &nbsp; &nbsp; &nbsp; // <-- Here change getSupportFragmentManager() to fragmentManager&nbsp; &nbsp; &nbsp; &nbsp; bottomSheetFragment.show(fragmentManager, bottomSheetFragment.getTag());&nbsp; &nbsp; }});

蝴蝶不菲

从 RecyclerView.Adapter 调用 BottomSheetDialog 片段。试试这个对我来说效果很好。DialogFragment bottomSheetDialogFragment = new BottomsheetDialog();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bottomSheetDialogFragment.show(((FragmentActivity)mContext).getSupportFragmentManager(), bottomSheetDialogFragment.getTag());

呼如林

你不能这样做:AppCompatActivity appCompatActivity = new AppCompatActivity();FragmentManager fragmentManager = appCompatActivity.getSupportFragmentManager();BottomSheetFragment bottomSheetFragment = new BottomSheetFragment();bottomSheetFragment.show(fragmentManager, bottomSheetFragment.getTag());您应该创建一个包含您的 BottomSheetFragment 的新活动(一个新类),并使用意图启动该活动。
随时随地看视频慕课网APP

相关分类

Java
我要回答