如何在ScreenSlidePagerAdapter中启动新活动?

时间:2019-06-27 04:17:57

标签: java android android-activity

我有一个FragmentActivity,其中有一个ScreenSlidePagerAdapter,它使用getItem()方法显示许多片段,以便在片段之间滑动。我希望一旦用户完成片段的滑动(例如,从0到15),就将打开一个新的活动,显示用户浏览片段的结果(这是一门课程)。

我需要的是在片段后打开新活动的意图。

这是我的代码:

    package activities;

import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import com.flixarts.ar.*.R;
import com.flixarts.ar.*.fragment1;
import com.flixarts.ar.*.fragment10;
import com.flixarts.ar.*.fragment11;
import com.flixarts.ar.*.fragment12;
import com.flixarts.ar.*.fragment13;
import com.flixarts.ar.*.fragment14;
import com.flixarts.ar.*.fragment15;
import com.flixarts.ar.*.fragment2;
import com.flixarts.ar.*.fragment3;
import com.flixarts.ar.*.fragment4;
import com.flixarts.ar.*.fragment5;
import com.flixarts.ar.*.fragment6;
import com.flixarts.ar.*.fragment7;
import com.flixarts.ar.*.fragment8;
import com.flixarts.ar.*.fragment9;
import com.flixarts.ar.*.menuscreen;

public class saludos extends FragmentActivity implements      fragment1.OnFragmentInteractionListener, fragment2.OnFragmentInteractionListener, fragment3.OnFragmentInteractionListener, fragment4.OnFragmentInteractionListener {

private TextView espacioFrases;
private TextView espacioFrasesTraducidas;
private Button next;
private Button buttonHome;
private ViewPager mPager;
private PagerAdapter pagerAdapter;
private static final int NUM_PAGES = 15;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_saludos);
 mPager = (ViewPager) findViewById(R.id.pager);
    pagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
    mPager.setAdapter(pagerAdapter);



}
@Override
public void onBackPressed(){
    if (mPager.getCurrentItem() == 0) {
        super.onBackPressed();
    }
    else {
        mPager.setCurrentItem(mPager.getCurrentItem() -1);
    }
}

@Override
public void onFragmentInteraction(Uri uri) {

}

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
    public ScreenSlidePagerAdapter(FragmentManager fm){
        super(fm);
    }
    @Override
    public Fragment getItem(int position){
        switch (position){
            case 0: return new fragment1();
            case 1: return new fragment2();
            case 2: return new fragment3();
            case 3: return new fragment4();
            case 4: return new fragment5();
            case 5: return new fragment6();
            case 6: return new fragment7();
            case 7: return new fragment8();
            case 8: return new fragment9();
            case 9: return new fragment10();
            case 10: return new fragment11();
            case 11: return new fragment12();
            case 12: return new fragment13();
            case 13: return new fragment14();
            case 14: return new fragment15();
            //Here I want to start new activity
            default: return new fragment1();
        }
    }

    @Override
    public int getCount(){
        return NUM_PAGES;
    }
}

我正在尝试使用getActivity()创建一个意图,但是此方法未在Android Studio中加载(可能是旧的)。

1 个答案:

答案 0 :(得分:2)

您应该在最后显示适配器片段的内部编写startactivity代码。 (如果您使用的是相同片段的实例,则在创建它时必须将位置从适配器传递到片段。

JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].value='<<Add your input String here>>';", search);