2012-01-28 83 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);