我有一个列表视图,我只想在触摸它时更改行(项目)的背景颜色(MotionEvent.ACTION_DOWN),但在滚动列表视图时防止这种情况发生。我怎样才能做到这一点 ?
这是我的 ListView 的 BaseAdapter
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
DataHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new DataHolder();
holder.newAppsLayout = (LinearLayout) row.findViewById(R.id.new_apps_layout);
holder.imageView1 = (ImageView) row.findViewById(R.id.imageView1);
row.setTag(holder);
} else {
holder = (DataHolder) row.getTag();
}
Data data = data.get(position);
ImageCacheManager.loadImage(holder.imageView1, item.getIconUrl());
final DataHolder finalHolder = holder;
row.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
finalHolder.newAppsLayout.setBackgroundColor(Color.GRAY);
} else {
finalHolder.newAppsLayout.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.content_bg)); }
return false;
}
});
return row;
static class DataHolder {
LinearLayout newAppsLayout;
ImageView imageView1;
}
它可以工作,但也可以在触摸滚动时工作。怎样才能防止这种情况?我的意思是,仅适用于触摸事件但不适用于滚动?
提前致谢