简体   繁体   中英

Check which image is attached to imageView

I have shown a list of apps installed on device in recyclerView( Image Attached ), In this, I categorized apps with imageview( you can see red circle in image )

I set both icons in coding, based on condition, Now I want to show toast onclick of item based on imageView ie appCategory , means...If item have imageView of phone, it shows toast "phone" otherwise "playstore"

that previous condition, I used to filter is not applicable here as it uses FOR Loop for whole list.


image for reference - Link

I tried tag and following code but doesn't worked for me

if (Category_Apps.drawable.constantState ==
        ContextCompat.getDrawable(applicationContext, R.drawable.ic_phone)!!.constantState) 
    {
        Toast.makeText(applicationContext, "Phone", Toast.LENGTH_SHORT).show()
    } 
    else 
    {
        Toast.makeText(applicationContext, "Play-store", Toast.LENGTH_SHORT).show()
    }

I guess you should save the desired Toast's text in recycle view item itself and you can simply show it on icon click.

public class AppListAdapter extends RecyclerView.Adapter<AppListAdapter.CustomViewHolder> {

    private Context context;
    private List<AppItem> appItemList;

    ........
    public class CustomViewHolder extends RecyclerView.ViewHolder {
        ImageView icon;
        .....
        public CustomViewHolder(@NonNull View itemView) {
            super(itemView);
            //other code for item setup
            icon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, appItemList.get(getAdapterPosition()).getIconText(), Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
}

Happy coding !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM