问题描述
我想用viewpager我也想就如何访问意见viewpager更多信息敬酒按钮单击消息。
I want to Toast a message on button click using viewpager also I want more information on how to access views in viewpager.
我曾尝试以下code ....不工作
I have tried following code.... not working
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
}
}
以下$ C $下按钮,我写的onclick ....不工作。
I have written following code for button onclick.... not working..
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();
}
});
plz帮助预先感谢您。
plz HELPthank you in advance.
推荐答案
留下你code都是一样的,但改变:
Leave your code all the same butchange:
((ViewPager) collection).addView(view, 0);
return view;
到
((ViewPager) collection).addView(v, 0);
return v;
你这样做是充气的按钮的onclick正确的,那么你充气另一种观点iwth渣油和屏幕,该按钮存在于你的应用程序的内存上呈现,但没有别的地方,通过返回V,这个按钮是绘制和onclicklistener上点击调用。
you did the onclick right on a button that was inflated, then you inflated another view iwth resId and rendered it on the screen, that button exists in the memory of your app but no where else, by returning v, that button is drawn and the onclicklistener is invoked on click.
这篇关于如何写viewpager按钮的onClick方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!