在我的 recyclerview 适配器类中实现 Android 底页

Posted

技术标签:

【中文标题】在我的 recyclerview 适配器类中实现 Android 底页【英文标题】:Implement Android bottom sheet in my recyclerview adapter class 【发布时间】:2016-06-18 03:02:08 【问题描述】:

我想在我的 Recyclerview 适配器类的 Onlongclick 中实现 android 底部工作表,但我试图长按我的 recyclerview 项目它崩溃了。

     public class AddAtendanceAdapter extends      
        RecyclerView.Adapter<AddAtendanceAdapter.AttendanceViewHolder> 
        public List<Details> dAttendance = Collections.emptyList();

        private LayoutInflater inflater;
        private static Context context;

        private View v;




        public AddAtendanceAdapter(Context context, List<Details> dAttendance) 
            this.dAttendance = dAttendance;
            this.context = context;
            inflater = LayoutInflater.from(context);
        

        @Override
        public AttendanceViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.student_item_row,
    parent, false);
            AttendanceViewHolder pvh = new AttendanceViewHolder(v);
            return pvh;
        

        @Override
        public void onBindViewHolder(AttendanceViewHolder holder, int postions) 
            Details details = dAttendance.get(postions);
            holder.StudentsName.setText(details.getStudentName());
            Glide.with(context)
                    .load(details.getStudentImage())
                    .into(holder.stdImg);

        

        @Override
        public int getItemCount() 
            if (dAttendance != null) 
                return dAttendance.size();
            
            return 0;
        


        @Override
        public void onAttachedToRecyclerView(RecyclerView recyclerView) 
            super.onAttachedToRecyclerView(recyclerView);
        

        public static class AttendanceViewHolder extends RecyclerView.ViewHolder 



            TextView StudentsName;
            CircleImageView stdImg;
            private  Activity activity;
            AttendanceViewHolder(View itemView) 
                super(itemView);
                StudentsName = (TextView) itemView.findViewById(R.id.txtStdName);
                stdImg = (CircleImageView) itemView.findViewById(R.id.stdImg);
                itemView.setOnLongClickListener(new View.OnLongClickListener() 
                    @Override
                    public boolean onLongClick(View v) 
                        Log.d("juu","hhh");
                        openBottomSheet(v);

                        return false;
                    
                );


            
            private  void openBottomSheet(View v) 
           //View view = activity.getLayoutInflater ().inflate (R.layout.bottom_sheet, null);
               // View view = inflater.inflate( R.layout.bottom_sheet, null );

                LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
            View view = inflater.inflate (R.layout.bottom_sheet, null);
                TextView txtBackup = (TextView)view.findViewById(R.id.txt_backup);
                TextView txtDetail = (TextView)view.findViewById(R.id.txt_detail);
                TextView txtOpen = (TextView)view.findViewById(R.id.txt_open);
                final TextView txtUninstall = (TextView)view.findViewById( R.id.txt_uninstall);

                final Dialog mBottomSheetDialog = new Dialog (context, R.style.MaterialDialogSheet);
                mBottomSheetDialog.setContentView (view);
                mBottomSheetDialog.setCancelable (true);
                mBottomSheetDialog.getWindow ().setLayout (LinearLayout.LayoutParams.MATCH_PARENT,
    LinearLayout.LayoutParams.WRAP_CONTENT);
                mBottomSheetDialog.getWindow ().setGravity (Gravity.BOTTOM);
                mBottomSheetDialog.show ();


                txtBackup.setOnClickListener(new View.OnClickListener() 

                    @Override
                    public void onClick(View v) 
                        Toast.makeText(context,"Clicked Backup",Toast.LENGTH_SHORT).show();
                        mBottomSheetDialog.dismiss();
                    
                );

                txtDetail.setOnClickListener(new View.OnClickListener() 

                    @Override
                    public void onClick(View v) 
                        Toast.makeText(context,"Clicked Detail",Toast.LENGTH_SHORT).show();
                        mBottomSheetDialog.dismiss();
                    
                );

                txtOpen.setOnClickListener(new View.OnClickListener() 

                    @Override
                    public void onClick(View v) 
                        Toast.makeText(context,"Clicked Open",Toast.LENGTH_SHORT).show();
                        mBottomSheetDialog.dismiss();
                    
                );

                txtUninstall.setOnClickListener(new View.OnClickListener() 

                    @Override
                    public void onClick(View v) 
                        Toast.makeText(context,"Clicked Uninstall",Toast.LENGTH_SHORT).show();
                        mBottomSheetDialog.dismiss();
                    
                );
            

        


我的原木猫是

android.view.WindowManager$BadTokenException: 无法添加窗口 -- 令牌 null 不适用于应用程序 在 android.view.ViewRootImpl.setView(ViewRootImpl.java:583) 在 android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:310) 在 android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85) 在 android.app.Dialog.show(Dialog.java:319) 在 in.edsys.teacherapp.adapters.AddAtendanceAdapter$AttendanceViewHolder.openBottomSheet(AddAtendanceAdapter.java:119) 在 in.edsys.teacherapp.adapters.AddAtendanceAdapter$AttendanceViewHolder.access$000(AddAtendanceAdapter.java:80) 在 in.edsys.teacherapp.adapters.AddAtendanceAdapter$AttendanceViewHolder$1.onLongClick(AddAtendanceAdapter.java:95)

【问题讨论】:

提供您的适配器长按代码,您似乎使用了错误的窗口令牌,这意味着您尝试显示不再存在的上下文。 你能给我那个示例代码吗? 看看下面的android开发者博客链接,android-developers.blogspot.in/2016/02/… 您必须尝试以下链接代码。它有效:github.com/Flipboard/bottomsheet 实际上它在我的 mainactivity 类中工作,但它在我的问题的适配器类中不起作用,我想在用户长按 recyclerview 项目时打开底页。 【参考方案1】:

您可以使用底页对话框底页对话框片段在适配器类中显示底页

BottomSheetDialog 示例

 View view = ((FragmentActivity)context).getLayoutInflater().inflate(R.layout.fragment_bottom_sheet, null);
                BottomSheetDialog dialog = new BottomSheetDialog(mContext);
                dialog.setContentView(view);
                dialog.show();

你也可以使用 BottomsheetDialogFragment

BottomsheetDialogFragment 示例

对话框类

public class BottomsheetDialog extends BottomSheetDialogFragment 

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup       container, @Nullable Bundle savedInstanceState) 
    View v=inflater.inflate(R.layout.fragment_bottom_sheet,container,false);
    return v;


像这样在适配器类中调用 BottomsheetDialog

BottomSheetDialogFragment bottomSheetDialogFragment = new BottomsheetDialog();               
bottomSheetDialogFragment.show(((FragmentActivity)mContext).getSupportFragmentManager(), bottomSheetDialogFragment.getTag());

欲了解更多信息,请查看以下链接

Link1

Link2

【讨论】:

很好的答案 非常感谢,即使我也面临同样的问题,但您的最后一个回答对我有所帮助。干杯!【参考方案2】:
private  void openBottomSheet(View v) 
                //View view = activity.getLayoutInflater ().inflate (R.layout.bottom_sheet, null);
                // View view = inflater.inflate( R.layout.bottom_sheet, null );
                Context context=v.getContext();
                LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
                View view = inflater.inflate (R.layout.bottom_sheet, null);
                TextView txtBackup = (TextView)view.findViewById(R.id.txt_backup);
                TextView txtDetail = (TextView)view.findViewById(R.id.txt_detail);
                TextView txtOpen = (TextView)view.findViewById(R.id.txt_open);
                final TextView txtUninstall = (TextView)view.findViewById( R.id.txt_backup);

            final Dialog mBottomSheetDialog = new Dialog (context, R.style.MaterialDialogSheet);
            mBottomSheetDialog.setContentView (view);
            mBottomSheetDialog.setCancelable (true);
            mBottomSheetDialog.getWindow ().setLayout (LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            mBottomSheetDialog.getWindow ().setGravity (Gravity.BOTTOM);
            mBottomSheetDialog.show ();


            txtBackup.setOnClickListener(new View.OnClickListener() 

                    @Override
                    public void onClick(View v) 
                            Toast.makeText(v.getContext(),"Clicked Backup",Toast.LENGTH_SHORT).show();
                            mBottomSheetDialog.dismiss();
                    
            );

            txtDetail.setOnClickListener(new View.OnClickListener() 

                    @Override
                    public void onClick(View v) 
                            Toast.makeText(v.getContext(),"Clicked Detail",Toast.LENGTH_SHORT).show();
                            mBottomSheetDialog.dismiss();
                    
            );

            txtOpen.setOnClickListener(new View.OnClickListener() 

                    @Override
                    public void onClick(View v) 
                            Toast.makeText(v.getContext(),"Clicked Open",Toast.LENGTH_SHORT).show();
                            mBottomSheetDialog.dismiss();
                    
            );

            txtUninstall.setOnClickListener(new View.OnClickListener() 

                    @Override
                    public void onClick(View v) 
                            Toast.makeText(v.getContext(),"Clicked Uninstall",Toast.LENGTH_SHORT).show();
                            mBottomSheetDialog.dismiss();
                    
            );
    

使用它为我工作的代码对 xml 文件进行小的更改

【讨论】:

伙计,你太棒了:D。希望成为我的朋友,一起在很酷的项目中工作

以上是关于在我的 recyclerview 适配器类中实现 Android 底页的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Kotlin 在 RecyclerView Adapter 中实现 onClick 并进行数据绑定

在 RecyclerView 的适配器中实现的 OnClickListener 不起作用

从片段中获取意图值后,我如何在 recyclerview 项目中实现单击

如何在 RecyclerView 中实现 StartActivityForResult

使用 Recycler View 时如何在 Text To Speech 中实现错误处理

可过滤在 Recyclerview 中无法按预期工作