选中时如何更改卡片视图的颜色?单击
how to change the colour of a cardview when selected?clicked
我正在尝试使用卡片视图而不是按钮,我喜欢您可以向其中添加的信息量。但我正在努力做到,如果他们按下卡片,它就会改变颜色。我希望它在发布后变回原样。这样它的工作方式与我的按钮类似。
我可以得到它,以便它在点击时发生变化,但它会一直保持这种状态,直到 activity 被销毁,
这是我目前用于更改颜色的代码。
public void setSingleEvent(GridLayout maingrid) {
for (int i = 0; i < maingrid.getChildCount(); i++) {
final CardView cardView = (CardView) maingrid.getChildAt(i);
final int finalI = i;
cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(mcontext, "Button: " + finalI, Toast.LENGTH_SHORT).show();
cardView.setCardBackgroundColor(mcontext.getResources().getColor(R.color.buttonPressed));
if (finalI == 0) {
mcontext.startActivity(new Intent(mcontext, Genre_Streaming.class));
}
}
});
您可以尝试使用 onTouchListener
而不是 onClickListener
来捕获 "Motion event" 这是一个用于报告移动(鼠标、笔、手指、轨迹球)事件的对象。
cardview.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// set color here
} else if (event.getAction() == MotionEvent.ACTION_UP) {
// set other color here
}
}
};
这是来自 android documentation page
的运动事件常量列表
您可以尝试使用 OnTouchListener
与 ACTION_DOWN
和 ACTION_UP
来处理 Press/Release 事件而不是 OnClickListener
。
修改代码:
public void setSingleEvent(GridLayout maingrid) {
for (int i = 0; i < maingrid.getChildCount(); i++) {
final CardView cardView = (CardView) maingrid.getChildAt(i);
final int finalI = i;
cardView.setOnTouchListener(new OnTouchListener () {
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
Toast.makeText(mcontext, "Button: " + finalI, Toast.LENGTH_SHORT).show();
cardView.setCardBackgroundColor(mcontext.getResources().getColor(R.color.buttonPressed));
if (finalI == 0) {
mcontext.startActivity(new Intent(mcontext, Genre_Streaming.class));
}
} else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
/* Reset Color */
cardView.setCardBackgroundColor(mcontext.getResources().getColor(R.color.red));
}
return true;
}
}
}
Link: http://developer.android.com/reference/android/view/MotionEvent.html#ACTION_UP
我正在尝试使用卡片视图而不是按钮,我喜欢您可以向其中添加的信息量。但我正在努力做到,如果他们按下卡片,它就会改变颜色。我希望它在发布后变回原样。这样它的工作方式与我的按钮类似。
我可以得到它,以便它在点击时发生变化,但它会一直保持这种状态,直到 activity 被销毁,
这是我目前用于更改颜色的代码。
public void setSingleEvent(GridLayout maingrid) {
for (int i = 0; i < maingrid.getChildCount(); i++) {
final CardView cardView = (CardView) maingrid.getChildAt(i);
final int finalI = i;
cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(mcontext, "Button: " + finalI, Toast.LENGTH_SHORT).show();
cardView.setCardBackgroundColor(mcontext.getResources().getColor(R.color.buttonPressed));
if (finalI == 0) {
mcontext.startActivity(new Intent(mcontext, Genre_Streaming.class));
}
}
});
您可以尝试使用 onTouchListener
而不是 onClickListener
来捕获 "Motion event" 这是一个用于报告移动(鼠标、笔、手指、轨迹球)事件的对象。
cardview.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// set color here
} else if (event.getAction() == MotionEvent.ACTION_UP) {
// set other color here
}
}
};
这是来自 android documentation page
的运动事件常量列表您可以尝试使用 OnTouchListener
与 ACTION_DOWN
和 ACTION_UP
来处理 Press/Release 事件而不是 OnClickListener
。
修改代码:
public void setSingleEvent(GridLayout maingrid) {
for (int i = 0; i < maingrid.getChildCount(); i++) {
final CardView cardView = (CardView) maingrid.getChildAt(i);
final int finalI = i;
cardView.setOnTouchListener(new OnTouchListener () {
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
Toast.makeText(mcontext, "Button: " + finalI, Toast.LENGTH_SHORT).show();
cardView.setCardBackgroundColor(mcontext.getResources().getColor(R.color.buttonPressed));
if (finalI == 0) {
mcontext.startActivity(new Intent(mcontext, Genre_Streaming.class));
}
} else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
/* Reset Color */
cardView.setCardBackgroundColor(mcontext.getResources().getColor(R.color.red));
}
return true;
}
}
}
Link: http://developer.android.com/reference/android/view/MotionEvent.html#ACTION_UP