0

I want to make recyclerview first item as imageview with add image logo and after click it choose an image from gallery and set as background to the image view and the background to the second item be add an image logo

enter image description here

Sumit Shukla
  • 4,116
  • 5
  • 38
  • 57

4 Answers4

0

This can be done in this way

  1. Create ArrayList with a custom object that contains the image URL
  2. Add only one object on the ArrayList
  3. Set the adapter to show from ArrayList
  4. In the recycler view's bind view holder, if url not exist then show addimageLogo
  5. Then on the click on the image add an image(Path/URL) on the object at the position
  6. Once an image is added on the object, then check if the last object has the URL then again point 2
  7. notifydataSetChanged()

Custom Object

public class URLContainer{

    public URLContainer(String url, String imageName) {
        this.url = url;
        this.imageName = imageName;
    }

    String url;
    String imageName;// you can use other  required properties if you want
}

ArrayList

private ArrayList<URLContainer> images = new ArrayList<>();

For Adding Single Object

private void addSingleContainer(){
        list.add(new URLContainer("",""));
}
Lakhwinder Singh
  • 6,799
  • 4
  • 25
  • 42
0

You need to set by default the add Image Logo to the ImageView.Inside the adapter add an OnClickListener() to the ImageView and write the code to open the Gallery for getting the Images, this will help you.

Vikas
  • 136
  • 6
0
  1. Add a viewType variable to your data class. In your case you could use 'Button' and 'Image'.
  2. Set the first object's viewType value as 'Button'
  3. Use Multiple ViewHolder method to implement Recyclerview

Override getItemViewType

override fun getItemViewType(position: Int): Int {
        return when (orders[position].viewType) {
            ViewType.Button-> 1
            ViewType.Image-> 2
            else -> 1
        }
    }

onCreateViewHolder

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
            val viewHolder: RecyclerView.ViewHolder
            when (viewType) {
                1 -> {
                    val buttonBinding = DataBindingUtil.inflate<ItemButtonBinding>(
                        LayoutInflater.from(parent.context),
                        R.layout.item_button, parent, false
                    )
                    viewHolder = ButtonViewHolder(buttonBinding .root)
                }
                2 -> {
                    val imageBinding = DataBindingUtil.inflate<ItemImageBinding>(
                        LayoutInflater.from(parent.context),
                        R.layout.item_image, parent, false)
                    viewHolder = ImageViewHolder(imageBinding .root)
                }
                else -> {
                    val imageBinding = DataBindingUtil.inflate<ItemImageBinding>(
                        LayoutInflater.from(parent.context),
                        R.layout.item_image, parent, false)
                    viewHolder = ImageViewHolder(imageBinding .root)
                }
            }
            return viewHolder
      }

onBindViewHolder

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

            when (holder.itemViewType) {
                1 -> {
                    val buttonViewHolder = holder as ButtonViewHolder
                    configureButtonViewHolder(buttonViewHolder , position)
                }
                2 -> {
                    val imageViewHolder = holder as ImageViewHolder
                    configureImageViewHolder(imageViewHolder , position)
                }
                else -> {
                    val imageViewHolder = holder as ImageViewHolder
                    configureImageViewHolder(imageViewHolder , position)
                }
            }
        }

Set OnClickListener on Button ItemView in Button ViewHolder class and place the image picker intent codes inside OnClickListener

Refer this answer

0

You can try this, It works for me

In Main Activity

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context=this;

        list=new ArrayList<>();
        list.add(String.valueOf(R.drawable.ic_baseline_add_24));

        recyclerView=findViewById(R.id.recycler);
        textView=findViewById(R.id.textView);
        button=findViewById(R.id.button);
        recyclerView.setLayoutManager(new GridLayoutManager(MainActivity.this,3));
        adaptor=new Adapter(list, getApplicationContext(), new AdapterCallback() {
            @Override
            public void onMethodCallback(String path,int position) {

                if(position==list.size()-1){
                    openGallery();
                }else {
                    Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putExtra("pos", path);
                    startActivity(intent);
                }
            }
        });
        recyclerView.setAdapter(adaptor);

        button.setOnClickListener(this);

        if((ActivityCompat.checkSelfPermission(
                this,colum[0])!= PackageManager.PERMISSION_GRANTED)&&
                (ActivityCompat.checkSelfPermission(
                        this,colum[1])!= PackageManager.PERMISSION_GRANTED)){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestPermissions(colum,123);
            }
        }
    }

    @Override
    public void onClick(View view) {
        openGallery();
    }
    private void openGallery() {
        Intent intent=new Intent();
        intent.setType("image/*");
        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,true);
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent,"Selcet Picture"),PICK);
    }
  @Override
    protected void onActivityResult(int requestCode, int resultCode,  Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==PICK && resultCode==RESULT_OK){
            if(data.getClipData()!=null){
                int x=data.getClipData().getItemCount();
                for(int i=0;i<x;i++){
                    list.add(list.size()-1,data.getClipData().getItemAt(i).getUri().toString());
                }

                adaptor.notifyDataSetChanged();
                textView.setText("Image("+list.size()+")");
            }else if(data.getData()!=null){
                String imgurl=data.getData().toString();
                list.add(list.size()-1,(imgurl));

                adaptor.notifyDataSetChanged();
                textView.setText("Image("+list.size()+")");
            }

In Adapter Class try this


    public Adapter(ArrayList list, Context context,AdapterCallback callback) {
        this.list = list;
        this.context = context;
        this.interface1 = callback;
    }

    View view;

    @Override
    public Adapter.RViewHolder onCreateViewHolder(ViewGroup v, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(v.getContext());
        view = inflater.from(v.getContext()).inflate(R.layout.data, v, false);
        RViewHolder viewHolder = new RViewHolder(view);
        return viewHolder;

    }

    @Override
    public void onBindViewHolder(Adapter.RViewHolder holder, int position) {

        path = list.get(position).toString();

        if(position==list.size()-1){
            holder.imageView.setImageResource(Integer.parseInt(path));
        }else holder.imageView.setImageURI(Uri.parse(path));

        int a=position+1;

        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              String  path1 = list.get(holder.getAdapterPosition()).toString();
                interface1.onMethodCallback(path1,holder.getAdapterPosition());
                Toast.makeText(context, "Clicked", Toast.LENGTH_SHORT).show();
            }
        });

In Interface

public interface AdapterCallback {
    void onMethodCallback(String data,int position);
}

In Second Activity

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        imgfrag = findViewById(R.id.imgfrag);
        getincoming();
    }

    private void getincoming() {
        if (getIntent().hasExtra("pos")) {
            String imageUrl = getIntent().getStringExtra("pos");
            Glide.with(this).asBitmap().load(imageUrl).into(imgfrag);
        }
    }