2

私の目的は、3つの異なるレイアウトをスワイプし、各レイアウトのアイテムをクリックできるようにすることです。現時点ではスワイプがうまくいき、3つのレイアウトすべてを表示できます。Androidビューページアイテムのアクセス

活動:

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

シェリーフが正しい。 2つのテキストビューがあるとします。あなたのR.layout.training_topicsレイアウト。 R.layout.tricks_topicsの1つの画像ビュー。そして、あなたは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; 

しかし、あなたはまだ

ImageView im = v.findViewById(R.id.image1); 
    im.setBackgroundResource(R.drawable.icon); 
を使用することができ、私の

View im = v.findViewById(R.id.image1); 
    ((ImageView)im).setBackgroundResource(R.drawable.icon); 

によってconfuesdされていないように見えるはずです

関連する問題