zoukankan      html  css  js  c++  java
  • listView 单选实现

    上一篇知道可以使用android自带的listview的chiocemode的单选模式实现。但那个布局是系统自带的checkedTextView,有时候我们需要自己实现布局,那么下面我们开始实现
     
    自定义组合布局,实现checkable接口。
    1. public class SingleView extends LinearLayout implements Checkable {  
    2.   
    3.      private TextView mText;  
    4.      private CheckBox mCheckBox;  
    5.      public SingleView(Context context, AttributeSet attrs, int defStyle) {  
    6.             super(context, attrs, defStyle);  
    7.            initView(context);  
    8.      }  
    9.   
    10.      public SingleView(Context context, AttributeSet attrs) {  
    11.             super(context, attrs);  
    12.            initView(context);  
    13.      }  
    14.   
    15.      public SingleView (Context context) {  
    16.             super(context);  
    17.            initView(context);  
    18.      }  
    19.   
    20.      private void initView(Context context){  
    21.             // 填充布局  
    22.            LayoutInflater inflater = LayoutInflater.from(context);  
    23.            View v = inflater.inflate(R.layout.item_single_layout , this, true);  
    24.             mText = (TextView) v.findViewById(R.id. title);  
    25.             mCheckBox = (CheckBox) v.findViewById(R.id. checkbox);  
    26.      }  
    27.   
    28.      @Override  
    29.      public void setChecked( boolean checked) {  
    30.             mCheckBox.setChecked(checked);  
    31.              
    32.      }  
    33.   
    34.      @Override  
    35.      public boolean isChecked() {  
    36.             return mCheckBox.isChecked();  
    37.      }  
    38.   
    39.      @Override  
    40.      public void toggle() {  
    41.             mCheckBox.toggle();  
    42.      }  
    43.        
    44.      public void setTitle(String text){  
    45.             mText.setText(text);  
    46.      }  
    47. }  
     
    activity中试下代码:
    1. public class MainActivity extends Activity {  
    2.      private ArrayList<String> groups;  
    3.      private ListView listView;  
    4.   
    5.      @Override  
    6.      protected void onCreate(Bundle savedInstanceState) {  
    7.             super.onCreate(savedInstanceState);  
    8.            setContentView(R.layout. activity_main);  
    9.   
    10.             listView = (ListView) findViewById(R.id. listView);  
    11.   
    12.             groups = new ArrayList<String>();  
    13.             groups.add( "11");  
    14.             groups.add( "22");  
    15.             groups.add( "33");  
    16.             groups.add( "44");  
    17.             groups.add( "55");  
    18.             groups.add( "66");  
    19.             groups.add( "77");  
    20.             groups.add( "88");  
    21.             groups.add( "99");  
    22.             groups.add( "00");  
    23.   
    24.            SingleAdapter singleAdapter = new SingleAdapter();  
    25.             listView.setAdapter(singleAdapter);  
    26.   
    27.            Button button = (Button) findViewById(R.id. button);  
    28.            button.setOnClickListener( new OnClickListener() {  
    29.   
    30.                  @Override  
    31.                  public void onClick(View v) {  
    32.                      PickNum();  
    33.                 }  
    34.            });  
    35.      }  
    36.   
    37.      private void PickNum() {  
    38.             int position = listView.getCheckedItemPosition();  
    39.             if (ListView. INVALID_POSITION != position) {  
    40.                 Toast. makeText(MainActivity.this, groups.get(position), 0).show();  
    41.            }  
    42.      }  
    43.   
    44.      private class SingleAdapter extends BaseAdapter {  
    45.   
    46.             @Override  
    47.             public int getCount() {  
    48.                  return groups.size();  
    49.            }  
    50.   
    51.             @Override  
    52.             public Object getItem( int position) {  
    53.                  // TODO Auto-generated method stub  
    54.                  return null;  
    55.            }  
    56.   
    57.             @Override  
    58.             public long getItemId( int position) {  
    59.                  // TODO Auto-generated method stub  
    60.                  return 0;  
    61.            }  
    62.   
    63.             @Override  
    64.             public View getView( final int position, View convertView,  
    65.                      ViewGroup parent) {  
    66.                  final SingleView singleView = new SingleView(MainActivity.this );  
    67.                 singleView.setTitle( groups.get(position));  
    68.                  return singleView;  
    69.   
    70.            }  
    71.   
    72.      }  
    73. }  

    效果:
     
    如果要替换默认checkbox的图标显示,设置checkbox的button属性即可。
     
    附件:
    http://download.csdn.net/detail/knxw0001/7961143
  • 相关阅读:
    新买的电脑桌面只有回收站该做些什么
    不安装oracle客户端也可以使用pl/sql developer
    Win7上安装Oracle数据库
    忘记oracle的sys用户密码怎么修改
    UML中类之间的关系
    JAVAEE 是什么,如何获取各种规范jar包及各种规范的jar包源码
    PL/SQL Developer使用技巧、快捷键
    Windows 7上安装Microsoft Loopback Adapter(微软环回网卡)
    超棒的30款JS类库和工具
    HTTP协议
  • 原文地址:https://www.cnblogs.com/spring87/p/4318355.html
Copyright © 2011-2022 走看看