我无法在Activity上调用fragment方法,因为fragment对象是由newInstance方法构造的

时间:2018-12-22 03:06:11

标签: java android

我想使MediaPlayer在活动按钮按下时停止运行。 我在片段中制作了stopMediaPlayer()方法。并且我尝试在Activity中的onClick(View view)方法上调用stopMediaPlayer()。

但就我而言, 片段的对象是使用newInstance构造的。 并添加了FragmentAdater类,该类扩展了FragmentPagerAdapter类。像下面 这意味着我没有可以直接从Activity调用该方法的片段实例。 我必须使用Adapter实例调用该方法,因为它具有FragmentManager。

public class FragmentAdapter extends FragmentPagerAdapter {

ArrayList<Fragment> fragmentArrayList = new ArrayList<>();

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

public void addFragment(Fragment fragment) {
    fragmentArrayList.add(fragment);
}


@Override
public Fragment getItem(int position) {
    return fragmentArrayList.get(position);
}

@Override
public int getCount() {
    return fragmentArrayList.size();
}

------------------下面的活动--------------------------- ----------

WholeGuideFragment wholeGuideFragment = new WholeGuideFragment();
Part1QuestionFragment part1QuestionFragment = new Part1QuestionFragment();
Part2QuestionFragment part2QuestionFragment = new Part2QuestionFragment();
Part3QuestionFragment part3QuestionFragment = new Part3QuestionFragment();
Part4QuestionFragment part4QuestionFragment = new Part4QuestionFragment();
Part5QuestionImageFragment part5QuestionImageFragment = new Part5QuestionImageFragment();
Part5QuestionStringFragment part5QuestionStringFragment = new Part5QuestionStringFragment();
Part6QuestionFragment part6QuestionFragment = new Part6QuestionFragment();
StopButton stopButton = StopButton.getStopButton();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_part1);
    setFragmentAdapter();
    initView();
    setSession();
    checkPermission();
    //audio record 묻기

    Log.d("aaa", "Acitivity onCreate");
}

private void setFragmentAdapter() {
    fragmentAdapter = new FragmentAdapter(getSupportFragmentManager());
    fragmentAdapter.addFragment(wholeGuideFragment);
    fragmentAdapter.addFragment(PartGuideFragment.newInstance(1, this));
    fragmentAdapter.addFragment(part1QuestionFragment);
    fragmentAdapter.addFragment(PartGuideFragment.newInstance(2, this));
    fragmentAdapter.addFragment(part2QuestionFragment);
    fragmentAdapter.addFragment(PartGuideFragment.newInstance(3, this));
    fragmentAdapter.addFragment(part3QuestionFragment);
    fragmentAdapter.addFragment(PartGuideFragment.newInstance(4, this));
    fragmentAdapter.addFragment(part4QuestionFragment);
    fragmentAdapter.addFragment(PartGuideFragment.newInstance(5, this));
    fragmentAdapter.addFragment(part5QuestionImageFragment);
    fragmentAdapter.addFragment(part5QuestionStringFragment);
    fragmentAdapter.addFragment(PartGuideFragment.newInstance(6, this));
    fragmentAdapter.addFragment(part6QuestionFragment);
}
private void initView() {
    isStopped = false;
    next_btn = findViewById(R.id.next_btn);
    back_btn = findViewById(R.id.back_btn);
    play_pause_button = findViewById(R.id.play_pause_button);
    viewPager = findViewById(R.id.viewPager);
    viewPager.setAdapter(fragmentAdapter);
    viewPager.beginFakeDrag();
    viewPager.setOffscreenPageLimit(0);
    next_btn.setOnClickListener(this);
    back_btn.setOnClickListener(this);

    play_pause_button.setOnClickListener(this);
    play_pause_button.setSelected(true);

}


@Override
public void onClick(View view) {
    int button_id = view.getId();
    position = viewPager.getCurrentItem();
    switch (button_id) {
        case R.id.next_btn:
            setNext(position);
            break;
        case R.id.back_btn:
            setBefore(position);
            break;
        case R.id.play_pause_button:
            if (play_pause_button.isSelected()) {
                fragmentPlayStop(position, play_pause_button.isSelected());
                stopButton.setStopButtonClicked(false);
                play_pause_button.setSelected(false);
            } else {
                fragmentPlayStop(position, play_pause_button.isSelected());
                stoppedPageNum = position;
                savedStoppedQ_num(position);
                stopButton.setStopButtonClicked(true);
                play_pause_button.setSelected(true);
            }
            //play_pause_button is selector:true=play_button,false=pause
            //stopButton is a Singleton class can share the boolean value to other fragments whether the activity stop button is pressed or not
            //stoppedPageNum and savedStoppedQ_num method is to compare the page whether the user pressed the button at the same fragment or not.
            //timer 초기화.
            break;
    }
}
public void fragmentPlayStop(int stop_play_position, boolean b) {
    if (b == true) {
        stoppedPageNum = checkStopAndCurrentPage(stop_play_position);
        fragmentonResume(stoppedPageNum);
    } else {
        fragmentonPause(stop_play_position);
    }
}
private void fragmentonPause(int stop_play_position) {
    switch (stop_play_position) {
        case 0:
            wholeGuideFragment.onPause();
            break;
        case 1:
            //mp stop
            break;
        case 2:
            part1QuestionFragment.stopMediaPlayer();
            part1QuestionFragment.stopTimer();
            break;
        case 3:
            //Guide fragment에서 mediaplayer어떻게 스탑할건지 생각하기
            //mp stop
            break;
        case 4:   break;
        case 5:
            //Guide fragment에서 mediaplayer어떻게 스탑할건지 생각하기
            //mp stop
            break;
        case 6:
            //mp, timer stop
            break;
        case 7:
            //Guide fragment에서 mediaplayer어떻게 스탑할건지 생각하기
            //인터페이스로 mpstop 이나 onPause호출하기
            break;
        case 8:
            //mp, timer stop
            break;
        case 9:
            //Guide fragment에서 mediaplayer어떻게 스탑할건지 생각하기
            //mp stop
            break;
        case 10:
            //mp stop
            break;
        case 11:
            //mp, timer stop
            break;
        case 12:
            //Guide fragment에서 mediaplayer어떻게 스탑할건지 생각하기
            //mp stop
            break;
        case 13:
            //mp, timer stop
            break;
    }
}

问题在这里。 由于对象通过newInstance方法,因此无法调用如下所示的fragment方法。如果是1,3,5,7,9,12,我必须调用stopMediaPlayer();

(PartGuideFragment)fragmentAdapter.getItem(9).stopMediaPlayer();

我已经尝试过,但是我只能调用诸如onPause,onStop等覆盖方法...

+参考下面的FragmentClass->“ PartGuideFragment”

public static PartGuideFragment newInstance(int part, GuideCompleteListener guideCompleteListener) {
    Bundle args = new Bundle();
    args.putInt("part", part);
    PartGuideFragment fragment = new PartGuideFragment();
    fragment.guideCompleteListener = guideCompleteListener;
    fragment.setArguments(args);
    return fragment;
}

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

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_part1_guide, container, false);
    textView = view.findViewById(R.id.guide_txt);
    part = getArguments().getInt("part");
    setGuideString();
    return view;
}

private void setGuideString() {
    switch (part){
        case 1:
            str = guidData.getPart1Guide();
            break;
        case 2:
            str = guidData.getPart2Guide();
            break;
        case 3:
            str = guidData.getPart3Guide();
            break;
        case 4:
            str = guidData.getPart4Guide();
            break;
        case 5:
            str = guidData.getPart5Guide();
            break;
        case 6:
            str = guidData.getPart6Guide();
            break;
    }
    Markwon.setMarkdown(textView, str);
}

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) {
        if(mediaPlayer == null){
            setMediaPlayer();
            mediaPlayer.start();
        }else{
            mediaPlayer.start();
        }
    }else{
        if(mediaPlayer == null){
            setMediaPlayer();
        }else{
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }
}

private void setMediaPlayer() {
    switch (part) {
        case 1:
            mediaPlayer = MediaPlayer.create(context, R.raw.part1_direction);
            break;
        case 2:
            mediaPlayer = MediaPlayer.create(context, R.raw.part2_direction);
            break;
        case 3:
            mediaPlayer = MediaPlayer.create(context, R.raw.part3_direction);
            break;
        case 4:
            mediaPlayer = MediaPlayer.create(context, R.raw.part4_direction);
            break;
        case 5:
            mediaPlayer = MediaPlayer.create(context, R.raw.part5_direction);
            break;
        case 6:
            mediaPlayer = MediaPlayer.create(context, R.raw.part6_direction);
            break;
    }
}

public void stopMediaPlayer(){
    if(mediaPlayer!= null){
        mediaPlayer.stop();
    }
}

public void playMediPalyer(){
    if(mediaPlayer!= null){
        mediaPlayer.reset();
        mediaPlayer.start();
    }else{
        setMediaPlayer();
        mediaPlayer.start();
    }
}

1 个答案:

答案 0 :(得分:0)

您刚才做错了演员表。试试这个

((PartGuideFragment)fragmentAdapter.getItem(9)).stopMediaPlayer();