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


    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

  • 相关阅读:
    青檬音乐台闲时听听电台吧
    [转]What is AOP?
    实现页面元素拖动效果的JS函数
    gorithms.算法概论.习题答案
    Asp.Net读取并显示Excel文件中的内容(OleDb方式)
    收藏的一些小软件
    Master Page Path (MasterPage 相对路径)
    rundll32 netplwiz.dll,UsersRunDll
    关于锁
    我的回帖保存
  • 原文地址:https://www.cnblogs.com/tanghuian/p/4549118.html
Copyright © 2020-2023  润新知