gpt4 book ai didi

java - 如何在 ScreenSlidePagerAdapter 中启动新 Activity ?

转载 作者:行者123 更新时间:2023-11-29 18:30:03 25 4
gpt4 key购买 nike

我有一个 FragmentActivity,其中有一个 ScreenSlidePagerAdapter,它使用 getItem() 方法显示许多 fragment ,用于在 fragment 之间滑动。我希望一旦用户完成滑动 fragment (例如从 0 到 15),就会打开一个新 Activity ,显示用户在 fragment 导航中所做的结果(这是一门类(class))。

我需要的是在 fragment 之后打开新 Activity 的 Intent 。

这是我的代码:

    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() 创建一个 Intent ,但是这个方法没有加载到 Android Studio 中(也许这是旧的)。

最佳答案

您应该在适配器的最后显示 fragment 中编写启动 Activity 代码。 (如果您使用相同 fragment 的实例,则必须在创建 fragment 时将位置从适配器传递到 fragment 。

Intent intent = new Intent(getContext(), ThirdActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

关于java - 如何在 ScreenSlidePagerAdapter 中启动新 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56783820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com