有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java FragmentPagerAdapter总是从第一页开始

我有一个带有五个片段的片段寻呼机适配器,在第三个片段中,我想调用另一个活动,它将所选图片发送到容器。它工作正常,但每次从其他活动中选择图片时,寻呼机适配器都从片段活动中的第一个片段开始,而不是从第三个片段活动开始。我必须从其他活动强制寻呼机适配器,还是应该在片段活动中更改某些内容

代码:

片段活动:

package com.example.安卓.womb_the_game;

import 安卓.content.pm.ActivityInfo;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.view.Window;
import 安卓.view.WindowManager;

import 安卓x.fragment.app.FragmentActivity;
import 安卓x.fragment.app.FragmentPagerAdapter;
import 安卓x.viewpager.widget.ViewPager;

import com.google.安卓.material.floatingactionbutton.FloatingActionButton;
import com.google.安卓.material.snackbar.Snackbar;
import com.google.安卓.material.tabs.TabLayout;


public class Circulation extends FragmentActivity {


    ViewPager vp;
    public static FragmentPagerAdapter adapterViewPager;

    @Override
    protected void onCreate(Bundle onSavedInstanceState) {
        super.onCreate(onSavedInstanceState);

        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_circulation);

        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);


        vp = findViewById(R.id.view_pager);
        adapterViewPager=new Adapter_Circulation(getSupportFragmentManager(), this);
        vp.setAdapter(adapterViewPager);


     TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.setupWithViewPager(vp);


        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });


    }

}

FragmentPagerAdapter:

package com.example.安卓.womb_the_game;

import 安卓x.fragment.app.Fragment;
import 安卓x.fragment.app.FragmentManager;
import 安卓x.fragment.app.FragmentPagerAdapter;

import 安卓.content.Context;

public class Adapter_Circulation extends FragmentPagerAdapter {
    private static int NUM_ITEMS = 5;
    Context context;


    public Adapter_Circulation(FragmentManager fm, Context c) {
        super(fm);
        this.context = c;

    }


    @Override
    public Fragment getItem(int position) {
        switch (position)
        {
            case 0:
                return Frg0.newInstance (); 
            case 1:
                return Frg1.newInstance(); 
            case 2:
                return Frg2.newInstance(); 
            case 3:
                return Frg3.newInstance(); 
            case 4:
                return Frg4.newInstance(); 
        }
        return null; //does not happen
    }


    @Override
    public int getCount() {
        return NUM_ITEMS; //three fragments
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "RULES";
            case 1:
                return "EVENTS";
            case 2:
                return "PLAN";
            case 3:
                return "SHOOT";
            case 4:
                return "JUMP";
        }
        return null;
    }
}

片段3

package com.example.安卓.womb_the_game;

import 安卓.os.Bundle;
import 安卓x.annotation.Nullable;
import 安卓x.fragment.app.Fragment;
import 安卓x.fragment.app.FragmentManager;
import 安卓x.fragment.app.FragmentTransaction;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.widget.ImageButton;

public class Frg3 extends Fragment{

    private ImageButton shoot;
    private ImageButton snipe;
    private ImageButton aim;

    public static Frg3 newInstance() {

        Bundle args = new Bundle();

        Frg3 fragment = new Frg3();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable final Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        View rootView = inflater.inflate(R.layout.fragment_frg3, container, false);
        shoot = (ImageButton) rootView.findViewById(R.id.shoot);
        snipe = (ImageButton) rootView.findViewById(R.id.snipe);
        aim = (ImageButton) rootView.findViewById(R.id.aim); 

        final View.OnClickListener mListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (view.getId()) {
                    case R.id.shoot:
                        FragmentManager FM0 = getFragmentManager();
                        FragmentTransaction FT0 = FM0.beginTransaction();
                        frg_shoot F10 = new frg_shoot();
                        FT0.add(R.id.fragment_container1, F10);
                        FT0.replace(R.id.fragment_container1, F10);
                        FT0.commit();
                        break;
                    case R.id.snipe:
                        FragmentManager FM = getFragmentManager();
                        FragmentTransaction FT = FM.beginTransaction();
                        frg_snipe F1 = new frg_snipe();
                        FT.add(R.id.fragment_container1, F1);
                        FT.replace(R.id.fragment_container1, F1);
                        FT.commit();
                        break;
                    case R.id.aim:

                        FragmentManager FM1 = getFragmentManager();
                        FragmentTransaction FT1 = FM1.beginTransaction();
                        frg_aim F11 = new frg_aim();
                        FT1.add(R.id.fragment_container1, F11);
                        FT1.replace(R.id.fragment_container1, F11);
                        FT1.commit();
                        break;
                }
            }
        };

        rootView.findViewById(R.id.shoot).setOnClickListener(mListener);
        rootView.findViewById(R.id.snipe).setOnClickListener(mListener);
        rootView.findViewById(R.id.aim).setOnClickListener(mListener);

        return rootView;
    }

}

其他活动:

package com.example.安卓.womb_the_game;


import 安卓x.fragment.app.FragmentActivity;
import 安卓.content.Intent;
import 安卓.content.pm.ActivityInfo;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.view.Window;
import 安卓.view.WindowManager;
import 安卓.widget.ImageButton;
import 安卓.widget.ImageView;


public class choose_dice_column extends FragmentActivity {


   private ImageButton b1;
   private ImageView im1;

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_choose_dice_column);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

      b1 = (ImageButton) findViewById(R.id.b1);
      im1 = findViewById(R.id.im1);


        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                Intent i = new Intent(choose_dice_column.this, Circulation.class);
                i.putExtra("resid1",R.drawable.n1);
                startActivity(i);
            }

        });
    }    
}

共 (1) 个答案

  1. # 1 楼答案

    问题是,每当你回到你的活动中,你都在创建一个新的适配器。因此,将以下内容添加到Circulation-活动中,以保持适配器的状态:

    if(adapterViewPager == null)
    {
        adapterViewPager = new Adapter_Circulation(getSupportFragmentManager(), this);
    }