我遇到了ViewPager
类的问题,我有三个页面,每个页面都有一个scrollView
,相对布局和ImageView。
当我点击ImageView时,我需要设置彩色背景。
我的问题是:当我点击ImageView
使其背景颜色时,背景颜色会显示在另一个PagerView页面中的另一个ImageView
上...
对于实例:
我有四个页面,每个页面都加载ImageView
,当我点击第一页中的第一个ImageView时,第二个ViewPager
页面中的ImageView得到了背景颜色..
我需要让我的第一个ImageView
(在第一页中)进行背景化。
这是代码...... 这是我的寻呼机适配器类
public class ViewPagerAdapter extends PagerAdapter {
private DataSetObserver mObserver;
ImageView image,image2;
Activity activity;
int imageArray[];
Point p;
public ViewPagerAdapter(Activity act, int[] imgArra) {
imageArray = imgArra;
activity = act;
}
@Override
public int getItemPosition(Object object) {
// TODO Auto-generated method stub
return super.getItemPosition(object);
}
public int getCount() {
return imageArray.length;
}
public Object instantiateItem(View collection, int position) {
int fff=PageIndicatorActivity.myPager.getCurrentItem();
Toast.makeText(activity, String.valueOf(fff), 5000).show();
ScrollView view = new ScrollView(activity);
RelativeLayout container = new RelativeLayout(activity);
image = new ImageView (activity);
image2=new ImageView(activity);
image.setId(1);
// Parameters
LayoutParams container_params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
LayoutParams content_params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams contents_paramw = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
contents_paramw.addRule(RelativeLayout.BELOW, image.getId());
view.setLayoutParams(container_params);
container.setLayoutParams(container_params);
image.setLayoutParams(content_params);
image2.setLayoutParams(contents_paramw);
image2.setImageResource(R.drawable.aya3);
image.setImageResource(R.drawable.aya2);
// i.setim(imageArray[position]);
/* ImageView vv=(ImageView)collection.findViewById(R.id.imageView1);
vv.setImageResource(R.drawable.aya2);
TextView view = new TextView(activity);
view.setTextIsSelectable(true);
view.setTextSize(30);
view.setTextColor(Color.BLUE);
view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
//view.setScaleType(ScaleType.FIT_XY);
view.setText(imageArray[position]);
((ViewPager) collection).addView(view, 0);
*/
container.addView(image);
container.addView(image2);
view.addView(container);
image.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int location[] = new int[2];
//Here is the problem
image.setBackgroundColor(Color.RED);
//=======================================================
image.setTag("1");
image.getLocationOnScreen(location);
p = new Point();
p.x = location[0]-600;
p.y = location[1]+30;
// Toast.makeText(activity, String.valueOf(location[0]), 5000).show();
// Toast.makeText(activity, String.valueOf(location[1]), 5000).show();
showPopup(activity, p);
notifyDataSetChanged();
}
});
image2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int location[] = new int[2];
//Here is the problem
image2.setBackgroundColor(Color.RED);
//=======================================================
image.getLocationOnScreen(location);
p = new Point();
p.x = location[0]-600;
p.y = location[1]+100;
// Toast.makeText(activity, String.valueOf(location[0]), 5000).show();
//Toast.makeText(activity, String.valueOf(location[1]), 5000).show();
showPopup(activity, p);
notifyDataSetChanged();
}
});
//container.addView(text);
//container.addView(image);
((ViewPager) collection).addView(view,0);
return view;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
//Toast.makeText(activity.getApplicationContext(), String.valueOf(arg1), 5000).show();
((ViewPager) arg0).removeView((ScrollView) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((ScrollView) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void notifyDataSetChanged() {
// TODO Auto-generated method stub
super.notifyDataSetChanged();
if (this.mObserver != null)
this.mObserver.onDataSetChanged();
}
@Override
public void startUpdate(ViewGroup container) {
// TODO Auto-generated method stub
super.startUpdate(container);
}
static abstract interface DataSetObserver
{
public abstract void onDataSetChanged();
}
private void showPopup(final Activity context, Point p) {
int popupWidth = 560;
int popupHeight = 80;
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);
// Creating the PopupWindow
final PopupWindow popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setFocusable(true);
// Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
int OFFSET_X = 30;
int OFFSET_Y = 30;
// Clear the default translucent background
popup.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup at the specified location, + offsets.
popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
// Getting a reference to Close button, and close the popup when clicked.
ImageButton close = (ImageButton) layout.findViewById(R.id.tashgheel);
close.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
popup.dismiss();
}
});
}
}
希望你们能帮我解决这个问题。
答案 0 :(得分:2)
试试这段代码,它运行正常,
final ImageView image = new ImageView (activity);
final ImageView image2=new ImageView(activity);
在instantiateItem(View collection, int position)
内部,因为您使用视图寻呼机上的每个页面都有相同的图像,因此结果集中在最后发起的页面上。
答案 1 :(得分:1)
@Sino Raj即使它有效,这也不是正确的方法。
试试这个
在ImageView
方法内部和instantiateItem
内部声明setOnClickListener
图片,图片2,将行image.setBackgroundColor(Color.RED)
更改为v.setBackgroundColor(Color.RED)