onClickListener(按钮)使用片段切换ViewPager的看法 [英] onClickListener (buttons) to switch view of ViewPager using Fragments
问题描述
我开发一个应用程序,它包括一个具有三个片段一FragmentActivity的。我想从第一个片段去第三和最后一个下一步按钮。对于这一点,我使用的是viewPager和pagerAdpater(我禁用分页/刷卡上viewpager)。我的下一步按钮,在他们自己的碎片得到落实。我对每一个片段将一个XML布局,一个用于包含viewpager的fragmentActivity。
I'm developing an application, which consists of a FragmentActivity with three Fragments. I want to go from the first fragment to the third and last one with "next" buttons. For that, I'm using a viewPager and pagerAdpater (I disabled the paging/swiping on the viewpager). My "next" buttons have been implemented in their own fragments. I have one xml layout for each fragment and one for the fragmentActivity containing the viewpager.
我试图用一个OnClickListener在那里我可以用setCurrentItem(index)方法,并从第一个到最后一个视图。
I am trying to use an OnClickListener where I could use the setCurrentItem(index) method and go from the first to the last view.
问题是,我不知道我在哪里可以把OnClickListener,因为我没有在我的片段viewpager参考。
The problem is that i don't know where I could put the OnClickListener, since I don't have the viewpager reference in my fragment.
我怎样才能得到我的片段viewpager参考?我必须把我的OnClickListener方法别的地方?
How can I get the viewpager reference in my fragments? Do I have to put my OnClickListener method somewhere else?
下面是我的3个片段之一code:
Here is the code of one of my 3 Fragments:
public class InitializationStep1 extends Fragment{
private PagerAdapter mPagerAdapter;
CustomViewPager pager;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = (LinearLayout)inflater.inflate(R.layout.initialization_step1_layout, container, false);
Button button = (Button) v.findViewById(R.id.goToStep2);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
CustomViewPager pager = (CustomViewPager) v.findViewById(R.id.viewpager);
if(pager == null)
System.out.println("It's null..."); // always getting a null value
else
pager.setCurrentItem(0);
}
});
return v;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
setUserVisibleHint(true);
}
}
这是我FragmentActivity:
and this is my FragmentActivity:
public class Initialization extends FragmentActivity{
private PagerAdapter mPagerAdapter;
CustomViewPager pager;
Button b1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.viewpager_initialization_layout);
//initialize the pager
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, InitializationStep1.class.getName()));
fragments.add(Fragment.instantiate(this, InitializationStep2.class.getName()));
fragments.add(Fragment.instantiate(this, InitializationStep3.class.getName()));
this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager(), fragments);
pager = (CustomViewPager)super.findViewById(R.id.viewpager);
pager.setPagingEnabled(false);
pager.setAdapter(this.mPagerAdapter);
pager.setCurrentItem(0);
}
}
要总结这一切,我有:结果
- 实施3片段(1,2和3)结果,
我想获得:结果
- 包含一个按钮,每一个片段进入下一个片段。(1 - > 2 - > 3)结果
- 含有完成按钮,最后一个片段(因为没有一个碎片导航到)
To sum all this up, I have:
- implemented 3 Fragments (1, 2 and 3)
and I would like to get:
- Each Fragment containing a button to go to the next Fragment (1 -> 2 -> 3).
- The last fragment containing a "finish" button (because there is no next Fragment to navigate to)
我认为这是一个很简单的动作,但我在Android开发相对较新,无法弄清楚这一点。我也许尝试使用了错误的组件来做到这一点。
I assume that this is a very simple action, but I'm relatively new in Android development and couldn't figure this out. I'm maybe trying to use the wrong component to do that.
感谢
推荐答案
我刚刚有同样的问题,试试这个!在初始化FragmentActivity:
I just had the same problem, try this! In your Initialization FragmentActivity:
public CustomViewPager getpager(){
return pager;
}
在InitializationStep1
In "InitializationStep1":
CustomViewPager pager;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = (LinearLayout)inflater.inflate(R.layout.initialization_step1_layout, container, false);
pager = ((Initialization)getActivity()).getpager();
Button next = (Button) v.findViewById(R.id.button1);
next.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
pager.setCurrentItem(2);
}
});
在这种方式,你可以得到您在FragmentActivity声明viewpager对象,所以你可以使用它在每个片段片段之间切换,只要你想!
In this way, you can get the viewpager object that you declare in your FragmentActivity, so you can use it in each fragment to switch between fragments as you want!
这篇关于onClickListener(按钮)使用片段切换ViewPager的看法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!