0

我有一个由自定义 CursorAdapter 提供的 ListView。ListView 行非常简单 - 左侧的 CheckBox 和两个 TextView。当“单击”复选框时,将启动操作模式,以便用户可以从列表中删除多个项目。

在努力处理复选框和 OnItemClickedListeners 并跟踪检查了哪些框时,我注意到 bindView 被无休止地调用。而且,我的意思是无休止:如果 ListView 有一个项目或 100 个项目,bindView 在片段的生命周期内每秒被调用多次。ListView 现在的行为完全符合我的要求,但我的代码或 XML 布局显然有问题。

在这个网站上搜索答案没有得到结果,所以我在这里发布我的第一个问题。

ListView 每一行的 XML 布局如下所示:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal"
     android:paddingBottom="@dimen/activity_vertical_margin" 
     android:paddingTop="@dimen/activity_vertical_margin"
     android:descendantFocusability="blocksDescendants"    
     >

    <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content"
      android:focusable="false"
      android:clickable="false"
      android:id ="@+id/chkBox" />
    <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" 
      android:text="Some example text"
      android:layout_weight=".5"
      android:id="@+id/txtExercise" />
    <TextView 
      android:id="@+id/txtDuration"
      android:layout_width="fill_parent" android:layout_height="wrap_content"
      android:text="0:00" android:gravity="right"
      android:layout_weight=".5" >

    </TextView>


     </LinearLayout>

我不确定这里要显示什么其他代码,所以我将把我的 newView 和 bindView 方法放入其中。希望这足以让有人帮助我识别问题。

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {     
    final LayoutInflater inflater = LayoutInflater.from(context);
    View v = inflater.inflate(layout, parent, false);
    Log.i(TAG, "newView=" + parent.getId());

    final ViewHolder holder = new ViewHolder();
    holder.txtExercise = (TextView) v.findViewById(R.id.txtExercise);
    holder.txtDuration = (TextView) v.findViewById(R.id.txtDuration);
    holder.check = (CheckBox) v.findViewById(R.id.chkBox);                  
    holder.check.setOnCheckedChangeListener(new OnCheckedChangeListener(){

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean checked) {
            checkedList.put((Integer) buttonView.getTag(),checked);             
        }

    });
    v.setTag(holder);       
    return v;
}


@Override
public void bindView(View v, Context arg1, Cursor c) {
    String exercise = c.getString(c.getColumnIndex("_ExcerciseName"));
    int sec = c.getInt(c.getColumnIndex(WorkoutLog.DURATION));
    String duration = returnDurationString(sec);
    Log.i(TAG, "bindview called");
    ViewHolder holder = (ViewHolder) v.getTag();

    if(holder.txtExercise !=null)
        holder.txtExercise.setText(exercise);
    if(holder.txtDuration != null)
        holder.txtDuration.setText(duration);
    if(holder.check !=null) {   
        holder.check.setTag(c.getInt(c.getColumnIndex(WorkoutLog._ID)));
        int x = (Integer) holder.check.getTag();            
        holder.check.setChecked(checkedList.get(x));            
    }
}

任何意见,将不胜感激!我对 Android 开发还是很陌生,所以我可能会犯一些完全愚蠢的错误,但我只是没有经验和/或知识来尝试解决这个问题。

[编辑] - 从我的 ListView 周围删除标签解决了这个问题!

4

1 回答 1

3

我想你有你android:layout_heightlistview设置wrap_content。尝试切换到fill_parent.

检查这个答案:为什么 Wrap_Content 会多次触发 BindView

就像是:

<ListView
        android:id="@+id/my_listView"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        //...
</ListView>

希望这有帮助!

于 2013-05-05T11:54:06.473 回答