我在一個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;
}
}