【问题标题】:Animate background on listview在列表视图上设置动画背景
【发布时间】:2012-12-31 09:46:28
【问题描述】:

我有一个 ListView,每行有 2 个 TextView 和一个 CheckBox。

我有一个可绘制对象(一条线),一旦用户检查复选框,可绘制对象将在选中的复选框行的背景上缩放为动画,我就可以在我的 getView() 我的自定义适配器上的方法,将可绘制对象设置为背景动画,但它也缩放 2 TextView 和复选框,我希望只有可绘制背景将作为动画缩放,我该怎么做?

这是我在自定义适配器上的 getView() 方法:

public View getView(final int position, View convertView, final ViewGroup parent) {
    final ViewHolder holder;

    LayoutInflater inflater = LayoutInflater.from(context);
    convertView = inflater.inflate(R.layout.listview_item_row, parent, false);
    // cache view fields into the holder
    holder = new ViewHolder();
    holder.itemTitle = (TextView) convertView.findViewById(R.id.itemTitle);
    holder.itemQuantity = (TextView) convertView.findViewById(R.id.itemQuantity);
    holder.itemCheck = (CheckBox) convertView.findViewById(R.id.itemCheck);
    convertView.setTag(holder);

    final Cursor cursor = getCursor();
    final View rowView = convertView;
    cursor.moveToPosition(position);
    holder.itemTitle.setText(cursor.getString((cursor.getColumnIndex(MySQLiteHelper.KEY_NAME))));
    holder.itemQuantity.setText(cursor.getString((cursor.getColumnIndex(MySQLiteHelper.KEY_QUANTITY))));
    holder.itemCheck.setChecked(cursor.getInt(cursor.getColumnIndex(MySQLiteHelper.KEY_CHECKED)) == 1);

    holder.itemCheck.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    cursor.moveToPosition(position);
                    itemsDataSource.updateItemCheckBox(1, cursor.getLong(cursor.getColumnIndex(MySQLiteHelper.KEY_ID)));
                    itemsDataSource.updateRoute(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.KEY_NAME)), true);
                    Animation drawLine = AnimationUtils.loadAnimation(context, R.anim.item_done);
                    rowView.setBackgroundResource(R.drawable.line);
                    rowView.startAnimation(drawLine);
                    ShowListActivity.PRIORITY++;
                }
                else {
                    cursor.moveToPosition(position);
                    itemsDataSource.updateItemCheckBox(0, cursor.getLong(cursor.getColumnIndex(MySQLiteHelper.KEY_ID)));
                    itemsDataSource.updateRoute(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.KEY_NAME)), false);
                    rowView.setBackgroundResource(0);
                    ShowListActivity.PRIORITY--;
                }

            }
        });
    if (holder.itemCheck.isChecked()) rowView.setBackgroundResource(R.drawable.line);
    else rowView.setBackgroundResource(0);
    return rowView;
}
private static class ViewHolder {
    TextView itemTitle;
    TextView itemQuantity;
    CheckBox itemCheck;     
}

【问题讨论】:

  • 代码可以提供更好的想法。
  • 请分享你的代码,而不是只是一个理论
  • java-samples.com/showtutorial.php?tutorialid=1515 在 Listview 中使用缩放动画阅读本教程可能对您有所帮助
  • 我该怎么做? - 不要将可绘制对象设置为整个行视图的背景,而是将其用作空视图和缩放的背景仅限该视图。
  • 我明白你在说 Luksprog,但我如何将空视图添加到列表视图的行中?

标签: android android-listview android-animation android-cursoradapter android-drawable


【解决方案1】:

更新

我设法解决了这个问题:

首先我像这样更改了自定义行布局文件:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frameRowLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:weightSum="10">

<LinearLayout android:id="@+id/rowLayout" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" >

        <CheckBox android:id="@+id/itemCheck" 
            android:layout_width="0dp" 
            android:layout_height="wrap_content" 
            android:layout_marginBottom="5dp" 
            android:layout_marginTop="5dp" 
            android:layout_weight="2" android:gravity="center" 
            android:focusable="false" 
            android:focusableInTouchMode="false" 
            android:singleLine="false" />

       <TextView android:id="@+id/itemTitle" 
           android:layout_width="0dp" 
           android:layout_height="fill_parent" 
           android:layout_marginBottom="5dp" 
           android:layout_marginTop="5dp" 
           android:layout_weight="6" 
           android:gravity="center" 
           android:text="some text1" 
           android:textColor="#2B89A8" 
           android:textSize="22dp" 
           android:textStyle="bold" />

       <TextView android:id="@+id/itemQuantity" 
           android:layout_width="0dp" 
           android:layout_height="fill_parent" 
           android:layout_marginBottom="5dp" 
           android:layout_marginTop="5dp" 
           android:layout_weight="2" 
           android:gravity="center" 
           android:text="some text2" 
           android:textColor="#2B89A8" 
           android:textSize="22dp" 
           android:textStyle="bold" />

</LinearLayout>

<View
    android:id="@+id/backgroundView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

</FrameLayout>

Framelayout 的整体思想是视图可以相互重叠,LinearLayout 之后的 View 与 LinearLayout 重叠。

然后我将自定义适配器中的 getView() 方法更改为如下:

public View getView(final int position, View convertView, final ViewGroup parent) {
    final ViewHolder holder;

    LayoutInflater inflater = LayoutInflater.from(context);
    convertView = inflater.inflate(R.layout.listview_item_row, parent, false);
    // cache view fields into the holder
    holder = new ViewHolder();
    holder.itemTitle = (TextView) convertView.findViewById(R.id.itemTitle);
    holder.itemQuantity = (TextView) convertView.findViewById(R.id.itemQuantity);
    holder.itemCheck = (CheckBox) convertView.findViewById(R.id.itemCheck);
    holder.rowView = (View) convertView.findViewById(R.id.backgroundView);
    convertView.setTag(holder);

    final Cursor cursor = getCursor();
    cursor.moveToPosition(position);
    holder.itemTitle.setText(cursor.getString((cursor.getColumnIndex(MySQLiteHelper.KEY_NAME))));
    holder.itemQuantity.setText(cursor.getString((cursor.getColumnIndex(MySQLiteHelper.KEY_QUANTITY))));
    holder.itemCheck.setChecked(cursor.getInt(cursor.getColumnIndex(MySQLiteHelper.KEY_CHECKED)) == 1);

    holder.itemCheck.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    cursor.moveToPosition(position);
                    itemsDataSource.updateItemCheckBox(1, cursor.getLong(cursor.getColumnIndex(MySQLiteHelper.KEY_ID)));
                    itemsDataSource.updateRoute(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.KEY_NAME)), true);
                    Animation drawLine = AnimationUtils.loadAnimation(context, R.anim.item_done);
                    holder.rowView.setBackgroundResource(R.drawable.line);
                    holder.rowView.startAnimation(drawLine);
                    ShowListActivity.PRIORITY++;
                }
                else {
                    cursor.moveToPosition(position);
                    itemsDataSource.updateItemCheckBox(0, cursor.getLong(cursor.getColumnIndex(MySQLiteHelper.KEY_ID)));
                    itemsDataSource.updateRoute(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.KEY_NAME)), false);
                    holder.rowView.setBackgroundResource(0);
                    ShowListActivity.PRIORITY--;
                }

            }
        });
    if (holder.itemCheck.isChecked()) holder.rowView.setBackgroundResource(R.drawable.line);
    else holder.rowView.setBackgroundResource(0);
    return convertView;
}
private static class ViewHolder {
    TextView itemTitle;
    TextView itemQuantity;
    CheckBox itemCheck; 
    View rowView;
}

希望你能理解我的所作所为。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-17
    • 2011-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-23
    • 2014-12-14
    • 1970-01-01
    相关资源
    最近更新 更多