我有ALr.java类和FragHandler。 FragHandler具有Tablayout和viewpager。 ALR和FragHandler代码在下面是书面的。我想通过调用Activatealert方法来通过单击ALR即rl.setonclicklisterner来打开FragHandler中的第三个选项卡的``设置''选项卡。

MyFragHandler类

public class FragHandler extends AppCompatActivity
    TabLayout tabLayout;
    Toolbar customtoolbar;
    SharedPreferences sharedPreferences;

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

        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);



        tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.addTab(tabLayout.newTab().setText("1"));
        tabLayout.addTab(tabLayout.newTab().setText("2"));
        tabLayout.addTab(tabLayout.newTab().setText("Settings"));
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);




        final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        final AlsViewChanger adapter = new AlsViewChanger(getSupportFragmentManager(), tabLayout.getTabCount());
        viewPager.setAdapter(adapter);

        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });


    }





}


以及尝试打开选项卡3的位置的ALR.java。

public class ALR extends Fragment  {


    TextView rl,




    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) throws NullPointerException {
        View view = inflater.inflate(R.layout.ar, container, false);



        rl = (TextView) view.findViewById(R.id.reto);


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


                    activatealert();

                });
    public void activatealert() {
        MaterialDialog activateald = new MaterialDialog.Builder(getActivity())
                .content("In order to use these services, please click settings and set appropriate options to activate Where R U feature.")
                .title("Activate Where R U")
                .positiveColor(Color.BLUE)
                .negativeColor(Color.RED)
                .positiveText("Settings")
                .negativeText("Cancel")
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        Intent intent = new Intent(getActivity(),FragHandler.class);
                        intent.putExtra("Settings",2);
                        startActivity(intent);

                    }
                })
                .show();
    }
}


它会打开选​​项卡式活动,但“第一个选项卡”并非按需显示的第三项。请指导。

最佳答案

根据您的代码进行编辑:

删除OnTabSelectedListener,则不需要它。

只需添加

tabLayout.setupWithViewPager(viewPager);


这是完整的代码:

public class FragHandler extends AppCompatActivity
    TabLayout tabLayout;
    Toolbar customtoolbar;
    SharedPreferences sharedPreferences;

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

        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

        tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.addTab(tabLayout.newTab().setText("1"));
        tabLayout.addTab(tabLayout.newTab().setText("2"));
        tabLayout.addTab(tabLayout.newTab().setText("Settings"));
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        final AlsViewChanger adapter = new AlsViewChanger(getSupportFragmentManager(), tabLayout.getTabCount());
        viewPager.setAdapter(adapter);

        tabLayout.setupWithViewPager(viewPager);
        int tabToSelect = getIntent().getIntExtra("Settings”, 0);
        viewPager.setCurrentItem(tabToSelect);
    }

}


请检查

int tabToSelect = getIntent().getIntExtra("Settings”, 0);


我只是假设您是否愿意。根据需要进行更改。

没有太多细节很难回答,但是这是我基于假设的一些建议:

1)您要启动具有多个选项卡的活动FragHandler。 TabLayout与ViewPager关联。因此,在这种情况下,将适配器设置为ViewPager后,请调用viewPager.setCurrentItem(2)。如果只有选项卡,则可以尝试tabLayout.getTabAt(2).select()。这应该触发您的OnTabSelectedListener,您可能已经在其中具有用于替换片段的代码。

2)带有对话框的片段位于您要的第三个选项卡所在的同一TabLayout中。在这种情况下,将以下代码放入设置按钮的onClick中:

// If ViewPager, then make it public in YourActivity
((YourActivity)getActivity()).viewPager.setCurrentItem(2);

// If TabLayout without ViewPager, then also make it public
((YourActivity)getActivity()).tabLayout.getTabAt(2).select();


并发布尽可能多的详细信息,以便您获得正确的答案

08-25 22:40