选项卡式活动中片段之间的相互作用

时间:2018-11-02 14:42:15

标签: java android android-layout android-fragments android-viewpager

我正在尝试Tabbed活动中两个片段之间的简单交互。

我有两种布局,分别为EditTextButtonEditText。我试图实现在按下FragmentOne中的EdidText时将文本从FragmentOne中的TextView移到Fragment 2中的Button(或ViewPager)。但它不起作用。

在调试期间没有任何问题。应用程序不会停止工作。

SectionsPagerAdapterpublic class FragmentOne extends Fragment { TextView textView; EditText editText; Button button; private OnFragmentOneInteractionListener mListener; public FragmentOne() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_one, container, false); editText = (EditText) view.findViewById(R.id.fragOne_txb); textView = (TextView) view.findViewById(R.id.fragTwo_header); button = (Button) view.findViewById(R.id.fragOne_btn_to2); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String text = editText.getText().toString(); mListener.onFragmentOneInteraction(text); } }); return view; } @Override public void onAttach(Context context) { super.onAttach(context); if (context instanceof OnFragmentOneInteractionListener) { mListener = (OnFragmentOneInteractionListener) context; } else { throw new RuntimeException(context.toString() + " must implement OnFragmentOneInteractionListener"); } } @Override public void onDetach() { super.onDetach(); mListener = null; } public interface OnFragmentOneInteractionListener { void onFragmentOneInteraction(String string); } 中有问题吗?

我有这两个片段。 片段一:

public class FragmentTwo extends Fragment{

private OnFragmentTwoInteractionListener mListener;

public EditText editText;

public FragmentTwo()
{
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
                         Bundle savedInstanceState)
{
    View view = inflater.inflate(R.layout.fragment_two, container, false);
    editText = (EditText) view.findViewById(R.id.fragTwo_txb);
    return view;
}

public void onUpdateEditText(String string)
{
    this.editText.setText(string);
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentTwoInteractionListener) {
        mListener = (OnFragmentTwoInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentTwoInteractionListener");
    }
}

@Override
public void onDetach()
{
    super.onDetach();
    mListener = null;
}

public interface OnFragmentTwoInteractionListener
{
    void onFragmentTwoInteraction(Uri uri);
}

}

然后分割两个:

public class MainActivity extends AppCompatActivity 
                      implements FragmentOne.OnFragmentOneInteractionListener,
                                 FragmentTwo.OnFragmentTwoInteractionListener
{
private SectionsPagerAdapter mSectionsPagerAdapter;

private ViewPager mViewPager;

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

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

    mViewPager.addOnPageChangeListener(
                   new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(
                   new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
}

@Override
public void onFragmentOneInteraction(String string)
{
    FragmentTwo fr2 = (FragmentTwo) getSupportFragmentManager().findFragmentById(R.id.fr2);
    fr2.onUpdateEditText(string);
}

@Override
public void onFragmentTwoInteraction(Uri uri) 
{

}

public class SectionsPagerAdapter extends FragmentPagerAdapter
{

    public SectionsPagerAdapter(FragmentManager fm)
    {
        super(fm);
    }

    @Override
    public Fragment getItem(int position)
    {
        switch (position)
        {
            case 0:
                FragmentOne fragmentOne = new FragmentOne();
                return fragmentOne;

            case 1:
                FragmentTwo fragmentTwo = new FragmentTwo();
                return fragmentTwo;

            default:
                return null;
        }
    }

    @Override
    public int getCount()
    {
        // Show 2 total pages.
        return 2;
    }
}
}

}

这是我的主要活动:

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

public class Canvas4 extends JComponent implements MouseListener, MouseMotionListener {

    //constructor
    public Canvas4() {
        super();
        addMouseListener(this);
        addMouseMotionListener(this);
    }

    //some variables I may or may not use
        int pressedX;
        int pressedY;
        int currentX;
        int currentY;
        int startDragX;
        int startDragY;
        int endDragX;
        int endDragY;
        int mouseReleasedX;
        int mouseReleasedY;



    //mouse events
    public void mouseClicked(MouseEvent event)  {  }

    public void mouseEntered(MouseEvent event)  {  }

    public void mouseExited(MouseEvent event)   {  }

    public void mousePressed(MouseEvent event)  {  
        pressedX = event.getX();
        pressedY = event.getY(); 
        drawCircle();

        startDragX = pressedX;
        startDragY = pressedY;
    }

    public void mouseReleased(MouseEvent event) {  
        mouseReleasedX = event.getX();
        mouseReleasedY = event.getY();
        //repaint() here maybe???
    }



    //mouse motion events
    public void mouseDragged(MouseEvent event) {
        System.out.println("You dragged the mouse.");
        endDragX = event.getX();
        endDragY = event.getY();
        drawLine();

    }

    public void mouseMoved(MouseEvent event) {  }



    //draw circle when mouse pressed
    //this method works fine
    public void drawCircle()    {
        Graphics g1 = this.getGraphics();
        g1.setColor(Color.CYAN);
        g1.fillOval(pressedX, pressedY, 10, 10);    
    }

    //draw line when mouse dragged
    //this is where I need help
    public void drawLine()  {
        Graphics g2 = this.getGraphics();
        g2.setColor(Color.RED);
        g2.drawLine(pressedX, pressedY, mouseReleasedX, mouseReleasedY);
    }
}

2 个答案:

答案 0 :(得分:1)

我刚刚尝试了您的代码,这是我所做的更新。如果您正在寻找它,请标记为正确

private SectionsPagerAdapter mSectionsPagerAdapter;

private ViewPager mViewPager;

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

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

    mViewPager.addOnPageChangeListener(
            new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(
            new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
}

@Override
public void onFragmentOneInteraction(String string)
{
    fragmentTwo.onUpdateEditText(string);

    /*FragmentTwo fr2 = (FragmentTwo) getSupportFragmentManager().findFragmentById(R.id.fr2);
    fr2.onUpdateEditText(string);*/
}

@Override
public void onFragmentTwoInteraction(Uri uri)
{
}

FragmentTwo fragmentTwo = new FragmentTwo();

public class SectionsPagerAdapter extends FragmentPagerAdapter
{

    public SectionsPagerAdapter(FragmentManager fm)
    {
        super(fm);
    }

    @Override
    public Fragment getItem(int position)
    {
        switch (position)
        {
            case 0:
                FragmentOne fragmentOne = new FragmentOne();
                return fragmentOne;

            case 1:

                return fragmentTwo;

            default:
                return null;
        }
    }

    @Override
    public int getCount()
    {
        // Show 2 total pages.
        return 2;
    }
}

答案 1 :(得分:0)

我建议改用getActivity()

if (getActivity() instanceof OnFragmentOneInteractionListener) {
    mListener = (OnFragmentOneInteractionListener) getActivity();
} else {
    throw new RuntimeException(context.toString()
            + " must implement OnFragmentOneInteractionListener");
}