如何访问我的 viewpager 页面之一中的按钮?
我对这个 android 和 java 很陌生。
我的 viewpager 现在可以滑动我所有的 xml 布局,但我不知道如何访问其中的项目。
我尝试使用 Button mybutton = (Button) findViewById (R.layout.result)
,然后在 mybutton 中设置 onClickListener
,但每次运行时它都被强制关闭它。
这是我的代码:
public class main extends Activity {
private ViewPager myviewpager;
private ViewPagerAdapter myviewpageradapter;
private static int NUM_AWESOME_VIEWS = 12;
Button button_1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myviewpageradapter = new ViewPagerAdapter();
myviewpager = (ViewPager) findViewById(R.id.myviewpager);
myviewpager.setAdapter(myviewpageradapter);
button_1 = (Button) myviewpager.findViewById(R.id.first_button);
button_1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
private class ViewPagerAdapter extends PagerAdapter {
@Override
public int getCount() {
return NUM_AWESOME_VIEWS;
}
@Override
public Object instantiateItem(View collection, int position) {
LayoutInflater layoutinflater = getLayoutInflater();
View view;
view = layoutinflater.inflate(R.layout.page1, null);
switch (position)
{
case 0 : view = layoutinflater.inflate(R.layout.page1, null); break;
case 1 : view = layoutinflater.inflate(R.layout.page2, null); break;
case 2 : view = layoutinflater.inflate(R.layout.page3, null); break;
case 3 : view = layoutinflater.inflate(R.layout.page4, null); break;
case 4 : view = layoutinflater.inflate(R.layout.page5, null); break;
case 5 : view = layoutinflater.inflate(R.layout.page6, null); break;
case 6 : view = layoutinflater.inflate(R.layout.page7, null); break;
case 7 : view = layoutinflater.inflate(R.layout.page8, null); break;
case 8 : view = layoutinflater.inflate(R.layout.page9, null); break;
case 9 : view = layoutinflater.inflate(R.layout.page10, null); break;
case 10 : view = layoutinflater.inflate(R.layout.page11, null); break;
case 11 : view = layoutinflater.inflate(R.layout.result, null);break;
}
((ViewPager) collection).addView(view,0);
return view;
}
@Override
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((View) view);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==((View)object);
}
@Override
public void finishUpdate(View arg0) {}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View arg0) {}
}
I'm new to this android and java.
My viewpager now can slide all my xml layout, but i don't know how to access item in them.
I tried using Button mybutton = (Button) findViewById (R.layout.result)
, then set the onClickListener
in mybutton, but then it was forced to close every time I run it.
Here is my code:
public class main extends Activity {
private ViewPager myviewpager;
private ViewPagerAdapter myviewpageradapter;
private static int NUM_AWESOME_VIEWS = 12;
Button button_1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myviewpageradapter = new ViewPagerAdapter();
myviewpager = (ViewPager) findViewById(R.id.myviewpager);
myviewpager.setAdapter(myviewpageradapter);
button_1 = (Button) myviewpager.findViewById(R.id.first_button);
button_1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
private class ViewPagerAdapter extends PagerAdapter {
@Override
public int getCount() {
return NUM_AWESOME_VIEWS;
}
@Override
public Object instantiateItem(View collection, int position) {
LayoutInflater layoutinflater = getLayoutInflater();
View view;
view = layoutinflater.inflate(R.layout.page1, null);
switch (position)
{
case 0 : view = layoutinflater.inflate(R.layout.page1, null); break;
case 1 : view = layoutinflater.inflate(R.layout.page2, null); break;
case 2 : view = layoutinflater.inflate(R.layout.page3, null); break;
case 3 : view = layoutinflater.inflate(R.layout.page4, null); break;
case 4 : view = layoutinflater.inflate(R.layout.page5, null); break;
case 5 : view = layoutinflater.inflate(R.layout.page6, null); break;
case 6 : view = layoutinflater.inflate(R.layout.page7, null); break;
case 7 : view = layoutinflater.inflate(R.layout.page8, null); break;
case 8 : view = layoutinflater.inflate(R.layout.page9, null); break;
case 9 : view = layoutinflater.inflate(R.layout.page10, null); break;
case 10 : view = layoutinflater.inflate(R.layout.page11, null); break;
case 11 : view = layoutinflater.inflate(R.layout.result, null);break;
}
((ViewPager) collection).addView(view,0);
return view;
}
@Override
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((View) view);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==((View)object);
}
@Override
public void finishUpdate(View arg0) {}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View arg0) {}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能只是问题,您的项目可能没有找到,所以尝试这个(如果您的视图不是 LinearLayout 更改它)
但是您的按钮可能会抛出异常,而且还设置了一个 onClickListener 。
如果只是在 XML 中定义按钮:
那么在代码中
如果这是不可能的,因为您稍后需要引用按钮,我遇到了同样的问题,并且想知道它是如何工作的。
It may just be the Problem, that your items are not found probably so try this(If your view is no LinearLayout change it)
But Your Button may throw Exception furthermore there is an onClickListener set to it.
If it's just the Button define it in XML:
then in Code
If this is not possible because you need to reference the Button later, I stuck with the same Problem and would like to know how it works.
我认为这里的问题是您正在尝试查找尚未初始化(膨胀)的视图(findViewById)。尝试在 ViewPagerAdapter 类型的 instatiateItem 方法内设置按钮侦听器。
例子:
I think the problem here is that you are trying to find a view (findViewById) that wasn't instatiated (inflated) yet. Try to set the button listener inside the instatiateItem method of ViewPagerAdapter type.
Example: