当我触摸布局时,图像不会改变。它是动态的,我已将其添加到布局中。这是我创建动态布局的完整代码
ID = 1;
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
width = size.x;
height = size.y;
Log.i("VRV", "Width :: " + width);
Log.i("VRV", "height :: " + height);
mWidth = width / 32;
Log.i("VRV", "mWidth :: " + mWidth);
int minHeight1 = height / 10;
int mHeight = height - (int) (minHeight1 * 0.9);
int minHeight = mHeight / 10;
Log.i("VRV", "minHeight is ::" + minHeight);
mAddingValue = (int) (minHeight * 0.9) + (int) (minHeight * 1);
mHeaderLayout.getLayoutParams().height = (int) (minHeight * 2);
mFooterLayout.getLayoutParams().height = (int) (minHeight * 2);
mLinearLayout.getLayoutParams().height = (int) (minHeight * 6);
mLinearLayout.setOnTouchListener(onTouchListener);
connect(getIntent().getStringExtra("DEVICENUM"));
ArrayList<String> list = new ArrayList<String>();
ArrayList<Integer> jList = new ArrayList<>();
for (int i = 0; i < 209; i++) {
int rowno = i / 30;
int col = i % 7;
int j1 = (rowno * 100) + col;
if (list.contains("" + j1)) {
Log.d("VRV13", " design new " + i);
jList.add(i + 1);
}
}
Log.i("VRV", "list.toString() :: " + jList.toString());
for (int i = 0; i < 7; i++) {
LinearLayout linearLayout = new LinearLayout(Drawingletters.this);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mWidth));
linearLayout.setHorizontalGravity(Gravity.CENTER_HORIZONTAL);
mViewList.add(linearLayout);
for (int j = 0; j < 30; j++) {
mView = new ImageView(Drawingletters.this);
mView.setId(ID + j);
mView.setLayoutParams(new LinearLayout.LayoutParams(mWidth, mWidth));
Log.i("VRV", "list.contains(ID) :: " + ID + " ==> " + list.contains(ID));
if (jList.contains(ID + j)) {
mView.setTag(1);
mView.setImageResource(R.drawable.round_white);
} else {
mView.setTag(0);
mView.setImageResource(R.drawable.round_gray);
}
mView.setPadding(1, 1, 1, 1);
mTextList.add(mView);
linearLayout.addView(mView);
}
ID = ID + 30;
mLinearLayout.addView(linearLayout);
}
当我触摸或拖动它时,我将白色圆形图像设置为它。那么布局应该看起来像这样
在此我拖动布局时白色图像是根据xy坐标显示的,当我点击单个白色图像时它会消失(不完全消失,因为我将灰色圆形图像设置为它。但我的主要问题是当我点击一个灰色的圆形图像时,图像没有变化(只有当我拖动时它才会改变)。我想在触摸时更改图像并拖动它们。 这是我的onTouchListner代码
View.OnTouchListener onTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int y = (int) event.getY();
int x = (int) event.getX();
if (y / mWidth < 7 && y / mWidth > -1) {
ViewGroup viewGroup = mViewList.get(y / mWidth);
ImageView mText = (ImageView) viewGroup.getChildAt(x / mWidth);
if (mText != null) {
if (isTouched && event.getAction() == MotionEvent.ACTION_UP) {
isTouched = false;
Log.i("VRV", "onTouchEvent mText.getId() :: " + mTextList.get(mText.getId() - 1).getTag());
mText.setImageResource(R.drawable.round_gray);
mText.setTag(1);
Log.i("VRV", "onTouchEvent mText.getId() :: " + mText.getId());
mTextList.get(mText.getId()-1).setTag(1);
return true;
} else{
isTouched = true;
isTouchedenable = false;
mText.setImageResource(R.drawable.round_white);
mText.setTag(0);
Log.i("VRV", "onTouchEvent mText.getId() :: " + mText.getId());
mTextList.get(mText.getId()-1).setTag(0);
}
}
}
return true;
}
};
请帮我解决这个问题