Android ImageView选择标记

问题描述 投票:0回答:2

我的应用程序中有10张图像。用户必须选择其中之一。我希望它是单击图像右上角的刻度线,而不是其他位置。我该怎么办?

java android android-imageview
2个回答
1
投票

您可以使用FrameLayout和ImageView并管理每个图像的选定状态。如果选择了图像,则使imgCheck可见,否则将其隐藏。

<FrameLayout
                android:layout_width="80dp"
                android:layout_height="80dp">
                <androidx.appcompat.widget.AppCompatImageView
                    android:id="@+id/img1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"/>
                <androidx.appcompat.widget.AppCompatImageView
                    android:layout_width="wrap_content"
                    android:src="@drawable/ic_checked"
                    android:layout_gravity="right"
                    android:layout_height="wrap_content"/>
</FrameLayout>

希望它会有所帮助!


0
投票

您可以将图像放入recyclerview并使用以下适配器保持所选图像的状态

ImageAdapter

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ViewHolder> {

    private Context context;
    private int selectedPos = -1;

    public ImageAdapter(Context context) {
        this.context = context;
    }

    public void setSelectedPos(int pos) {
        selectedPos = pos;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(
                LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_image, parent, false)
        );
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (position == selectedPos) {
            holder.ivCheck.setVisibility(View.VISIBLE);
        } else {
            holder.ivCheck.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return 10;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView ivCheck;
        private ImageView image;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            image = itemView.findViewById(R.id.iv_src);
            ivCheck = itemView.findViewById(R.id.iv_check);

            image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    setSelectedPos(getAdapterPosition());
                }
            });
        }
    }
}

布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="8dp">

    <ImageView
        android:id="@+id/iv_src"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="@color/colorPrimary"/>

    <ImageView
        android:id="@+id/iv_check"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:src="@drawable/ic_action_tick"/>

</RelativeLayout>
© www.soinside.com 2019 - 2024. All rights reserved.