单击按钮时重定向片段

时间:2015-06-30 06:21:34

标签: android android-fragments

我正在使用此代码替换片段,但是有一个问题,它没有替换它只是覆盖旧片段的旧片段,所以请告诉我这里有什么问题。

TypeA -r- TypeB
TypeB -r- TypeC

2 个答案:

答案 0 :(得分:0)

您确定传递了父视图吗?这会将您的片段保留在fragTransaction.replace(R.id.fragment_signup,frag);行吗? 对于fragmentTransaction.replace(...,...),您需要为片段和片段本身指定容器。检查R.id.fragment_signup指示的位置。您可以在此SO问题中找到更多信息:Unable to replace fragment on button click

答案 1 :(得分:0)

import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import javax.swing.*;
import javax.swing.event.*;

public class InternalFrameEventTest {
  public JComponent makeUI() {
    final JDesktopPane desktop = new JDesktopPane();
    JPanel p = new JPanel(new BorderLayout());
    p.add(desktop);
    p.add(new JButton(new AbstractAction("New") {
      @Override public void actionPerformed(ActionEvent e) {
        JInternalFrame f = new JInternalFrame("title", true, true, true, true);
        InternalFrameHandler handler = new InternalFrameHandler();
        f.addInternalFrameListener(handler);
        f.addPropertyChangeListener(handler);
        f.setSize(240, 120);
        f.setVisible(true);
        desktop.add(f);
      }
    }), BorderLayout.NORTH);
    return p;
  }
  public static void main(String... args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(new InternalFrameEventTest().makeUI());
    f.setSize(320, 240);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}

class InternalFrameHandler implements PropertyChangeListener, InternalFrameListener {
  //PropertyChangeListener
  @Override public void propertyChange(PropertyChangeEvent e) {
    if (JInternalFrame.IS_MAXIMUM_PROPERTY.equals(e.getPropertyName())) {
      System.out.println("isMaximum: " + e.getNewValue());
    }
  }
  //InternalFrameListener
  @Override public void internalFrameClosing(InternalFrameEvent e) {
    System.out.println("internalFrameClosing");
  }
  @Override public void internalFrameClosed(InternalFrameEvent e) {
    System.out.println("internalFrameClosed");
  }
  @Override public void internalFrameOpened(InternalFrameEvent e) {
    System.out.println("internalFrameOpened");
  }
  @Override public void internalFrameIconified(InternalFrameEvent e) {
    System.out.println("internalFrameIconified");
  }
  @Override public void internalFrameDeiconified(InternalFrameEvent e) {
    System.out.println("internalFrameDeiconified");
    if (e.getInternalFrame().isMaximum()) {
      System.out.println("isMaximum: " + e.getInternalFrame().isMaximum());
    }
  }
  @Override public void internalFrameActivated(InternalFrameEvent e) {
    System.out.println("internalFrameActivated");
  }
  @Override public void internalFrameDeactivated(InternalFrameEvent e) {
    System.out.println("internalFrameDeactivated");
  }
}

如果你使用相同的代码,那么在调用第一个按钮setonclicklistner

之前添加它