问题
I am trying to create a list view as follows:-

Now, I have made the list view work successfully using the WearableListView adapter. However, I am using static images in the ImageView. What I want is to create this gray and blue kind of animation when a particular list element is focused while scrolling. How do I do that? I tried using a selector xml file for the ImageView but it seems this ListView doesnt make use of this selector(android:state-focused, selected, pressed-nothing works). Any idea how do I get what I want? Hope I made my question clear. Thanks.
回答1:
Yes you are right, image selector doesn't work, why? I don't know exactly and we don't have any documentation on it to read.
But fortunately I have implemented this scenario successfully and it's almost similar to the default settings screen.
For workaround, I have set circle color inside below methods:
onScaleUpStart()
- Set circle color for selected itemonScaleDownStart()
- Set circle color for non-selected items.
I have taken CircleImageView and TextView inside my item layout. Example code could be:
private final class MyItemView extends FrameLayout implements WearableListView.Item {
final CircledImageView image;
final TextView text;
private float mScale;
private final int mFadedCircleColor;
private final int mChosenCircleColor;
public MyItemView(Context context) {
super(context);
View.inflate(context, R.layout.wearablelistview_item, this);
image = (CircledImageView) findViewById(R.id.image);
text = (TextView) findViewById(R.id.text);
mFadedCircleColor = getResources().getColor(android.R.color.darker_gray);
mChosenCircleColor = getResources().getColor(android.R.color.holo_blue_dark);
}
@Override
public float getProximityMinValue() {
return mDefaultCircleRadius;
}
@Override
public float getProximityMaxValue() {
return mSelectedCircleRadius;
}
@Override
public float getCurrentProximityValue() {
return mScale;
}
@Override
public void setScalingAnimatorValue(float value) {
mScale = value;
image.setCircleRadius(mScale);
image.setCircleRadiusPressed(mScale);
}
@Override
public void onScaleUpStart() {
image.setAlpha(1f);
text.setAlpha(1f);
image.setCircleColor(mChosenCircleColor);
}
@Override
public void onScaleDownStart() {
image.setAlpha(0.5f);
text.setAlpha(0.5f);
image.setCircleColor(mFadedCircleColor);
}
}
回答2:
New update brings WearableListView.OnCenterProximityListener alternative of WearableListView.Item to implement Wearable list view with selector,
It has two methods to implement:
onCenterPosition
onNonCenterPosition
public class WearableListItemLayout extends LinearLayout implements WearableListView.OnCenterProximityListener { private CircledImageView image; private TextView text; public WearableListItemLayout(Context context) { this(context, null); } public WearableListItemLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public WearableListItemLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onFinishInflate() { super.onFinishInflate(); image = (CircledImageView) findViewById(R.id.image); text = (TextView) findViewById(R.id.name); } @Override public void onCenterPosition(boolean b) { image.setAlpha(1f); image.setBackgroundResource(R.drawable.blue_oval); text.setAlpha(1f); } @Override public void onNonCenterPosition(boolean b) { image.setAlpha(0.5f); image.setBackgroundResource(R.drawable.gray_oval); text.setAlpha(0.5f); } }
来源:https://stackoverflow.com/questions/25106866/android-wear-wearablelistview-imageview-selector