android 如何从回收器适配器调用onClicklistener中的对话片段

c3frrgcw  于 2023-03-16  发布在  Android
关注(0)|答案(3)|浏览(133)

我有一个显示在片段中的回收器适配器。当我单击项目时,我希望打开对话框片段。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);
    }

}
dtcbnfnu

dtcbnfnu1#

如果您使用的是AppCombatActivity

android.support.v4.app.FragmentManager manager = ((AppCompatActivity) context).getSupportFragmentManager();
gfttwv5a

gfttwv5a2#

对于AppCompatActivity,请使用以下命令:

FragmentManager manager = ((AppCompatActivity) context).getSupportFragmentManager();

EventRegFragmentDialog eventRegFragment= new EventRegFragmentDialog();

  eventRegFragment.show(manager, "Edit Fragment");

请导入以下内容:

import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
ozxc1zmp

ozxc1zmp3#

在经历了Android开发的漫长旅程之后,我可以肯定地说,最好不要直接从适配器调用任何东西。您必须将接口(侦听器)传递给适配器,然后在fragment/activity中调用您需要的内容。首先,让我们在适配器中创建一个接口(您可以根据项目架构随时创建接口)。

interface OnItemClick{
              void onItemClick();
    }

public class TehcnikMyProfileAdapter extends RecyclerView.Adapter<TehcnikMyProfileAdapter.RequestFriendViewHolder> {

private List<Vehicles> listVehicles = Collections.emptyList();
private Context context;
private OnItemClick clickListener;

public TehcnikMyProfileAdapter(List<Vehicles> list, Context context, OnItemClick clickListener) {
    this.listVehicles = list;
    this.context = context;
    this.clickListener = clickListener;
}

@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) {
            //get this callback in your Activity/Fragment
            this.clickListener.onItemClick
//Move this logic into Activity/Fragment
>                 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);
    }

}

相关问题