Анимированный фон в списке

У меня есть ListView, в каждой строке которого есть 2 TextView и CheckBox.

У меня есть рисуемый элемент (линия), который я хочу, чтобы после того, как пользователь проверит CheckBox, рисунок будет масштабироваться как анимация на фоне отмеченной строки CheckBox, я смог в своем getView() в моем пользовательском адаптере, чтобы анимировать рисуемый как фон, но он также масштабирует 2 TextView и CheckBox, и я хочу, чтобы только рисуемый фон был масштабирован как анимация, как мне это сделать?

вот мой метод 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;     
}

person Shimon Geld    schedule 31.12.2012    source источник
comment
код может дать лучшее представление.   -  person Paresh Mayani    schedule 31.12.2012
comment
пожалуйста, поделитесь своим кодом, а не просто теорией   -  person swiftBoy    schedule 31.12.2012
comment
java-samples.com/showtutorial.php?tutorialid=1515 читать этот учебник с масштабной анимацией в Listview может быть вам полезен   -  person DynamicMind    schedule 31.12.2012
comment
как мне это сделать? - Не устанавливайте рисуемый объект в качестве фона всего представления строки, вместо этого используйте его в качестве фона пустого представления и масштабируйте только это представление.   -  person user    schedule 31.12.2012
comment
Я понимаю, что вы говорите, Luksprog, но как я добавляю пустое представление в строку в списке?   -  person Shimon Geld    schedule 31.12.2012
comment
Это действительно зависит от того, как выглядит ваш вид строки, где находится CheckBox и т. д.   -  person user    schedule 31.12.2012
comment
Можете ли вы дать мне пример или порекомендовать мне один, чтобы я мог понять, как это сделать на практике?   -  person Shimon Geld    schedule 31.12.2012
comment
Ludsprog, спасибо за идею!!!   -  person Shimon Geld    schedule 01.01.2013


Ответы (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 перекрывает 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;
}

надеюсь, вы понимаете, что я сделал.

person Shimon Geld    schedule 31.12.2012