如何从另一个活动按钮单击事件中打开viewpager中的特定片段页面

时间:2015-11-10 09:49:33

标签: android

任何人都可以让我知道如何打开一个特定的片段 来自其他活动的viewpager按钮单击事件。

我正在使用一个显示两个选项的警告对话框 在viewpager中选择相应的屏幕,如下所示,但我得到null 单击screentwo按钮上的指针,其中包含打开viewpagers第二页的代码。

public class MainActivity extends FragmentActivity {
    int pos;
    ViewPager pager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);     
         pager = (ViewPager) findViewById(R.id.viewPager);
        pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
      Button   ds = (Button) findViewById(R.id.show_back);
     ds.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent goIntent = new Intent(MainActivity.this,SecondActivity.class);
            startActivity(goIntent);

        }
     });
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {

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

        @Override
        public Fragment getItem(int pos) {
            switch(pos) {

            case 0: return FirstFragment.newInstance("FirstFragment, Instance 1");
            case 1: return SecondFragment.newInstance("SecondFragment, Instance 1");
            default: return FirstFragment.newInstance("FirstFragment, Instance 1");
            }
        }

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

goesBack = (Button) findViewById(R.id.show_front);
goesBack.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(SecondActivity.this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
        alertDialogBuilder.setMessage("Please choose your Favourite Screen");
        alertDialogBuilder.setNegativeButton("ScreenOne",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            Intent go = new Intent(SecondActivity.this,MainActivity.class);
            startActivity(go);
             }
        });
        alertDialogBuilder.setPositiveButton("ScreenTwo", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // Create new fragment and transaction
                SecondFragment newFragment = new SecondFragment();
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.container, newFragment, "NewFragment");
                transaction.addToBackStack(null);
                transaction.commit();
             }
        });
        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();         
    }
});

谢谢&的问候,

Aditya.J

3 个答案:

答案 0 :(得分:8)

在你的onClick中,只需传递你想要在意图的附加内容中显示的viewpager的位置,并在viewpager活动中调用pager.setCurrentItem(position)

所以对于你的onClick

代码就是这样的
goesBack = (Button) findViewById(R.id.show_front);
goesBack.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(SecondActivity.this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
        alertDialogBuilder.setMessage("Please choose your Favourite Screen");
        alertDialogBuilder.setNegativeButton("ScreenOne",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent go = new Intent(SecondActivity.this,MainActivity.class);

                // you pass the position you want the viewpager to show in the extra, 
                // please don't forget to define and initialize the position variable
                // properly
                go.putExtra("viewpager_position", 0)

                startActivity(go);
            }
        });
        alertDialogBuilder.setPositiveButton("ScreenTwo", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // Create new fragment and transaction

                /*SecondFragment newFragment = new SecondFragment();
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.container, newFragment, "NewFragment");
                transaction.addToBackStack(null);
                transaction.commit();*/
                // You don't do the above code, it will just create a new fragment
                // you do this instead.
                Intent go = new Intent(SecondActivity.this,MainActivity.class);

                // you pass the position you want the viewpager to show in the extra, 
                // please don't forget to define and initialize the position variable
                // properly
                go.putExtra("viewpager_position", 1)

                startActivity(go);
             }
        });
        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();         
    }
});

您的viewpager活动的代码将是这样的。

int pos;
ViewPager pager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);     
    int position = 0;
    Bundle extras = getIntent().getExtras();
    if(extras != null) {
        position = extras.getInt("viewpager_position");
    }
    pager = (ViewPager) findViewById(R.id.viewPager);
    MyPagerAdapter myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
    pager.setAdapter(myPagerAdapter);
    Button   ds = (Button) findViewById(R.id.show_back);
    ds.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent goIntent = new Intent(MainActivity.this,SecondActivity.class);
        startActivity(goIntent);

    }
    });

    pager.setCurrentItem(position) // this way if there is no extras 
    // you will get first position of viewpager
    // otherwise you will get the position of viewpager whichever was passed in the intent.
}

答案 1 :(得分:1)

viewPager.setCurrentItem(THE_POSITION)

答案 2 :(得分:0)

此代码将使用上一个和下一个按钮移动到上一个或下一个片段,并将在末尾打开下一个活动。

 private int[] layouts;

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

        ViewPager viewPager = findViewById(R.id.view_pager);
        viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));    

        layouts = new int[]{
                R.layout.fragment_1,
                R.layout.fragment_2,
                R.layout.fragment_3};

        Button btn_next = findViewById(R.id.btn_next);
        btn_next.setOnClickListener(v -> {
            int current = viewPager.getCurrentItem() + 1;
            if (current < layouts.length) {
                // move to next screen
                viewPager.setCurrentItem(current);
            } else {
                // open next activity
                Intent intent = new Intent(WizardSetupActivity.this, NextActivity.class);
                startActivity(intent);
                finish();
            }
        });

        Button btn_prev = findViewById(R.id.btn_prev);
        btn_prev.setOnClickListener(v -> {
            int current = viewPager.getCurrentItem() - 1;
            if (current >= 0) {
                // move to previous screen
                viewPager.setCurrentItem(current);
            } 
        });
    }