[英][ViewPager - How to Navigate from one page to another using a button?
I am learning how to use ViewPager in my android app. 我正在学习如何在Android应用程序中使用ViewPager。 Right now, I was able to create a basic app with three tabs with swipe support.
现在,我能够创建带有三个带有滑动支持选项卡的基本应用程序。 Now, what I'm thinking to do is, how can I go from one page to another using a button inside one of the fragments currently in the ViewPager, I have tried this one
现在,我想做的是,如何使用ViewPager当前片段之一中的按钮从一页转到另一页,我已经尝试过了
Android ViewPager Prev/Next Button but it does not work for me, for a better understanding of what I am trying to achieve: Android ViewPager上一个/下一个按钮,但不适用于我,以便更好地了解我要实现的目标:
I have three(3) tabs (3 Fragments), TAB A, TAB B, TAB C 我有三个(3)标签(3个片段),TAB A,TAB B,TAB C
in TAB A, I have a button. 在TAB A中,我有一个按钮。 I want the action of that button INSIDE TAB A(Fragment A) to go to TAB C (or TAB B).
我希望该按钮在TAB A(片段A)中的动作转到TAB C(或TAB B)。 I've been searching for the right approach but to no avail.
我一直在寻找正确的方法,但无济于事。 I hope someone can help me, Thanks!
希望有人能帮助我,谢谢!
okay here is how to add button in fragment A to switch fragment c : 1- add button in Fragment a in xml file which id foo like this : 好的,这是如何在片段A中添加按钮以切换片段c的方法:1-在xml文件中的片段foo中添加片段a的按钮,如下所示:
<Button
android:id="@+id/foo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="go to fragemnt c" />
go to fragment a class overide method called onCreateView and set onclickListner create by creating interface called it buttonClick and create var from it in side fragment a then override method onAttch initialize interface var inside onAttch method: 去片段化一个名为onCreateView的类覆盖方法,并通过创建名为buttonClick的接口来设置onclickListner创建,并在侧面片段中从中创建var,然后重写方法onAttch初始化onAttch方法内的接口var:
public class FragmentA extends Fragment {
buttonClick click;
Button foo;
interface buttonClick {
void buttonClicked(View v);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
click = (buttonClick) activity;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_a, container,false);
foo = (Button) view.findViewById(R.id.foo);
foo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
click.buttonClicked(v);
}
});
return view;
}
}
then back to your MainActivity implements buttonClick interface and override methods in the method called buttonClicked(View v); 然后回到您的MainActivity实现buttonClick接口,并在名为buttonClicked(View v)的方法中重写方法; setCurrentItem for view pager like this :
setCurrentItem用于视图分页器,如下所示:
calss MainActivity implements FragmentA.buttonClick {
// your code here ...
public void buttonClicked(View v){
//get your viewPager var
viewPager.setCurrentItem(3);
}
}
i hope this helps 我希望这有帮助
获取您的ViewPager,使用
ViewPager.setCurrentItem(int item)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.