如何从自定义alertdialog显示片段,警报对话框在适配器

时间:2017-08-06 08:56:09

标签: android android-fragments

我正在尝试从自定义alertdilog的按钮单击事件中显示片段,警报对话框在片段的适配器中,我想显示来自alertdialog按钮单击的片段,但我无法这样做。

alertdialog代码:

    holder.commentButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                        AlertDialog.Builder builder=new AlertDialog.Builder(context);
                        final AlertDialog alertDialog=builder.create();

                        View viewTmp_comment=LayoutInflater.from(context).inflate(R.layout.comment_show_layout,null,false);
                        RecyclerView recyclerView_commentShow=(RecyclerView)viewTmp_comment.findViewById(R.id.recyclerViewId_commentShow);
                        TextView textViewId_commentShow_layout=(TextView)viewTmp_comment.findViewById(R.id.textViewId_commentShow_layout);
                        ImageButton commentSendButtonId_Post=(ImageButton)viewTmp_comment.findViewById(R.id.commentSendButtonId_Post);
                        ImageButton imageUpload_CommentButton=(ImageButton)viewTmp_comment.findViewById(R.id.imageUploadButtonId_comment);
                        ImageButton smileyUpload_CommentButton=(ImageButton)viewTmp_comment.findViewById(R.id.smileyButtonId_comment);
                        editTextId_CommentPost=(EditText) viewTmp_comment.findViewById(R.id.editTextId_CommentPost);
    //                RelativeLayout smileyLoadLayoutId_CommentAdapter=(RelativeLayout)

                        final CommentShowAdapter commentShowAdapter=new CommentShowAdapter(context,newsFeedClassArrayList.get(position).getCommentShowClassArrayList(),"NewsFeed");
                        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(context);
                        recyclerView_commentShow.setLayoutManager(linearLayoutManager);
                        if(newsFeedClassArrayList.get(position).getCommentShowClassArrayList()!=null) {
                            recyclerView_commentShow.setAdapter(commentShowAdapter);

                            DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(context, linearLayoutManager.getOrientation());
                            recyclerView_commentShow.addItemDecoration(dividerItemDecoration);
                            commentShowAdapter.notifyDataSetChanged();
                            recyclerView_commentShow.onChildDetachedFromWindow(null);
                        }else {
                            builder.setMessage("No comments yet");
                        }

                    //comment show alert dismiss
                    textViewId_commentShow_layout.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            try {
                                if(alertDialog.isShowing()){
                                    alertDialog.dismiss();
                                }
                            }catch (Exception e){}
                        }
                    });


**//here i am trying to lauch fragment**
                    smileyUpload_CommentButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            ServiceClass.setSmileyKey("comment");

                            smileyFragment=new SmileyShowFragment();

                            FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
                            Fragment fragment = fragmentManager.findFragmentById(R.id.smileyLoadLayoutId_CommentAdapter);

                            if (!(fragment instanceof SmileyShowFragment)) {

                                fragmentTransaction.replace(R.id.smileyLoadLayoutId_CommentAdapter, smileyFragment, "Smiley").commit();
                            } else {

                                fragmentTransaction.remove(smileyFragment).commit();

                            }
                        }
                    });

                        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
                        Window window = alertDialog.getWindow(); lp.copyFrom(window.getAttributes());
                        lp.width = context.getResources().getDisplayMetrics().widthPixels;
                        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
                        lp.gravity=Gravity.BOTTOM; window.setAttributes(lp);

                        alertDialog.setView(viewTmp_comment);
                        alertDialog.show();
                }
            });

提醒xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ddd"

    >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:id="@+id/textViewId_commentShow_layout"
        android:text="Comments"
        android:textSize="16sp"
        android:paddingLeft="20dp"
        android:textStyle="bold"
        android:paddingTop="10dp"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/underline"/>

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/recyclerViewId_commentShow"
        >

    </android.support.v7.widget.RecyclerView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal"
        android:weightSum="4"
        android:background="#1470a6"
        >

        <ImageButton
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@drawable/camera"
            android:id="@+id/imageUploadButtonId_comment"
            android:layout_gravity="center"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="5dp"
            />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/smileyButtonId_comment"
            android:src="@drawable/smiley"
            android:layout_gravity="center"
            />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:id="@+id/editTextId_CommentPost"
            android:layout_weight="3"
            android:background="#FFFFFF"
            android:hint="Your comment"
            android:textSize="16sp"

            />

        <ImageButton
            android:layout_width="30dp"
            android:layout_height="match_parent"
            android:id="@+id/commentSendButtonId_Post"
            android:src="@drawable/send_comment"
            android:background="#1470a6"
            android:gravity="right"
            android:layout_gravity="right"
            android:layout_weight="1"
            />

    </LinearLayout>
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/smileyLoadLayoutId_CommentAdapter"
        >
    </FrameLayout>
</LinearLayout>

如何解决这个问题?是否有可能在alertdialog.i中显示片段已在谷歌搜索但没有类似的解决方案

1 个答案:

答案 0 :(得分:0)

您应该创建一个单独的自定义对话框。为此,您可以扩展DialogFragment并在onCreateDialog()上设置您的视图和正确的分配。为自定义对话框片段类创建一个布局,其中包含一个片段。您可以在自定义对话框上创建界面,以侦听按钮单击或其他操作。然后,单击您的按钮使用片段管理器在对话框片段中显示片段。如下所示:

  

//给它一个带有FrameLayout的片段来保存片段   需要持有多个或只需在布局中添加片段   如果你不需要持有人。

public class MyCustomizedDialog extends DialogFragment{

private TextView dialogTitle;
private TextView dialogText;
private FrameLayout myFragmentContainer;
private Button myButton;
private CustomDialogListener customDialogListener;

public interface CustomDialogListener{
  //set listeners here, i.e. for buttons clicked by user
  public void onBtnClick();
}

public setDialogFragmentListener(CustomDialogListener customDialogListener){
    this.customDialogListener = customDialogListener;
}
@override
public Dialog onCreateDialog(Bundle savedInstanceState) { 
  //initialize dialog
  setUpMyCustomDialog(dialog);
  return dialog;
}

public void setUpMyCustomDialog(Dialog dialog){
    dialogTitle = (TextView)dialog.findViewById(R.id.dialog_title_tv);
    dialogText = (TextView)dialog.findViewById(R.id.dialog_text_tv);
    myFragmentContainer=(FrameLayout)dialog.findViewById(R.id.child_fragment_container);
    myButton = (Button)dialog.findViewById(R.id.myButton);

    myButton.setOnClickListener(new OnClickListener(){
        public void onClick(View view) {
        //initialize your fragment here.
        }
    }

} 

}