我想在我的Recyclerview Adapter类的Onlongclick中实现android底部工作表,但是我试图按下我的recyclelerview项目的长按,它已经崩溃了。
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) at in.edsys.teacherapp.adapters.AddAtendanceAdapter $ AttendanceViewHolder.openBottomSheet(AddAtendanceAdapter.java:119) at in.edsys.teacherapp.adapters.AddAtendanceAdapter $ AttendanceViewHolder.access $ 000(AddAtendanceAdapter.java:80) 在in.edsys.teacherapp.adapters.AddAtendanceAdapter $ AttendanceViewHolder $ 1.onLongClick(AddAtendanceAdapter.java:95)
答案 0 :(得分:6)
您可以使用底部工作表对话框或底部工作表对话框片段,以便在适配器类中显示底部工作表
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());
更多信息请查看以下链接
答案 1 :(得分:1)
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文件进行小的更改