0

カルーセルビューで作業しています。1つのレイアウトで、4枚の画像を配置しました。そして、各画像がクリックされたときに異なるメソッドを設定したいので、各画像を各オブジェクトとして設定したいと思います。

    ImageButton product_photo = (ImageButton) l.findViewById(R.id.myoffer_image);

    product_photo.setImageResource(R.drawable.myoffers_0);

    product_photo.setImageResource(R.drawable.myoffers_1);

    product_photo.setImageResource(R.drawable.myoffers_2);

    product_photo.setImageResource(R.drawable.myoffers_3);

私は以下のコードのように試しました..しかし、確かにエラーになります..

ImageButton one = product_photo.setImageResource(R.drawable.myoffers_0);

私の質問が明確かどうかわかりません..でも、もっと明確にわかるような回答があれば、説明します!

これが pageadapter クラスです。

public class Myoffers_PagerAdapter extends FragmentPagerAdapter implements
    ViewPager.OnPageChangeListener {

private Myoffers_LinearLayout cur = null;
private Myoffers_LinearLayout next = null;
private Myoffers context;
private FragmentManager fm;
private float scale;

public Myoffers_PagerAdapter(Myoffers context, FragmentManager fm) {
    super(fm);
    this.fm = fm;
    this.context = context;
}

@Override
public Fragment getItem(int position) 
{
    // make the first pager bigger than others
    if (position == Myoffers.FIRST_PAGE)
        scale = Myoffers.BIG_SCALE;         
    else
        scale = Myoffers.SMALL_SCALE;

    position = position % Myoffers.PAGES;
    return Myoffers_Fragment.newInstance(context, position, scale);
}

@Override
public int getCount()
{       
    return Myoffers.PAGES * Myoffers.LOOPS;
}

//@Override
public void onPageScrolled(int position, float positionOffset,
        int positionOffsetPixels) 
{   
    if (positionOffset >= 0f && positionOffset <= 1f)
    {
        cur = getRootView(position);
        next = getRootView(position +1);

        cur.setScaleBoth(Myoffers.BIG_SCALE 
                - Myoffers.DIFF_SCALE * positionOffset);
        next.setScaleBoth(Myoffers.SMALL_SCALE 
                + Myoffers.DIFF_SCALE * positionOffset);
    }
}

//@Override
public void onPageSelected(int position) {}

//@Override
public void onPageScrollStateChanged(int state) {}

private Myoffers_LinearLayout getRootView(int position)
{
    return (Myoffers_LinearLayout) 
            fm.findFragmentByTag(this.getFragmentTag(position))
            .getView().findViewById(R.id.root);
}

private String getFragmentTag(int position)
{
    return "android:switcher:" + context.pager.getId() + ":" + position;
}

}

メインクラス!

public class Myoffers extends FragmentActivity {
public final static int PAGES = 4;
public final static int LOOPS = 1000; 
public final static int FIRST_PAGE = PAGES * LOOPS / 2;
public final static float BIG_SCALE = 1.0f;
public final static float SMALL_SCALE = 0.5f;
public final static float DIFF_SCALE = BIG_SCALE - SMALL_SCALE;

public Myoffers_PagerAdapter adapter;
public ViewPager pager;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.myoffers);

    pager = (ViewPager) findViewById(R.id.myviewpager);

    adapter = new Myoffers_PagerAdapter(this, this.getSupportFragmentManager());
    pager.setAdapter(adapter);
    pager.setOnPageChangeListener(adapter);

    pager.setCurrentItem(FIRST_PAGE);

    pager.setOffscreenPageLimit(3);

    pager.setPageMargin(-100);
}

}

フラグメント コード public class Myoffers_Fragment extends Fragment {

protected static final String TAG = "Philips";
FileOutputStream mOutputStream; 
private String id;

public static Fragment newInstance(Myoffers context, int pos, float scale)
{
    Bundle b = new Bundle();
    b.putInt("pos", pos);
    b.putFloat("scale", scale);

    return Fragment.instantiate(context, Myoffers_Fragment.class.getName(), b);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (container == null) {
        return null;
    }

    LinearLayout l = (LinearLayout) inflater.inflate(R.layout.mf, container, false);

    int pos = this.getArguments().getInt("pos");
    TextView tv = (TextView) l.findViewById(R.id.text);
    tv.setText("Product " + pos);


    /*
     * Put images with Hard-Coding
     */
    {
    ImageButton product_photo = (ImageButton) l.findViewById(R.id.myoffer_image);

    product_photo.setImageResource(R.drawable.myoffers_0);

    product_photo.setImageResource(R.drawable.myoffers_1);

    product_photo.setImageResource(R.drawable.myoffers_2);

    product_photo.setImageResource(R.drawable.myoffers_3);

    }


    Myoffers_LinearLayout root = (Myoffers_LinearLayout) l.findViewById(R.id.root);
    float scale = this.getArguments().getFloat("scale");
    root.setScaleBoth(scale);

    return l;
    }

ありがとう!

4

2 に答える 2