如何将Listener从DialogFragment设置为另一个DialogFragment?

时间:2016-05-16 15:03:58

标签: android android-fragments

我在一个Android应用程序中遇到此问题。我有一个Fragment,我可以单击Button,我可以显示带有一些EditText的Dialog Fragment。 所以,我在其中一个EditText上实现了一个onFocusChangeListener,我可以看到另一个带有RecycleListView的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>

这是Another 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(),"");
            }
        }
    }


}

这是显示RecycleListView

的另一个DialogFragment的类
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 个答案:

答案 0 :(得分: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中,在onRecurrenceTypeListener创建新的AlertDialogsFragment,在新实例中传递此字符串。

class MainActivity extends Activity implements OnRecurrenceTypeListener{

    public void onRecurrenceTypeSelected(String str){

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

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