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:

    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;

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:

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.


Post a Comment for "Using Buttons Within A ViewPager"