我使用drawable作为图像背景来提供边框,但它不能按要求工作(圆圈并不完美)。
Recycler视图项目布局
<data>
<variable
name="news"
type="com.android.mvvm.model.NewsItem" />
<variable
name="clickListener"
type="com.android.common.util.RecyclerViewOnItemClickHandler"></variable>
</data>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/circular_image_selected"
android:layout_width="100dp"
android:layout_height="100dp"
android:onClick="@{clickListener.onClick}"
android:src="@{news.thumb}" />
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/circular_image_unselected"
android:layout_width="100dp"
android:layout_height="100dp"
android:onClick="@{clickListener.onClick}"
android:src="@{news.thumb}"
android:visibility="gone" />
</FrameLayout>
适配器的onBindViewHolder方法,我将背景设置为所选项目的图像视图
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof RecyclerViewHolder) {
NewsItem item = list.get(position);
RecyclerViewHolder newsViewHolder = (RecyclerViewHolder) holder;
if (item.isSelected) {
CircleImageView img = (CircleImageView) newsViewHolder.getBinding().getRoot().findViewById(R.id.circular_image_unselected);
newsViewHolder.getBinding().getRoot().findViewById(R.id.circular_image_unselected).setVisibility(View.VISIBLE);
img.setBackground(context.getResources().getDrawable(R.drawable.selected_img_bg));
} else {
CircleImageView img = (CircleImageView) newsViewHolder.getBinding().getRoot().findViewById(R.id.circular_image_selected);
img.setColorFilter(Color.argb(150, 155, 155, 155), PorterDuff.Mode.SRC_ATOP);
newsViewHolder.getBinding().getRoot().findViewById(R.id.circular_image_unselected).setVisibility(View.INVISIBLE);
}
newsViewHolder.getBinding().setVariable(BR.clickListener,
new RecyclerViewOnItemClickHandler<>(item, position, listener));
newsViewHolder.getBinding().setVariable(BR.news, item);
newsViewHolder.getBinding().executePendingBindings();
}
}
Drawable for image background
<?xml version="1.0" encoding="utf-8"?><shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@android:color/transparent" />
<stroke android:color="@android:color/holo_red_dark" android:width="3dp"/>
<size
android:width="100dp"
android:height="100dp"/>
答案 0 :(得分:1)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:useLevel="false"
android:thickness="7.8dp"
android:innerRadius="0dp"
>
<solid
android:color="#F00"
/>
<stroke
android:width="1dip"
android:color="#FFF" />
使用此作为您的图像的背景...相应地增加厚度..或者如果这不起作用,则保留先前的背景xml并在您的imageview中添加10:15 dp的android:padding
答案 1 :(得分:0)
我通过将imageview放入relativelayout来解决这个问题
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:layout_margin="5dp"
android:padding="2dp"
android:background="@drawable/imageborder"
android:src="@drawable/placeholder"
android:id="@+id/notificator_image"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<stroke android:width="2dp"
android:color="#cc195d" />
</shape>