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

问题描述:

我想显示片段从自定义alertdilog的按钮单击事件,警报对话框是在片段的适配器,我想从alertdialog按钮点击显示片段,但我无法这样做。如何显示自定义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中显示片段?我在谷歌上搜索过但没有类似的解决方案

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

//给它用的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. 
     } 
    } 

} 

}

+0

我alertdialog定制 – AAA

+0

您可以编辑我的代码吗?如果可能的话 – AAA