2012-01-28 51 views
2

我的目标是能够滑过3种不同的布局,并且能够点击每个布局上的项目。目前滑动运行良好,所有3种布局均可查看。Android Viewpager项目访问

活动:

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

问题: 的每个打通刷卡布局,包含了一些ImageViews的,如果我在我的活动设置代码那些倾听他们对图像的触动,我得到了一个关闭力的错误。我猜测活动中的代码不知道ImageView存储在哪个布局中?我已阅读关于片段是我需要的吗?

谢谢你们一直爱帮助

回答

2

试试这个:

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; 
2

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; 

但是不要被confuesd我

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