意图从片段到第二个标签

时间:2016-09-09 12:29:56

标签: android android-fragments

我在主要活动中使用viewpager在android中设置了标签布局。我有三个Tab和三个片段。在第一个片段我有按钮我想要点击该按钮的第二个选项卡。怎么打算请帮助我。 这是我的代码

public class MainActivity extends AppCompatActivity {

    //Declaring All The Variables Needed

    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private ViewPagerAdapter viewPagerAdapter;

    public static final String PREFS_NAME = "MyPrefsFile";


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


        SharedPreferences settings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        String name = settings.getString("first", "str1");

        //str1 = intent.getStringExtra("first");
        Toast.makeText(MainActivity.this, "" + name, Toast.LENGTH_SHORT).show();


        toolbar = (Toolbar) findViewById(R.id.toolbar);
        tabLayout = (TabLayout) findViewById(R.id.tabs);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        FragmentManager manager = getSupportFragmentManager();
        ViewPagerAdapter adapter = new ViewPagerAdapter(manager);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setTabsFromPagerAdapter(adapter);

        tabLayout.setTabTextColors(ContextCompat.getColorStateList(this, R.drawable.tab_selector));
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        setSupportActionBar(toolbar);
        final FloatingActionMenu floatingActionsMenu=(FloatingActionMenu) findViewById(R.id.material_design_android_floating_action_menu);
        final FloatingActionButton floatingActionButton1=(FloatingActionButton)findViewById(R.id.material_design_floating_action_menu_item1);
        floatingActionButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "Table", Toast.LENGTH_SHORT).show();
                viewPager.setCurrentItem(0);
              floatingActionsMenu.close(true);
            }
        });

        FloatingActionButton floatingActionButton2=(FloatingActionButton)findViewById(R.id.material_design_floating_action_menu_item2);
        floatingActionButton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "Order", Toast.LENGTH_SHORT).show();
                viewPager.setCurrentItem(1);
                floatingActionsMenu.close(true);
            }
        });
        FloatingActionButton floatingActionButton3=(FloatingActionButton)findViewById(R.id.material_design_floating_action_menu_item3);
        floatingActionButton3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "Logout", Toast.LENGTH_SHORT).show();
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            Intent tologin=new Intent(MainActivity.this,LoginPage.class);
            startActivity(tologin);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}

2 个答案:

答案 0 :(得分:1)

所有片段都在同一个ViewPager中,你只想要一个到另一个片段,你可以这样做......

视图寻呼机位于Activity中,但Button位于Fragment内部,因此您需要在活动中创建此方法

public void navigateFragment(int position){
viewPager.setCurrentItem(position, true); 

}

并在片段的OnClickListener方法中调用:

((MainActivity)getActivity()).navigateFragment(POSITION_YOU_WANNA_SELECT);

希望这会对你有所帮助。

答案 1 :(得分:0)

推荐方法:您可以在第二个片段中实现一个侦听器,并在第一个片段中创建引用。 从第二个片段中定义的侦听器的第一个片段调用方法中单击按钮后。

第二种方法:您可以从第一个片段维护静态数据,然后单击即可加载第二个片段并访问该静态数据。