2016-05-16 107 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。