简体   繁体   English

如何在不单击Android Studio上的选项卡的情况下移至另一个选项卡?

[英]How to move to another tab without clicking the tab on Android Studio?

My main activity pretty much sets up the tabs and displays the content inside the ViewPager. 我的主要活动几乎是设置选项卡并在ViewPager中显示内容。 I am able to navigate to another tab by clicking the tabs. 我可以通过单击选项卡导航到另一个选项卡。

Code: 码:

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    private SectionsPageAdapter pageAdapter;
    private ViewPager mViewPager;


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

        pageAdapter = new SectionsPageAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter

        mViewPager = (ViewPager) findViewById(R.id.container);
        setupViewPager(mViewPager);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager((mViewPager));


    }

    // Add fragments to SectionsPageAdapter and give titles
    private void setupViewPager(ViewPager viewPager) {
        SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
        adapter.addFragment(new ProfileTab(), "Profile");
        adapter.addFragment(new GithubTab(), "Github Repos");
        adapter.addFragment(new FollowerTab(), "Followers");
        adapter.addFragment(new FollowingTab(), "Followings");
        viewPager.setAdapter(adapter);
    }


}

However, when I try to navigate to another tab by clicking on a TextView from one of the pages, it gives me an error. 但是,当我尝试通过单击其中一个页面上的TextView导航到另一个选项卡时,它给我一个错误。 What I've tried so far is, 到目前为止,我尝试过的是

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.profile_tab, container, false);

        ... Code Hidden ...
        // numRepos is the TextView that I have

        numRepos.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(getActivity().getApplicationContext(), "num repos", Toast.LENGTH_SHORT).show();
                viewPager.setCurrentItem(2);
            }
        });


        return view;

And I actually knew this wouldn't work because the viewPager in this page is not same as the ViewPager in the MainActivity.java. 而且我实际上知道这行不通,因为此页面中的viewPager与MainActivity.java中的ViewPager不同。 How can I navigate to another tab by clicking the numRepos TextView? 如何通过单击numRepos TextView导航到另一个选项卡?

Try this in your fragment : 在您的片段中尝试

ViewPager mviewPager = (ViewPager) getActivity().findViewById(R.id.viewpager);

this id will be your viewpager id which is given in activity layout. 该ID将是您在活动布局中提供的viewpager ID。

then just use this. 然后就用这个 mviewPager.setCurrentItem(2);

Create STATIC method in MainActivity section. MainActivity部分中创建STATIC方法。

public static ViewPager mViewPager; //GLOBAL
public static SectionsPageAdapter pageAdapter; //GLOBAL

public static void _openITEM() // Create this static method
{
    viewPager.setCurrentItem(2);
}

Then 然后

numRepos.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               _openITEM();
            }
        });

Try to put this in the onClick method: 尝试将其放在onClick方法中:

TabActivity tabs = (TabActivity) getParent();
tabs.getTabHost().setCurrentTab(2);

Try add the viewPager findViewById Like this maybe help 尝试添加viewPager findViewById像这样的帮助

 viewPager = (ViewPager) getActivity.findViewById(R.id.container);
 numRepos.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(getActivity().getApplicationContext(), "num repos", Toast.LENGTH_SHORT).show();
                viewPager.setCurrentItem(2);
            }
        });

Create this method in your MainActivity - 在您的MainActivity创建此方法-

public void navigateToPage(int i) {
        viewPager.setCurrentItem(i);
    }

Then call this method like this from any of your fragments - 然后从您的任何片段中调用此方法-

    ((MainActivity)getActivity()).navigateToPage(2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过单击Android中的按钮从一个选项卡切换到另一个选项卡? - How to switch from one tab to another tab by clicking on button in Android? android-如何从另一个选项卡重新启动选项卡 - android - How to restart the tab from another tab Android Studio-如何设置其他活动中的标签内容? - Android Studio - How to set a tab content from another activity? 如何从Android中的活动移至特定标签 - How to move to a specific tab from an activity in android 如何在不单击任何内容的情况下移动到另一个活动 - how to move to another activity without clicking anything Kotlin (android studio) - 通过单击更改选项卡不会更改所选片段上的按钮,滑动到选项卡有效 - Kotlin (android studio) - changing tab by clicking doesn't change button on selected fragment, where swiping to the tab works Android 工作室 Gradle 标签 - Android studio Gradle Tab 如何通过单击选项卡1中的按钮切换到选项卡2? - How to switch to tab 2 by clicking a button in tab 1? 如何从android中的另一个选项卡调用一个选项卡? - How can i invoke a tab from another tab in android? 如何在 Android 的另一个选项卡中创建一个选项卡? - How Can I create a tab inside another tab in Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM