如何从RecyclerView Adapter启动

时间:2019-09-08 18:04:39

标签: android-recyclerview dialogfragment

我要在RecyclerView项目上启动一个dialogFragment,单击RecyclerView Adapter

我尝试了

FragmentManager fr = ((Activity)context).getFragmentManager();
AnswersDialogFragment msgDialog = new AnswersDialogFragment();
msgDialog.show(fr, "Dialog");

但是我遇到了投射错误

java.lang.ClassCastException: android.view.ContextThemeWrapper cannot be cast to android.support.v4.app.FragmentActivity

我要在单击项目时启动对话框片段

@Override
    public AnswersViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.answercard, parent, false);
        AnswersViewHolder evh = new AnswersViewHolder(v, mListener);
        return evh;
    }
@Override
    public void onBindViewHolder(AnswersViewHolder holder, int position) {
        int viewType = getItemViewType(position);

        if(viewType==COMMENT_TYPE){
            final Comments item = (Comments) commentsList.get(position);
            holder.UserName.setText(Html.fromHtml(item.getUserName()));
            holder.CommentText.setText(Html.fromHtml(item.getCommentText()));

        }else{
            final Replys replyitem = (Replys) commentsList.get(position);
            holder.UserName.setText(Html.fromHtml(replyitem.getReplyAuthor()));
            holder.CommentText.setText(Html.fromHtml(replyitem.getReplyBody()));
        }

    }

imgLikeComment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    FragmentManager fr = ((Activity)context).getFragmentManager();
                    AnswersDialogFragment msgDialog = new AnswersDialogFragment();
                    msgDialog.show(fr, "Dialog");
});

AnswersDialogFragment.java

public class AnswersDialogFragment extends DialogFragment {

    ArrayList<Object> answersList;

    RecyclerView recyclerView;
    AnswersAdapter recyclerViewadapter;
    RecyclerView.LayoutManager recyclerViewlayoutManager;

    ImageView sendReply ;
    EditText commentInput;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogStyle);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.nestedcomments, container, false);
..... etc

项目单击正常,我无法启动片段

1 个答案:

答案 0 :(得分:0)

尝试改用SupportFragmentManager

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