更改行背景颜色,而项目在ListView控件触摸 [英] Change row background color while item be touch in ListView

查看:174
本文介绍了更改行背景颜色,而项目在ListView控件触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用BaseAdapter ListView中显示的项目。
我尝试以下code在BaseAdapter。

I try to use BaseAdapter to show item in ListView. I try below code in BaseAdapter.

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
    //...
    convertView.setOnTouchListener(new OnTouchListener() {
@Override
    public boolean onTouch(View v, MotionEvent event) {
       switch(event.getAction()) {
         case MotionEvent.ACTION_DOWN:
         v.setBackgroundResource(R.drawable.ic_corner_four_click);
     break;

         case MotionEvent.ACTION_UP:
         v.setBackgroundResource(R.drawable.ic_corner_four);
     break;
     }

     return false;
     }
   });
}

虽然项目被感动,将其更改背景ic_corner_four_click。
不过,虽然释放手指或转移到其他项目,也没有重新改变为ic_corner_four。
如何修改呢?

While item be touched, it change background to ic_corner_four_click. But while release finger or move to other item, it did not rechange to ic_corner_four. How to modify it?

推荐答案

您应该使用 StateListDrawable 来定义特定状态的背景。 参见文档。如果你看看右边这个问题,你可以看到其他非常类似的问题。 --->

You should use a StateListDrawable to define the background in a specific state. See the documentation. If you look to the right of the question you can see other very similar questions. --->

<一个href=\"http://stackoverflow.com/questions/6939838/change-the-background-image-of-listview-on-focus\">This 之一,例如。

这篇关于更改行背景颜色,而项目在ListView控件触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆