Android Viewpager项目访问权限

我的目标是能够刷过3种不同的布局,并能够点击每个布局上的项目。 在滑动工作正常的时刻,可以查看所有3种布局。

活动:

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; } }); 

PageAdapter:

 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,如果我在我的Activity中设置代码来监听它们的图像视图,我会收到一个强制关闭错误。 我猜测活动中的代码不知道ImageView存储在哪个布局中? 我读过关于片段的内容这是我需要的吗?

谢谢你们总是喜欢这个帮助

试试这个:

 View v = inflater.inflate(resId, null); ImageView img = v.findviewbyid(R.id.IMAGEID); // do what you want with the image 

将代码放在开关盒中并从那里返回以获得最佳结果

即:将以下代码移至每个案例

  View v = inflater.inflate(resId, null); ((ViewPager) collection).addView(v, 0); return v; 

Shereef是对的。 假设您有两个文本视图。 在你的R.layout.training_topics布局中。 您的R.layout.tricks_topics上有一个图像视图。 你使用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);