我正在进行对话框调用,允许用户在微调器中选择一个节点,当他单击添加时,我从微调器中获取数据,然后回到调用该对话框的片段,但是他正在发送数据发送给我的班级片段父级。
创建片段的类:
public class Tab extends AppCompatActivity implements DialogAddAresta.ExampleDialogListner {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab);
TabLayout tabLayout = findViewById(R.id.tablayout_id);
ViewPager viewPager = findViewById(R.id.viewpager_id);
ViewPageAdapter adapter = new ViewPageAdapter(getSupportFragmentManager());
adapter.AddFragment(new FragmentGrafo(), "Grafo");
adapter.AddFragment(new FragmentAlgoritmo(), "Algoritmo");
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
}
//I put the tost to see the return of the apply.
@Override
public void applyNodes(String selectedNodeTo, String selectedNodeFrom) {
Toast.makeText(getApplicationContext(), "Voce esta na Tab "+selectedNodeTo+" "+selectedNodeFrom, Toast.LENGTH_SHORT).show();
}
}
我的DialogAddAresta:
public class DialogAddAresta extends AppCompatDialogFragment {
private Spinner spTo, spFrom;
private List<String> listNodes;
private ExampleDialogListner listner;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(Objects.requireNonNull(getActivity()));
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.layout_dialog_add_aresta, null);
listNodes = getArguments().getStringArrayList("list");
spTo = view.findViewById(R.id.sp_to);
spFrom = view.findViewById(R.id.sp_from);
ArrayAdapter<String> nodes = new ArrayAdapter<>(getActivity(),android.R.layout.simple_spinner_item, listNodes);
nodes.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spTo.setAdapter(nodes);
spFrom.setAdapter(nodes);
builder.setView(view)
.setTitle("Add Aresta")
.setCancelable(false)
.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String selectedNodeTo = spTo.getSelectedItem().toString();
String selectedNodeFrom = spFrom.getSelectedItem().toString();
listner.applyNodes(selectedNodeTo, selectedNodeFrom);
//Toast.makeText(getActivity(), "nos "+selectedNodeTo+" e "+selectedNodeFrom, Toast.LENGTH_SHORT).show();
}
});
return builder.create();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
listner = (ExampleDialogListner) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + "deve implementar ExampleDialogListner");
}
}
public interface ExampleDialogListner{
void applyNodes(String selectedNodeTo, String selectedNodeFrom);
}
}
及其片段FragmentGrafo(我想将数据发送到此类):
public class FragmentGrafo extends Fragment implements DialogAddAresta.ExampleDialogListner {
private static final int CONTENT_VIEW_ID = 10101010;
private DefaultFragment fragment;
private Graph graph;
private String[] criaNos = new String[]{"A","B","C","D","E","F","G","H","I","J","K",
"L","M","N","O","P","Q","R","S","T","U","V","X","W","Y","Z"};
private int pos = 0;
private float x = 0.0f;
private float y = 0.0f;
private String styleSheet = "graph { fill-color: #EEE; }" +
"node { size: 100; text-size: 30; fill-color: white; stroke-mode: plain;}";
public List<String> nos = new ArrayList<>();
private ArrayAdapter<String> adapter;
private List<String> criaArestas = new ArrayList<>();
private int arestaPos = 0;
private String nodeTo, nodeFrom;
TextView txtTips;
public FragmentGrafo(){
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.grafo_fragment, container, false);
txtTips = view.findViewById(R.id.txtView);
BottomNavigationView bottomNavigationView = view.findViewById(R.id.bottomNavView);
bottomNavigationView.setOnNavigationItemSelectedListener(selectedListener);
return view;
}
private BottomNavigationView.OnNavigationItemSelectedListener selectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_add_node:
if (pos < criaNos.length) {
graph.addNode(criaNos[pos]);
graph.getNode(criaNos[pos]).setAttribute("ui.label", criaNos[pos]);
graph.getNode(criaNos[pos]).setAttribute("xy", x, y);
nos.add(criaNos[pos]);
pos++;
x += 1.0f;
y += 1.5f;
}else {
Toast.makeText(getActivity(), "Não pode adcionr mais nós", Toast.LENGTH_SHORT).show();
}
return true;
case R.id.action_add_aresta:
openDialogAddAresta();
return true;
case R.id.action_remover:
txtTips.setText("Azul");
Toast.makeText(getActivity(), "Ultima aresta "+criaArestas.toString(), Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
};
@Override
public void onCreate(@Nullable android.os.Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
graph = new MultiGraph("AthenaGraph");
graph.setAttribute( "ui.stylesheet", styleSheet);
graph.setAttribute("ui.antialias");
display(savedInstanceState, graph, false);
}
public void display(Bundle savedInstanceState, Graph graph, boolean autoLayout) {
if (savedInstanceState == null) {
FragmentManager fm = getActivity().getFragmentManager();
fragment = (DefaultFragment) fm.findFragmentByTag("fragment_tag");
if (null == fragment) {
fragment = new DefaultFragment();
fragment.init(graph, autoLayout);
}
FragmentTransaction ft = fm.beginTransaction() ;
ft.add(R.id.layoutFragment, fragment).commit();
}
}
public void openDialogAddAresta() {
DialogAddAresta dialogAddAresta = new DialogAddAresta();
Bundle bundle = new Bundle();
bundle.putStringArrayList("list", (ArrayList<String>)nos);
dialogAddAresta.setArguments(bundle);
dialogAddAresta.show(getActivity().getSupportFragmentManager(), "Add Aresta");
}
@Override
public void applyNodes(String selectedNodeTo, String selectedNodeFrom) {
Toast.makeText(getActivity(), "FragmentGrafo "+selectedNodeTo+" "+selectedNodeFrom, Toast.LENGTH_SHORT).show();
}
}