如何在回收器适配器中的onclicklistener中调用对话框片段

时间:2016-03-27 18:48:46

标签: android android-fragments android-recyclerview recycler-adapter

我有一个回收器适配器,每个显示片段。当我点击项目时,我想要那个打开的对话框片段。 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);
    }

}

2 个答案:

答案 0 :(得分:0)

如果您使用的是AppCombatActivity

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

答案 1 :(得分:0)

对于 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;