按下的选择器状态似乎不起作用
Selector state pressed doesn't seem to be working
我有一个 RecyclerView,它有一个 ImageView,因为它是 ViewHolder。我想在我点击的 ImageView 周围添加一个白色边框。由于所有这些状态都非常混乱(按下、选择、聚焦),所有这些状态都可能意味着同一件事,我想问这个具体案例..
这是适配器的 ViewHolder xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_margin="10dp"
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.myproject.RoundRectCornerImageView
android:id="@+id/imageHolder"
android:layout_width="56dp"
android:layout_height="56dp"
android:background="@drawable/image_details_selector"
android:clickable="true"
android:scaleType="centerCrop"
android:src="@drawable/thumbnail"
android:focusable="true" />
这是我正在使用的选择器:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:state_selected="true">
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<stroke android:width="4dp" android:color="#fff" />
<padding android:bottom="1dp" android:left="1dp" android:right="1dp" android:top="1dp" />
</shape>
</item>
<item android:state_pressed="false" android:state_selected="false">
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<stroke android:width="1dp" android:color="@android:color/transparent" />
<padding android:bottom="1dp" android:left="1dp" android:right="1dp" android:top="1dp" />
</shape>
</item>
所以我想在这里做的是,在水平 RecyclerView 中有一个图像列表,如果我单击其中一个图像,它将在其周围出现这个白色边框,所有其他 ImageView 不会。我实现了 onClick 侦听器,它在后台加载图像并且它正在工作,但我无法让这个选择器按我想要的方式工作。有什么想法吗?
您在那个场景中添加了 android:state_selected="true"
它正在检查条件按下效果和状态。从选择器中删除状态。希望这对你有用。
使用下面的选择器
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<stroke android:width="4dp" android:color="#fff" />
<padding android:bottom="1dp" android:left="1dp" android:right="1dp" android:top="1dp" />
</shape>
</item>
<item android:state_pressed="false">
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<stroke android:width="1dp" android:color="@android:color/transparent" />
<padding android:bottom="1dp" android:left="1dp" android:right="1dp" android:top="1dp" />
</shape>
</item>
请参考这段代码:
'<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<stroke android:width="4dp" android:color="#fff" />
<padding android:bottom="1dp" android:left="1dp" android:right="1dp" android:top="1dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<stroke android:width="1dp" android:color="@android:color/transparent" />
<padding android:bottom="1dp" android:left="1dp" android:right="1dp" android:top="1dp" />
</shape>
</item>
请检查您在上面的代码中所做的与此进行比较。
首先,您必须像这样设置您的选择器:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<stroke android:width="4dp" android:color="#fff" />
<padding android:bottom="1dp" android:left="1dp" android:right="1dp" android:top="1dp" />
</shape>
</item>
<item android:state_selected="false">
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<stroke android:width="1dp" android:color="@android:color/transparent" />
<padding android:bottom="1dp" android:left="1dp" android:right="1dp" android:top="1dp" />
</shape>
</item>
在这种情况下,只有选中的才有效。然后您必须在适配器的 getView()
方法中手动设置选择,如下所示:
ImageView iv = (ImageView) findViewById(R.id.image_view);
iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(lastImageView != null)
lastImageView.setSelected(false);
lastImageView = (ImageView) view;
lastImageView.setSelected(true);
}
});
并在适配器上将其设置为全局
private ImageView lastImageView = null;
一旦检测到点击,您必须手动设置 ImageView 的状态。
RecyclerView 不为其子项存储状态。
我有一个 RecyclerView,它有一个 ImageView,因为它是 ViewHolder。我想在我点击的 ImageView 周围添加一个白色边框。由于所有这些状态都非常混乱(按下、选择、聚焦),所有这些状态都可能意味着同一件事,我想问这个具体案例..
这是适配器的 ViewHolder xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_margin="10dp"
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.myproject.RoundRectCornerImageView
android:id="@+id/imageHolder"
android:layout_width="56dp"
android:layout_height="56dp"
android:background="@drawable/image_details_selector"
android:clickable="true"
android:scaleType="centerCrop"
android:src="@drawable/thumbnail"
android:focusable="true" />
这是我正在使用的选择器:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:state_selected="true">
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<stroke android:width="4dp" android:color="#fff" />
<padding android:bottom="1dp" android:left="1dp" android:right="1dp" android:top="1dp" />
</shape>
</item>
<item android:state_pressed="false" android:state_selected="false">
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<stroke android:width="1dp" android:color="@android:color/transparent" />
<padding android:bottom="1dp" android:left="1dp" android:right="1dp" android:top="1dp" />
</shape>
</item>
所以我想在这里做的是,在水平 RecyclerView 中有一个图像列表,如果我单击其中一个图像,它将在其周围出现这个白色边框,所有其他 ImageView 不会。我实现了 onClick 侦听器,它在后台加载图像并且它正在工作,但我无法让这个选择器按我想要的方式工作。有什么想法吗?
您在那个场景中添加了 android:state_selected="true"
它正在检查条件按下效果和状态。从选择器中删除状态。希望这对你有用。
使用下面的选择器
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<stroke android:width="4dp" android:color="#fff" />
<padding android:bottom="1dp" android:left="1dp" android:right="1dp" android:top="1dp" />
</shape>
</item>
<item android:state_pressed="false">
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<stroke android:width="1dp" android:color="@android:color/transparent" />
<padding android:bottom="1dp" android:left="1dp" android:right="1dp" android:top="1dp" />
</shape>
</item>
请参考这段代码:
'<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<stroke android:width="4dp" android:color="#fff" />
<padding android:bottom="1dp" android:left="1dp" android:right="1dp" android:top="1dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<stroke android:width="1dp" android:color="@android:color/transparent" />
<padding android:bottom="1dp" android:left="1dp" android:right="1dp" android:top="1dp" />
</shape>
</item>
请检查您在上面的代码中所做的与此进行比较。
首先,您必须像这样设置您的选择器:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<stroke android:width="4dp" android:color="#fff" />
<padding android:bottom="1dp" android:left="1dp" android:right="1dp" android:top="1dp" />
</shape>
</item>
<item android:state_selected="false">
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<stroke android:width="1dp" android:color="@android:color/transparent" />
<padding android:bottom="1dp" android:left="1dp" android:right="1dp" android:top="1dp" />
</shape>
</item>
在这种情况下,只有选中的才有效。然后您必须在适配器的 getView()
方法中手动设置选择,如下所示:
ImageView iv = (ImageView) findViewById(R.id.image_view);
iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(lastImageView != null)
lastImageView.setSelected(false);
lastImageView = (ImageView) view;
lastImageView.setSelected(true);
}
});
并在适配器上将其设置为全局
private ImageView lastImageView = null;
一旦检测到点击,您必须手动设置 ImageView 的状态。 RecyclerView 不为其子项存储状态。