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
Asked
Active
Viewed 1,878 times
0
-
This question is too broad. – Kaushik Burkule Sep 19 '19 at 12:08
-
Possible duplicate of [How to create RecyclerView with multiple view type?](https://stackoverflow.com/questions/26245139/how-to-create-recyclerview-with-multiple-view-type) – denvercoder9 Sep 19 '19 at 12:38
4 Answers
0
This can be done in this way
- Create ArrayList with a custom object that contains the image URL
- Add only one object on the ArrayList
- Set the adapter to show from ArrayList
- In the recycler view's bind view holder, if url not exist then show addimageLogo
- Then on the click on the image add an image(Path/URL) on the object at the position
- Once an image is added on the object, then check if the last object has the URL then again point 2
- 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
- Add a viewType variable to your data class. In your case you could use 'Button' and 'Image'.
- Set the first object's viewType value as 'Button'
- 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

Aravinth Velusamy
- 140
- 2
- 8
-
Thank you very much it very useful but i am use java can you write code in java – Malek Alzein Sep 19 '19 at 14:24
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);
}
}

Amirthalakshmi
- 1
- 1