2016-05-16 73 views
0

我在一个Android应用程序中遇到此问题。我有一个片段,我可以点击按钮,然后用一些EditText显示一个对话框片段。 因此,我在这个EditText之一上实现了一个onFocusChangeListener,并且我可以看到另一个带有RecycleListView的DialogFragment。如何将Listener从DialogFragment设置为另一个DialogFragment?

现在我想要这个:我想点击这个RecycleListView的一个项目,并在产生这个事件的EditText中显示这个。

因此XML文件,其中是的EditText是这样的:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/ScrollView01" 
    android:layout_width="800dp" 
    android:layout_height="fill_parent" > 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="830dp" 
    android:layout_height="fill_parent" 
    android:focusable="true" 
    android:focusableInTouchMode="true"> 



     <EditText android:id="@+id/textAgent" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:paddingTop="30dp" 
      android:hint="Insert agent" 
      /> 

     <AutoCompleteTextView android:id="@+id/reaction_autocomplete" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:paddingTop="30dp" 
       android:layout_toRightOf="@id/labelReaction" 
       android:hint="select an option"/> 

      <Spinner 


</RelativeLayout> 

</ScrollView> 

这是另一个DialogFragment

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="830dp" 
    android:layout_height="wrap_content" 
    android:focusable="true" 
    android:focusableInTouchMode="true"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recycler_view" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:scrollbars="vertical" 
     android:layout_marginTop="@dimen/margin_list_row"/> 

</RelativeLayout> 

这是主要片段的布局:

public class AlertsDialogFragment extends DialogFragment { 

    Context mContext; 
    List<AlertValueSet> listValueSet_Description; 
    List<AlertValueSet> listValueSet_Status; 
    List<AlertValueSet> listValueSet_Agent; 
    List<AlertValueSet> listValueSet_Reaction; 
    ArrayAdapter<AlertValueSet> adapterAgent; 
    View v; 

    public AlertsDialogFragment() { 

     mContext = getActivity(); 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 

     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     v = getActivity().getLayoutInflater().inflate(R.layout.alert_insert_dialog, null); 
     builder.setView(v); 

     builder.setTitle("Insert Alerts"); 
     builder.setCancelable(false); 

     //spinner status 
     EditText textAgent = (EditText) v.findViewById(R.id.textAgent); 
     textAgent.setOnFocusChangeListener(new AgentClickListener()); 
     return builder.create(); 
    } 

    public static AlertsDialogFragment newInstance() { 
     AlertsDialogFragment f= new AlertsDialogFragment(); 
     return f; 
    } 


    public class AgentClickListener implements View.OnFocusChangeListener { 
     public void onFocusChange(View v, boolean hasFocus) { 
      if(hasFocus) { 
       AlertsAgentDialogFragment dialog = AlertsAgentDialogFragment.newInstance(listValueSet_Agent); 
       dialog.show(getActivity().getFragmentManager(),""); 
      } 
     } 
    } 


} 

这是另一个DialogFragment的类,它显示RecycleListView

public class AlertsAgentDialogFragment extends DialogFragment { 

    Context mContext; 
    View v; 
    List<AlertValueSet> list; 
    private RecyclerView recyclerView; 
    private AlertInsertAgentAdapter pAdapter; 
    //private OnRecurrenceTypeListener mListener; 

    /* public interface OnRecurrenceTypeListener{ 
     void onRecurrenceTypeSelected(String rrule); 
    }*/ 

    public AlertsAgentDialogFragment() { 

     mContext = getActivity(); 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 

     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     v = getActivity().getLayoutInflater().inflate(R.layout.alert_agent_dialog, null); 
     builder.setView(v); 

     builder.setTitle("Select agent"); 
     builder.setCancelable(false); 

     pAdapter = new AlertInsertAgentAdapter(list, new AlertInsertAgentAdapter.OnItemClickListener() { 
      @Override 
      public void onItemClick(View view, int position) { 
       //recupero l'elemento che l'utente ha selezionato 
       AlertValueSet alert = list.get(position); 
       EditText textAgent = (EditText)v.findViewById(R.id.textAgent); 
       textAgent.setText(alert.getDisplayName()); 
      } 
     }); 
     RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(v.getContext()); 
     recyclerView = (RecyclerView) v.findViewById(R.id.recycler_view); 
     recyclerView.setLayoutManager(mLayoutManager); 
     recyclerView.setItemAnimator(new DefaultItemAnimator()); 
     recyclerView.setAdapter(pAdapter); 



     return builder.create(); 
    } 


    public static AlertsAgentDialogFragment newInstance(List<AlertValueSet> list) { 
     AlertsAgentDialogFragment f= new AlertsAgentDialogFragment(); 
     f.list=list; 
     return f; 
    } 
} 

回答

1

这可以通过类似于片段之间的通信完成的方式来处理。

在AlertsAgentDialogFragment类中创建接口,在基础活动中实现此接口。

在onItemClick()中,使用接口的对象调用接口的方法,如下所示。下面注意onAttach()方法。

public class AlertsAgentDialogFragment extends DialogFragment { 

    Context mContext; 
    View v; 
    List<AlertValueSet> list; 
    private RecyclerView recyclerView; 
    private AlertInsertAgentAdapter pAdapter; 
    private OnRecurrenceTypeListener mListener; 

    public interface OnRecurrenceTypeListener{ 
     void onRecurrenceTypeSelected(String rrule); 
    } 

    public void onAttach(Context context){ 
     if(context instanceOf MainActivity){ 
      mListener = (OnRecurrenceTypeListener) context; 
     } 
    } 

    public AlertsAgentDialogFragment() { 

     mContext = getActivity(); 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 

     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     v = getActivity().getLayoutInflater().inflate(R.layout.alert_agent_dialog, null); 
     builder.setView(v); 

     builder.setTitle("Select agent"); 
     builder.setCancelable(false); 

     pAdapter = new AlertInsertAgentAdapter(list, new AlertInsertAgentAdapter.OnItemClickListener() { 
      @Override 
      public void onItemClick(View view, int position) { 
       //recupero l'elemento che l'utente ha selezionato 
       AlertValueSet alert = list.get(position); 
       //EditText textAgent = (EditText)v.findViewById(R.id.textAgent); 
       //textAgent.setText(alert.getDisplayName()); 
       mListener.onRecurrenceTypeSelected(alert.getDisplayName()); 
      } 
     }); 
     RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(v.getContext()); 
     recyclerView = (RecyclerView) v.findViewById(R.id.recycler_view); 
     recyclerView.setLayoutManager(mLayoutManager); 
     recyclerView.setItemAnimator(new DefaultItemAnimator()); 
     recyclerView.setAdapter(pAdapter); 



     return builder.create(); 
    } 


    public static AlertsAgentDialogFragment newInstance(List<AlertValueSet> list) { 
     AlertsAgentDialogFragment f= new AlertsAgentDialogFragment(); 
     f.list=list; 
     return f; 
    } 
} 

我假设MainActivity为基本活动的名称,然后在MainActivity实现上述听众,onRecurrenceTypeListenercreate新AlertDialogsFragment内通过这个字符串中的新实例后。

class MainActivity extends Activity implements OnRecurrenceTypeListener{ 

    public void onRecurrenceTypeSelected(String str){ 

     AlertsAgentDialogFragment aDialog = 
      AlertsAgentDialogFragment.newIstance(str); 
     aDialog.show(getActivity().getFragmentManager(), ""); 
    } 
} 

现在从onCreateDialog()更新您的editText。