码迷,mamicode.com
首页 > 移动开发 > 详细

【Android笔记】listview点击或选中item改变item样式或背景

时间:2015-06-03 15:14:27      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

1、自定义Adapter中配置:

 1 public class MyAdapter extends BaseAdapter{
 2 
 3  int mSelect = 0;   //选中项
 4 
 5      ......
 6 
 7  public void changeSelected(int positon){ //刷新方法
 8      if(positon != mSelect){
 9       mSelect = positon;
10      notifyDataSetChanged();
11      }
12     }
13 
14 
15 
16 public View getView(int position, View convertView, ViewGroup parent) {
17 //     if(convertView==null){
18         LayoutInflater factory = LayoutInflater.from(context);
19         View v = (View) factory.inflate(R.layout.list_item, null);
20         TextView tv = (TextView) v.findViewById(R.id.Item_tv);
21         tv.setText("test");
22 //     }
23         if(mSelect==position){    
24          v.setBackgroundResource(R.drawable.fc_bg);  //选中项背景
25         }else{
26          v.setBackgroundResource(R.drawable.bg);  //其他项背景
27         }
28         
29         return v;
30     }
31 
32 }

2、在Activity中应用:

 1 ...........
 2 
 3 mAdapter = new MyAdapter(...);
 4 
 5 myList.setAdapter(mAdapter);
 6 
 7 //////////点击监听
 8 
 9 myList.setOnItemClickListener(new OnItemClickListener() {
10 
11   @Override
12   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
13           mAdapter.changeSelected(position);//刷新
14 
15   }......}
16 
17 ////////////////选中监听
18 
19 myList.setOnItemSelectedListener(new OnItemSelectedListener() {
20 
21   @Override
22   public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
23             mAdapter.changeSelected(position);//刷新
24 
25   }......}

参考原文:http://www.tuicool.com/articles/YFBVJf

【Android笔记】listview点击或选中item改变item样式或背景

标签:

原文地址:http://www.cnblogs.com/tanghuian/p/4549118.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!