Java Android Viewpager项目访问
我的目标是能够浏览3种不同的布局,并能够单击每个布局上的项目。目前,刷卡工作正常,可以查看所有3种布局 活动:Java Android Viewpager项目访问,java,android,android-layout,viewflipper,android-viewpager,Java,Android,Android Layout,Viewflipper,Android Viewpager,我的目标是能够浏览3种不同的布局,并能够单击每个布局上的项目。目前,刷卡工作正常,可以查看所有3种布局 活动: public class FetchMenu extends Fetch { protected ImageView block; /** Called when the activity is first created. */ @Override public void onCreate(Bu
public class FetchMenu extends Fetch {
protected ImageView block;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//pagerviwer
MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(1);
//icon on layout 1
fbicon = (ImageView)findViewById(R.id.facebookicon);
fbicon.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com/pages/Fetch-Training/174358202669554"));
startActivity(browserIntent);
// TODO Auto-generated method stub
return false;
}
});
页面适配器:
class MyPagerAdapter extends PagerAdapter {
public int getCount() {
return 3;
}
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0:
resId = R.layout.training_topics;
break;
case 1:
resId = R.layout.behaviour_topics;
break;
case 2:
resId = R.layout.tricks_topics;
break;
}
View v = inflater.inflate(resId, null);
((ViewPager) collection).addView(v, 0);
return v;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
}
问题:
每个被浏览的布局都包含一组ImageView,如果我在活动中设置代码来侦听这些ImageView的触动,我会得到一个强制关闭错误。我猜活动中的代码不知道ImageView存储在哪个布局中?我读过关于碎片的书,这是我需要的吗
谢谢大家一直都很喜欢帮助试试这个:
View v = inflater.inflate(resId, null);
ImageView img = v.findviewbyid(R.id.IMAGEID);
// do what you want with the image
将代码放入开关盒中,然后从开关盒返回以获得最佳结果
i、 e:将以下代码移动到每个案例中
View v = inflater.inflate(resId, null);
((ViewPager) collection).addView(v, 0);
return v;
试试这个:
View v = inflater.inflate(resId, null);
ImageView img = v.findviewbyid(R.id.IMAGEID);
// do what you want with the image
将代码放入开关盒中,然后从开关盒返回以获得最佳结果
i、 e:将以下代码移动到每个案例中
View v = inflater.inflate(resId, null);
((ViewPager) collection).addView(v, 0);
return v;
谢里夫是对的。假设您有两个文本视图。在R.layout.training\u主题布局中。和一个关于R.layout.u主题的图像视图。如果使用PageAdapter,代码应该如下所示
int resId = 0;
View v = null;
switch (position) {
case 0:
resId = R.layout.training_topics;
v = inflater.inflate(resId, null);
View tv = v.findViewById(R.id.text1);
View tv2 = v.findViewById(R.id.text2);
((TextView)tv).setText("testtesttes");
((TextView)tv2).setText("sttesttes2");
break;
case 1:
resId = R.layout.behaviour_topics;
v = inflater.inflate(resId, null);
break;
case 2:
resId = R.layout.tricks_topics;
v = inflater.inflate(resId, null);
View im = v.findViewById(R.id.image1);
((ImageView)im).setBackgroundResource(R.drawable.icon);
break;
}
((ViewPager) collection).addView(v, 0);
return v;
但不要被我的想法搞糊涂了
View im = v.findViewById(R.id.image1);
((ImageView)im).setBackgroundResource(R.drawable.icon);
你仍然可以使用
ImageView im = v.findViewById(R.id.image1);
im.setBackgroundResource(R.drawable.icon);
谢里夫是对的。假设您有两个文本视图。在R.layout.training\u主题布局中。和一个关于R.layout.u主题的图像视图。如果使用PageAdapter,代码应该如下所示
int resId = 0;
View v = null;
switch (position) {
case 0:
resId = R.layout.training_topics;
v = inflater.inflate(resId, null);
View tv = v.findViewById(R.id.text1);
View tv2 = v.findViewById(R.id.text2);
((TextView)tv).setText("testtesttes");
((TextView)tv2).setText("sttesttes2");
break;
case 1:
resId = R.layout.behaviour_topics;
v = inflater.inflate(resId, null);
break;
case 2:
resId = R.layout.tricks_topics;
v = inflater.inflate(resId, null);
View im = v.findViewById(R.id.image1);
((ImageView)im).setBackgroundResource(R.drawable.icon);
break;
}
((ViewPager) collection).addView(v, 0);
return v;
但不要被我的想法搞糊涂了
View im = v.findViewById(R.id.image1);
((ImageView)im).setBackgroundResource(R.drawable.icon);
你仍然可以使用
ImageView im = v.findViewById(R.id.image1);
im.setBackgroundResource(R.drawable.icon);