Reputation: 299
I am trying to use a ViewPager where users can interact within each pane. For this ViewPager, I am creating a tutorial where I show users to swipe horizontally to switch pane. When the user reaches the last pane, a button is displayed to the user where they will be navigated to the dashboard. I have read a few articles trying to understand how a button works within a ViewPager, but could not really make much sense.
What the code should do is when a button within a pane within a ViewPager is clicked, the dashboard activity is started.
Here is the full code of the pagerAdapter:
class MyPagerAdapter extends PagerAdapter {
public int getCount() {
return 5;
}
public Object instantiateItem(View collection, int position) {
View v = new View(collection.getContext());
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0:
resId = R.layout.tutorial_step1;
break;
case 1:
resId = R.layout.tutorial_step2;
break;
case 2:
resId = R.layout.tutorial_step3;
break;
case 3:
resId = R.layout.tutorial_step4;
break;
case 4:
resId = R.layout.tutorial_complete;
v = inflater.inflate(R.layout.tutorial_complete, null, false);
LinearLayout l = (LinearLayout) v.findViewById(R.id.llComplete);
Button button=(Button)l.findViewById(R.id.bTutorialComplete);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Create an intent that starts a different activity
}
});
break;
}
((ViewPager) collection).addView(v, 0);
return v;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
}
From what I have seen in articles, the code I should be working with is:
public Object instantiateItem(View collection, int position) {
View v = new View(collection.getContext());
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0:
resId = R.layout.tutorial_step1;
break;
case 1:
resId = R.layout.tutorial_step2;
break;
case 2:
resId = R.layout.tutorial_step3;
break;
case 3:
resId = R.layout.tutorial_step4;
break;
case 4:
resId = R.layout.tutorial_complete;
v = inflater.inflate(R.layout.tutorial_complete, null, false);
LinearLayout l = (LinearLayout) v.findViewById(R.id.llComplete);
Button button=(Button)l.findViewById(R.id.bTutorialComplete);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Create an intent that starts a different activity
}
});
break;
}
((ViewPager) collection).addView(v, 0);
return v;
}
It is only case 4:
within the switch
statement that I am working with as this has a button whereas the other layouts only contain images and text which requires no interaction.
I have been working on this for countless hours now and still couldn't figure out a way to resolve this. I would be very grateful if you can help me with this.
Upvotes: 1
Views: 5316
Reputation: 3392
final Context context = collection.getContext();
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
context.startActivity(new Intent(context, Dashboard.class));
}
});
break;
P.s. you cannot make collection
final
since the method instantiateItem
is an @Override
Hint: if you want to make pages that are different you can put your code in switch(position)
:
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE)
View v = null;
switch (position) {
case 0:
v = inflater.inflate(R.layout.somelayout);
Button btn = (Button) v.findViewById(R.id.somebutton);
btn.setText("btn");
break;
case 1:
v = inflater.inflate(R.layout.someotherlayout);
TextView tv = (Button) v.findViewById(R.id.sometextview);
tv.setText("btn");
break;
}
((ViewPager) collection).addView(v, 0);
return v;
Upvotes: 4
Reputation: 14941
It looks like you want to create an intent and then start an activity with it. You can use the context from "collection" which you are already accessing. Here's some code which should get you going again:
public void onClick(View v) {
Context context = collection.getContext();
Intent intent = new Intent(context, DashboardActivity.class);
context.startActivity(intent);
}
Note that you'll probably have to make collection become final.
Upvotes: 1