如何将Imageview设置为对象?

时间:2013-06-14 20:09:05

标签: android object imageview

我正在使用轮播视图。有了一个布局,我放了四张图片。 我想将每个图像设置为每个对象,因为我想在单击每个图像时设置不同的方法。

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

}

片段代码     公共类Myoffers_Fragment扩展了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;
    }

谢谢!

2 个答案:

答案 0 :(得分:0)

你的问题有点模糊,但我会尽我所能回应 - 如果我误解了什么,请随时告诉我,我会相应地编辑我的答案。

由于'carousel'不是android SDK的一部分,因此确定你需要做什么有点困难(如果你展示了你用于这个轮播的类/库会有所帮助)。但是,它可能会在实现中使用此类或其子类:AbsListView

每个基于listview的类都有一些在单击项目时调用的方法(这里是performItemClick)。当它被调用时,它将依次调用相关AdapterView的onItemClick方法。适配器是您(或您正在使用的库)创建的,填充了要显示的数据,并附加到ListView。

通过注册AdapterView.OnClickListener,您可以指定在任何给定索引处单击列表项时将导致的行为。

如果没有关于您的轮播的更多详细信息,我无法提供更多针对特定代码的帮助。但是,如果您发布更多信息,我很乐意更新我的答案!

答案 1 :(得分:0)

ImageButton是一个类,您创建了一个名为“product_photo”的对象。然后你创建另一个名为'one'。问题是您尝试将方法分配给一个方法而不是创建新的ImageButton。相反,你应该

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

您的布局中是否需要“一个”才能使用findViewById(可能是您这样做)取决于您的应用程序。