我正在使用轮播视图。使用一种布局,我放了四张图片。我想将每个图像设置为每个对象,因为我想在单击每个图像时设置不同的方法。
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 扩展片段 {
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;
}
谢谢!