我有一个显示在片段中的回收器适配器。当我单击项目时,我希望打开对话框片段。getSupportFragmentmanager是不调用的,它以红色突出显示。如何打开对话框片段。请帮助我,也许,我有错误的逻辑。
public class TehcnikMyProfileAdapter extends RecyclerView.Adapter<TehcnikMyProfileAdapter.RequestFriendViewHolder> {
List<Vehicles> listVehicles = Collections.emptyList();
Context context;
public TehcnikMyProfileAdapter(List<Vehicles> list, Context context) {
this.listVehicles = list;
this.context = context;
}
@Override
public RequestFriendViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//Inflate the layout, initialize the View Holder
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout_technik, parent, false);
return new RequestFriendViewHolder(v);
}
@Override
public void onBindViewHolder(RequestFriendViewHolder holder, int position) {
holder.titleFirm.setText(listVehicles.get(position).getFirmName());
holder.titleModel.setText(listVehicles.get(position).getModelName());
holder.titleYear.setText(String.valueOf(listVehicles.get(position).getYear()));
// holder.countPhoto.setText(String.valueOf(listVehicles.get(position).getPhotos().size()));
View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// Intent myIntent = new Intent(context, FriendProfileActivity.class);
// myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// myIntent.putExtra("id", mUserList.get(position).getId());
// context.startActivity(myIntent);
}
};
holder.imgvMooreOptions.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager manager = context.getSupportFragmentManager();
GarageOptionDialogFragment myDialogFragment = new GarageOptionDialogFragment();
myDialogFragment.show(manager, "dialog");
}
});
holder.cv.setOnClickListener(clickListener);
}
@Override
public int getItemCount() {
//returns the number of elements the RecyclerView will display
return listVehicles.size();
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}
3条答案
按热度按时间dtcbnfnu1#
如果您使用的是AppCombatActivity
gfttwv5a2#
对于AppCompatActivity,请使用以下命令:
请导入以下内容:
ozxc1zmp3#
在经历了Android开发的漫长旅程之后,我可以肯定地说,最好不要直接从适配器调用任何东西。您必须将接口(侦听器)传递给适配器,然后在fragment/activity中调用您需要的内容。首先,让我们在适配器中创建一个接口(您可以根据项目架构随时创建接口)。