赞
踩
在使用ListView的时候,有时是在item被选中时,字体颜色的变化,松开跳转了之后就还原颜色,这时怎么做呢
1、在适配器的getView 方法:
@Override public View getView(int position, View convertView, ViewGroup parent) { final ViewH mViewH; if (convertView == null) { convertView = View.inflate(mContext, R.layout.selectorphone_adapter, null); mViewH = new ViewH(); mViewH.txt1 = (TextView) convertView.findViewById(R.id.txt1); convertView.setTag(mViewH); } else { mViewH = (ViewH) convertView.getTag(); } mViewH.txt1.setText(mArrayList.get(position).get("txt1").toString()); // 点击Item跳转是刷新适配器,改回原来的颜色。 if (position == selectItem) { mViewH.txt1.setTextColor(Color.BLACK); } mViewH.txt1.setTextColor(Color.BLACK); //按下Item时设置为你想要的颜色。 convertView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case KeyEvent.ACTION_DOWN: mViewH.txt1.setTextColor(Color.GREEN); Log.i("TAG", "ACTION_DOWN"); break; } return false; } }); return convertView; }
/2、但是又问题,为什么不直接在case KeyEvent.ACTION_UP:处还原呢,哈哈哈,事件传不到哪了,所以给ListView设置监听事件吧
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position,
- long id) {
- mSelectorphone_Adapter.setSelectItem(position);
- mSelectorphone_Adapter.notifyDataSetChanged();
- Intent intent = new Intent(this, Switchphone_Act.class);
- intent.putExtra("name", mArrayList.get(position));
- startActivity(intent);
-
- }
/3、再有
// 点击Item跳转是刷新适配器,改回原来的颜色。
这个代码上面有了
- if (position == selectItem) {
- mViewH.txt1.setTextColor(Color.BLACK);
- }
然后在再,适配器里面写这个这个方法:
- public void setSelectItem(int selectItem) {
- this.selectItem = selectItem;
- }
哈哈哈哈,搞定了!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。