具有不同布局的多个DialogFragment

时间:2019-01-23 15:05:48

标签: java android android-dialogfragment dialogfragment

我一直在寻找在一项活动中实现不同DialogFragment的方法。我发现了几种方法,但是它们依赖于片段之间的链式样式,因此不需要不同的布局。

我正在使用表单应用程序,并且在片段上输入信息时,我需要进入“主活动”,因为所需的信息并不总是准备就绪,可以优先考虑自由输入。

现在我在主要活动上实现了Dialog片段,这至少在我尝试的方式下,不允许我添加另一个片段。我读过有关创建父片段并关联子片段并根据需要调用它们的信息,但是正如我早先所说的那样,这种方式将一种布局与父片段相关联,我不确定如何放置不同的布局。

目前,我有一个MainActivity,它将我定向到一个DialogFragment(FpsDialog),另一个按钮将我定向到另一个活动,并且也希望转换为对话框。

这是我主要活动的代码

    package com.komorebiestudio.cam_report_funcionality;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements FpsDialog.FpsDialogListener  {

    private TextView textViewinfo1;
    private Button button1;
    private  Button lensButton;
    private TextView lensInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lensInfo = findViewById(R.id.lensinfo);
        lensInfo.setText(getIntent().getStringExtra("LensIntent"));
        textViewinfo1 = findViewById(R.id.info1);
        button1 = findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view) {
                openDialog();
            }
        });
        lensButton = findViewById(R.id.lensbutton);
        lensButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this,LensActivity.class));
            }
        });
    }


    public void openDialog(){
        FpsDialog fps_dialog = new FpsDialog();
        fps_dialog.show(getSupportFragmentManager(),"Fps Dialog");

    }


    @Override
    public void applyText(String fpsinfo) {
        textViewinfo1.setText(fpsinfo);

    }


}

对话框片段中的代码

    package com.komorebiestudio.cam_report_funcionality;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatDialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class FpsDialog extends AppCompatDialogFragment {
    private EditText editTextFpsEntry;
    private FpsDialogListener listener;
    private Button buttonfps2397;
    private Button buttonfps50;
    private Button buttonfps24;
    private Button buttonfps48;



    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.fps_dialog, null);

        builder.setView(view)
                .setTitle("Set Fps")
                .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                })
                .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        String fpsinfo = editTextFpsEntry.getText().toString();
                        listener.applyText(fpsinfo);

                    }
                });


        editTextFpsEntry = view.findViewById(R.id.fpsEntry);
        buttonfps2397 = view.findViewById(R.id.fps2397);
        buttonfps50 = view.findViewById(R.id.fps50);
        buttonfps24 = view.findViewById(R.id.fps24);
        buttonfps48 = view.findViewById(R.id.fps48);

        buttonfps2397.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String setFps2397 = "23,97";
                listener.applyText(setFps2397);
                dismiss();

            }
        });

        buttonfps50.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String setFps50 = "50";
                listener.applyText(setFps50);
                dismiss();
            }
        });

        buttonfps24.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String setFps24 = "24";
                listener.applyText(setFps24);
                dismiss();
            }
        });

        buttonfps48.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String setFps48 = "48";
                listener.applyText(setFps48);
                dismiss();
            }
        });


        return builder.create();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        try {
            listener = (FpsDialogListener) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString()+
                    "must implement Lens Dialog Listener");
        }

    }



    public interface FpsDialogListener{
        void applyText(String fpsinfo);
    }



}

以及我想添加为对话框的RecyclerView的代码。

package com.komorebiestudio.cam_report_funcionality;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;

public class LensActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private LensAdapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    public String lensString;
    public String brandLens;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lens_activity);

        final ArrayList <LensItem> lensList = new ArrayList<>();
        lensList.add(new LensItem(R.drawable.zeiss,"24mm ","Zeiss Compact Prime"));
        lensList.add(new LensItem(R.drawable.angenieux,"35mm ","Angenieux"));
        lensList.add(new LensItem(R.drawable.cooke,"50mm ","Cooke S5I"));
        lensList.add(new LensItem(R.drawable.zeiss,"85mm ","Zeiss Compact Prime"));
        lensList.add(new LensItem(R.drawable.angenieux,"135mm ","Angenieux"));
        lensList.add(new LensItem(R.drawable.cooke,"100mm ","Cooke S5I"));

        mRecyclerView = findViewById(R.id.lens_list);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this);
        mAdapter = new LensAdapter(lensList);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mAdapter);



        mAdapter.setOnItemClickListener(new LensAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
               //Crea String con la informacion de posicion y texto del lente
                String lensPosition = lensList.get(position).getLens();
                String lensbrand = lensList.get(position).getBrandLens();
                brandLens =lensbrand;
                lensString = lensPosition + lensbrand;
                //Crea un Intent para enviar la informacion de lensString a la MainActivity
                Intent lensIntent = new Intent(LensActivity.this,MainActivity.class);
                lensIntent.putExtra("LensIntent",lensString);
                //Al utilizar startActivity con lensIntent como parametro cierro el Intent y paso a la activity declarada.
                startActivity(lensIntent);
            }
        });

    }




}

0 个答案:

没有答案