【发布时间】:2023-04-07 02:25:02
【问题描述】:
我想使用 viewpager 在按钮单击时发送消息我还想了解有关如何访问 viewpager 中的视图的更多信息。
我已经尝试过以下代码....不工作
public class MyPagerAdapter extends PagerAdapter {
@Override
public int getCount() {
return 3;
}
@Override
public Object instantiateItem(final View collection, final int position) {
v = new View(collection.getContext());
LayoutInflater inflater =
(LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0:
resId = R.layout.cate1;
v = inflater.inflate(R.layout.cate1, null, false);
add1 = (Button) v.findViewById(R.id.btnAdd);
add1.setOnClickListener( new OnClickListener() {
public void onClick(View m) {
Toast.makeText(collection.getContext(),"click",Toast.LENGTH_LONG).show();
}
});
break;
case 1:
resId = R.layout.cate2;
break;
case 2:
resId = R.layout.cate3;
break;
}
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
@Override
public void destroyItem(final View arg0, final int arg1, final Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(final View arg0, final Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public void finishUpdate(View arg0) {
// TODO Auto-generated method stub
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
// TODO Auto-generated method stub
}
@Override
public Parcelable saveState() {
// TODO Auto-generated method stub
return null;
}
@Override
public void startUpdate(View arg0) {
// TODO Auto-generated method stub
}
}
我已经为按钮 onclick 编写了以下代码.. 不工作..
v = inflater.inflate(R.layout.cate1, null, false);
add1 = (Button) v.findViewById(R.id.btnAdd);
add1.setOnClickListener( new OnClickListener() {
public void onClick(View m) {
Toast.makeText(collection.getContext(),"click",Toast.LENGTH_LONG).show();
}
});
请帮助 提前谢谢你。
【问题讨论】:
-
您能否详细说明“我已尝试以下代码....无法正常工作”。什么不工作?你得到什么错误?
-
add1.setOnClickListener 不工作。
-
它没有做你想做的事?或者它什么都不做?还是抛出异常?如果抛出异常,异常是什么?
-
它什么都不做......没有任何错误......你有任何带有按钮操作的viewpager示例源代码......?感谢您的回复。
标签: android onclick android-viewpager