Using buttons within a ViewPager

旧巷老猫 提交于 2019-12-03 09:11:31
    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;

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!