Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android ImageView选择标记_Java_Android_Android Imageview - Fatal编程技术网

Java Android ImageView选择标记

Java Android ImageView选择标记,java,android,android-imageview,Java,Android,Android Imageview,我的应用程序中有10个图像。用户必须选择其中一个。我希望它是勾号在右上角的点击图像,而不是在其他。如何操作?您可以使用FrameLayout和ImageView并管理每个图像的选定状态。如果选择图像,则使imgCheck可见,否则将其隐藏 <FrameLayout android:layout_width="80dp" android:layout_height="80dp"> <a

我的应用程序中有10个图像。用户必须选择其中一个。我希望它是勾号在右上角的点击图像,而不是在其他。如何操作?

您可以使用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>


希望这会有帮助

您可以使用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>


希望这会有帮助

您可以将图像放入回收视图,并使用以下适配器保持选定图像的状态

图像适配器

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());
                }
            });
        }
    }
}
公共类ImageAdapter扩展了RecyclerView.Adapter{
私人语境;
private int selectedPos=-1;
公共ImageAdapter(上下文){
this.context=上下文;
}
公共无效设置选定位置(内部位置){
selectedPos=pos;
notifyDataSetChanged();
}
@非空
@凌驾
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType){
返回新的视图持有者(
LayoutInflater.from(parent.getContext())
.充气(右布局.项目\图像,父项,假)
);
}
@凌驾
public void onBindViewHolder(@NonNull ViewHolder,int位置){
如果(位置==所选位置){
支架.ivCheck.setVisibility(视图.可见);
}否则{
holder.ivCheck.setVisibility(视图.消失);
}
}
@凌驾
public int getItemCount(){
返回10;
}
公共类ViewHolder扩展了RecyclerView.ViewHolder{
私人影像检查;
私有图像查看图像;
公共视图持有者(@NonNull View itemView){
超级(项目视图);
image=itemView.findviewbyd(R.id.iv_src);
ivCheck=itemView.findViewById(R.id.iv_检查);
image.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
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>

您可以将图像放入回收视图,并使用以下适配器保持选定图像的状态

图像适配器

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());
                }
            });
        }
    }
}
公共类ImageAdapter扩展了RecyclerView.Adapter{
私人语境;
private int selectedPos=-1;
公共ImageAdapter(上下文){
this.context=上下文;
}
公共无效设置选定位置(内部位置){
selectedPos=pos;
notifyDataSetChanged();
}
@非空
@凌驾
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType){
返回新的视图持有者(
LayoutInflater.from(parent.getContext())
.充气(右布局.项目\图像,父项,假)
);
}
@凌驾
public void onBindViewHolder(@NonNull ViewHolder,int位置){
如果(位置==所选位置){
支架.ivCheck.setVisibility(视图.可见);
}否则{
holder.ivCheck.setVisibility(视图.消失);
}
}
@凌驾
public int getItemCount(){
返回10;
}
公共类ViewHolder扩展了RecyclerView.ViewHolder{
私人影像检查;
私有图像查看图像;
公共视图持有者(@NonNull View itemView){
超级(项目视图);
image=itemView.findviewbyd(R.id.iv_src);
ivCheck=itemView.findViewById(R.id.iv_检查);
image.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
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>


分享你想逐一检查的代码。这听起来很可笑。所以我什么都没试过:(请在您的浏览器上搜索,您肯定可以从那里获得解决方案。您只需思考、查找并尝试实施,如果遇到任何问题,请在此处发布您的问题。我这样问是因为找不到任何答案。@AliShare您尝试过的代码我想逐一检查。这听起来很荒谬。因此我没有尝试任何东西:(请在您的浏览器上搜索,您肯定可以从那里获得解决方案。您只需思考、查找并尝试实施,如果发现任何问题,请在此处发布您的问题。我之所以这样问,是因为我找不到任何答案。@Ali)