我有 LinearLayout,我在其中动态添加视图。单击它时,我需要更改每个视图的背景。
我尝试在我的代码中执行此操作:
public class UserDetailActivity extends Activity implements View.OnTouchListener {
...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.l_user_detail);
Intent intent = getIntent();
User user = (User) intent.getSerializableExtra("class");
userWillGo = (LinearLayout) findViewById(R.id.linerLayout_userDetail_willGoTO);
for (int i = 0; i < user.getUserWillGo().size(); i++) {
View myView = (View)LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_event_list, userWillGo, false);
myView.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT));
...
myView.setOnTouchListener(mOnTouchListener());
userWillGo.addView(myView);
}
}
private View.OnTouchListener mOnTouchListener() {
return new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
v.setBackgroundResource(R.color.textBlue);
}
if (event.getAction() == MotionEvent.ACTION_UP) {
v.setBackgroundResource(R.drawable.bg_item);
}
return true;
}
};
}
我得到这个代码的结果:
我需要得到什么: