具有复杂行项的ListActivity:如何访问行视图?

时间:2013-11-22 11:41:40

标签: android android-ui

我很确定这是可能的,但奇怪的是找不到怎么样。 所以,我有ListActivity。我希望这是它的行元素:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <CheckBox
        android:id="@+id/checkbox"
        style="@style/CheckBox" />

    <TextView
        android:id="@+id/text"
        style="@style/Label"
        android:layout_width="fill_parent"
        android:layout_height="50sp"
        android:gravity="center_vertical" />

</LinearLayout>

我已经可以使用此布局了,我知道在创建TextView时如何指定ArrayAdapter的ID。布局很好,我可以勾选复选框。

问题是,我不明白如何访问这些复选框。我需要一个膨胀的行布局来调用findViewById,但它是异步膨胀的,而不是在我向适配器添加项目时立即膨胀。怎么做?

1 个答案:

答案 0 :(得分:2)

  

选中CheckBox后,我需要取消选中所有其他复选框   存储已选中的索引

这是我现在的解决方案。我现在没有你的实现(适配器的实现,数据源等)所以你可能需要一些修改。所以现在我的解决方案:

您需要onCheckedChangedListener并输入 ListAdapter 类:

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
   // do your stuff
}

现在我不知道你的实际场景,但很可能你有一些你正在传递给Adapter的对象集合,你在 getView()方法中使用它返回行的视图。

在这里我建议修改你的Object(表示适配器中每一行的项目)并添加属性:

private boolean isChecked;

将“代表”您的CheckBox

现在隐含地,每个 CheckBox 都将被取消选中(因为布尔值被隐式指定为false,所以没关系)。

现在直接进入Adapter类。在您的课程中,您需要完成 getView()

public View getView(final int position, View convertView, ViewGroup parent) {
   RowHolder holder = null; // RowHolder that holds widgets for each row
   LayoutInflater inflater = LayoutInflater.from(context); // for inflate rows

   // logic for inflating rows, pretty simply
   if (convertView == null) {
      // inflate row
      convertView = inflater.inflate(<yourLayout>, null, false);
      holder = new RowHolder(convertView);
      convertView.setTag(holder);
   }
   else {
      // recycle row
      holder = (RowHolder) convertView.getTag();
   }

   // inicialising row values
   final Item i = collection.get(position); // item from collection

   /** here will be work with CheckBoxes **/

   // here you will set position as CheckBox's tag
   holder.getCheckBox().setTag(position);

   // set CheckBox checked or not due to item in collection
   holder.getCheckBox().setChecked(item.isChecked());

   // and assign listener to CheckBox
   holder.getCheckBox().setOnCheckedChangeListener(this);
}

这是非常重要的逻辑。在这里,您将Adapter中 CheckBox 的位置保存到CheckBox本身。现在CheckBox知道“在哪里”

然后你的听众会做出以下的事情:

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
   // buttonView is CheckBox you changed state so we know which CheckBox it is
   int position = (Integer) buttonView.getTag();

   // if CheckBox is checked
   if (isChecked) {

      // iterate collection and assign all items except selected into false
      for (int i = 0; i < collection.size(); i++) {
         if (i != position) {
            collection.get(i).setChecked(false);
         }
      }

      // now call notifyDataSetChanged() that will call getView() method again
      // and it will update your List
      notifyDataSetChanged();
   }  
}

这里的听众会耍手段。

还有一些东西

我希望它能帮助你实现目标。