如何以编程方式将ToggleButton添加到特定的listView

时间:2013-08-16 07:20:04

标签: android android-listview android-arrayadapter togglebutton

我知道如何将ToggleButton添加到所有listView行。但是,如何将ToggleButton添加到一个特定的listView行?

我用listAdapter填充我的listView,如此

ListView mlistView = (ListView) findViewById(R.id.listViewSetting);
mlistView.setAdapter(new ArrayAdapter<String>(this,
          android.R.layout.simple_list_item_1,
          new String[] {"Rate @ME App", "Feedback", "Block", "Terms of Service", "Push Notifications", "Sign Out"}));

1 个答案:

答案 0 :(得分:2)

  1. 创建包含togglebutton
  2. 的布局文件
  3. 通过扩展适配器来创建customAdapter,例如ArrayAdapter
  4. 覆盖getView()方法
  5. 实施viewHolder模式以提高性能
  6. 向getView()方法添加逻辑以处理togglebutton
  7. 这是一些伪代码

        @Override
        public View getView(final int position, final View convertView, final ViewGroup parent) {
             // Do inflation here, use a viewholder pattern to improve performance
    
             // Add logic to handle togglebutton, view being togglebutton
             if(items.get(position).equals(something){
                view.setVisibilty(View.Visible);
             }else{
                 view.setVisibilty(View.GONE);
             }
         }