如何为viewpager添加单击事件实现界面跳转
2016-10-09 · 百度知道合伙人官方认证企业
为viewpager添加单击事件实现界面跳转的实现方法如下:
通过事件的传递关系,需要在instantiateItem里面逐层设置onclick事件
@Override
public Object instantiateItem(View collection, final int pos) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View page = inflater.inflate(R.layout.YOUR_PAGE, null);
page.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Log.i("TAG", "This page was clicked: " + pos);
}
});
((ViewPager) collection).addView(page, 0);
return page;
}
2.指定跳转的页面:
cancelButton = (Button) findViewById(R.id.cancel_button);
signInButton = (Button) findViewById(R.id.sign_in_button);
2016-10-01
ViewPager 需要加载 View;
给你加载的 View 设置点击事件就能实现跳转了。