我的主要活动几乎设置了标签并在ViewPager中显示内容。通过单击选项卡,我可以导航到另一个选项卡。
代码:
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);
}
}
但是,当我尝试通过从其中一个页面单击TextView导航到另一个选项卡时,它会给我一个错误。我到目前为止所尝试的是,
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;
我实际上知道这不起作用,因为此页面中的viewPager与MainActivity.java中的ViewPager不同。如何通过单击numRepos TextView?
导航到另一个选项卡答案 0 :(得分:1)
在您的片段:
中尝试此操作ViewPager mviewPager = (ViewPager) getActivity().findViewById(R.id.viewpager);
此ID将是您在活动布局中提供的viewpager ID。
然后就这样使用它。 mviewPager.setCurrentItem(2);
答案 1 :(得分:1)
在 MainActivity
部分中创建 STATIC 方法。
public static ViewPager mViewPager; //GLOBAL
public static SectionsPageAdapter pageAdapter; //GLOBAL
public static void _openITEM() // Create this static method
{
viewPager.setCurrentItem(2);
}
,然后强>
numRepos.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
_openITEM();
}
});
答案 2 :(得分:0)
尝试将其放入<ng-container *ngIf="pokemon.PreviousEvolutions">
<ion-card>
...
</ion-card>
</ng-container>
方法:
<ng-container *ngIf="pokemon.NextEvolutions">
<ion-card>
...
</ion-card>
</ng-container>
答案 3 :(得分:0)
尝试添加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);
}
});
答案 4 :(得分:0)
在MainActivity
-
public void navigateToPage(int i) {
viewPager.setCurrentItem(i);
}
然后从任何片段中调用此方法 -
((MainActivity)getActivity()).navigateToPage(2);