Skip to content Skip to sidebar Skip to footer

Using Buttons Within A Viewpager

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

Solution 1:

finalContextcontext= collection.getContext();
    button.setOnClickListener(newOnClickListener() {

        @OverridepublicvoidonClick(View v) {
            context.startActivity(newIntent(context, Dashboard.class));
        }
    });
    break;

P.s. you cannot make collectionfinal 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):

LayoutInflaterinflater= (LayoutInflater) collection.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE)
Viewv=null;
switch (position) {
    case0:
        v = inflater.inflate(R.layout.somelayout);
        Buttonbtn= (Button) v.findViewById(R.id.somebutton);
        btn.setText("btn");
    break;

    case1:
        v = inflater.inflate(R.layout.someotherlayout);
        TextViewtv= (Button) v.findViewById(R.id.sometextview);
        tv.setText("btn");
    break;
}

((ViewPager) collection).addView(v, 0);
return v;

Solution 2:

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:

publicvoidonClick(View v) {
    Contextcontext= collection.getContext();
    Intentintent=newIntent(context, DashboardActivity.class);
    context.startActivity(intent);
}

Note that you'll probably have to make collection become final.

Post a Comment for "Using Buttons Within A Viewpager"