磨损设置列表项

时间:2015-01-27 07:34:11

标签: android listview wear-os

我正在尝试为我的磨损应用复制磨损设置列表。 我想使用这样的复选框项列表。

Selected Unselected

问题是如果我想让放大动画正常工作,我必须使用CircledImageView。当列表滚动时,我无法使用复选框进行正确的动画制作。这样做的正确方法是什么? 有没有办法以一种基于checked.unchecked显示两个图像的方式使用CircledListItem?或者有没有办法使用Checkbox正确管理焦点缩放?

2 个答案:

答案 0 :(得分:4)

复制“设置”屏幕有点复杂。 - CheckBox和TextView不起作用,因为确切的行为是图标保持不变并且圆圈在焦点上扩展。我们必须使用CircledImageView。 - 我使用的解决方案是 - >使用CircledImageView和Textview并自己处理CheckBox登录。

这是列表项目布局。

<?xml version="1.0" encoding="utf-8"?>

<android.support.wearable.view.CircledImageView
    android:id="@+id/config_image"
    android:layout_width="@dimen/config_icon_item_size"
    android:layout_height="@dimen/config_icon_item_size"
    android:layout_marginLeft="@dimen/config_item_margin_leftright"
    android:layout_gravity="center_vertical"
    app:circle_border_color="@color/color_item_circle_border_color"
    app:circle_radius="@dimen/default_settings_circle_radius"
    app:circle_border_width="@dimen/config_icon_circle_border"
    app:circle_color="@android:color/transparent" />

<TextView
    android:id="@+id/config_text"
    android:layout_height="@dimen/config_icon_item_size"
    android:layout_marginLeft="@dimen/config_icon_size_plus_leftmargin"
    android:layout_marginRight="@dimen/config_item_margin_leftright"
    android:layout_width="wrap_content"
    android:fontFamily="sans-serif-condensed-light"
    android:gravity="center_vertical"
    android:textColor="@color/config_item_text"
    android:textSize="@dimen/list_item_textsize"
    android:text="@string/str_placeholder" />
/>

在代码中我必须处理动画和Checkbox状态。

private final class MyItemView extends FrameLayout implements WearableListView.OnCenterProximityListener {

    private static final int ANIMATION_DURATION_MS = 150;
    /**
     * The ratio for the size of a circle in shrink state.
     */
    private static final float SHRINK_CIRCLE_RATIO = .75f;

    private static final float SHRINK_LABEL_ALPHA = .5f;
    private static final float EXPAND_LABEL_ALPHA = 1f;

    private final TextView mTextView;
    private final CircledImageView mImage;

    private final float mExpandCircleRadius;
    private final float mShrinkCircleRadius;

    private final ObjectAnimator mExpandCircleAnimator;
    private final ObjectAnimator mExpandIconAnimator;
    private final ObjectAnimator mExpandLabelAnimator;
    private final AnimatorSet mExpandAnimator;

    private final ObjectAnimator mShrinkCircleAnimator;
    private final ObjectAnimator mShrinkIconAnimator;
    private final ObjectAnimator mShrinkLabelAnimator;
    private final AnimatorSet mShrinkAnimator;
    public ItemResource mResources;

    public MyItemView(Context context, ItemResource resources) {
        super(context);
        View.inflate(context, R.layout.config_listitem, this);
        mResources = resources;
        mTextView = (TextView) findViewById(R.id.config_text);


        mImage = (CircledImageView) findViewById(R.id.config_image);
        mImage.setCircleBorderColor(Color.WHITE);
        mExpandCircleRadius = mImage.getCircleRadius();
        mShrinkCircleRadius = mExpandCircleRadius * SHRINK_CIRCLE_RATIO;

        mShrinkCircleAnimator = ObjectAnimator.ofFloat(mImage, "circleRadius",
                mExpandCircleRadius, mShrinkCircleRadius);
        mShrinkIconAnimator = ObjectAnimator.ofFloat(mImage, "alpha",
                EXPAND_LABEL_ALPHA, SHRINK_LABEL_ALPHA);
        mShrinkLabelAnimator = ObjectAnimator.ofFloat(mTextView, "alpha",
                EXPAND_LABEL_ALPHA, SHRINK_LABEL_ALPHA);
        mShrinkAnimator = new AnimatorSet().setDuration(ANIMATION_DURATION_MS);
        mShrinkAnimator.playTogether(mShrinkCircleAnimator, mShrinkLabelAnimator, mShrinkIconAnimator);

        mExpandCircleAnimator = ObjectAnimator.ofFloat(mImage, "circleRadius",
                mShrinkCircleRadius, mExpandCircleRadius);
        mExpandLabelAnimator = ObjectAnimator.ofFloat(mTextView, "alpha",
                SHRINK_LABEL_ALPHA, EXPAND_LABEL_ALPHA);
        mExpandIconAnimator = ObjectAnimator.ofFloat(mImage, "alpha",
                SHRINK_LABEL_ALPHA, EXPAND_LABEL_ALPHA);
        mExpandAnimator = new AnimatorSet().setDuration(ANIMATION_DURATION_MS);
        mExpandAnimator.playTogether(mExpandCircleAnimator, mExpandLabelAnimator, mExpandIconAnimator);
    }


    @Override
    public void onCenterPosition(boolean animate) {
        if (animate) {
            mShrinkAnimator.cancel();
            if (!mExpandAnimator.isRunning()) {
                mExpandCircleAnimator.setFloatValues(mImage.getCircleRadius(), mExpandCircleRadius);
                mExpandLabelAnimator.setFloatValues(mTextView.getAlpha(), EXPAND_LABEL_ALPHA);
                mExpandIconAnimator.setFloatValues(mImage.getAlpha(), EXPAND_LABEL_ALPHA);
                mExpandAnimator.start();
            }
            Log.i("OnCenter+animate:",this.mResources.mSelectedTitle);
        } else {
            mExpandAnimator.cancel();
            mImage.setCircleRadius(mExpandCircleRadius);
            mTextView.setAlpha(EXPAND_LABEL_ALPHA);
            mImage.setAlpha(EXPAND_LABEL_ALPHA);
            Log.i("OnCenter:",this.mResources.mSelectedTitle);
        }
    }

    @Override
    public void onNonCenterPosition(boolean animate) {
        if (animate) {
            mExpandAnimator.cancel();
            if (!mShrinkAnimator.isRunning()) {
                mShrinkCircleAnimator.setFloatValues(mImage.getCircleRadius(), mShrinkCircleRadius);
                mShrinkLabelAnimator.setFloatValues(mTextView.getAlpha(), SHRINK_LABEL_ALPHA);
                mShrinkLabelAnimator.setFloatValues(mImage.getAlpha(), SHRINK_LABEL_ALPHA);
                mShrinkAnimator.start();
                Log.i("nonCenter+animate:",this.mResources.mSelectedTitle);
            }
        } else {
            mShrinkAnimator.cancel();
            mImage.setCircleRadius(mShrinkCircleRadius);
            mTextView.setAlpha(SHRINK_LABEL_ALPHA);
            mImage.setAlpha(SHRINK_LABEL_ALPHA);
            Log.i("nonCenter:",this.mResources.mSelectedTitle);
        }
    }

    public boolean isChecked() {
        return mResources.mCheck;
    }

    public void toggle() {
        mResources.mCheck = !mResources.mCheck;
    }
}

在我的适配器(我们在这里使用RecyclerView)中,我们会在每次点击时切换。

@Override
public void onClick(WearableListView.ViewHolder viewHolder) {
    MyItemView itemView = (MyItemView) viewHolder.itemView;
    itemView.toggle();
    //do your stuff
}

答案 1 :(得分:1)

创建自己的列表项类,实现WearableListView.OnCenterProximityListener界面,并在onCenterPositiononNonCenterPosition方法中应用任何动画:

mCheckBox.animate().scaleX(1.6f).scaleY(1.6f).setDuration(ANIMATION_DURATION);

您可以在samples\android-21\wearable\Notifications中看到使用此技术的示例。

编辑: 我认为实现这种行为的最简单方法是将CheckBox放在RelativeLayout中,如下所示:

<RelativeLayout
    android:layout_width="80dp"
    android:layout_height="80dp">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_centerInParent="true"
        android:src="@drawable/circle"/>

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

</RelativeLayout>

并仅将缩放动画应用于ImageView。