Java – use the recycler view in Android to use two sets of multiple images for data binding at the same time

Our problem is to update multiple images in the recycler view by clicking any single item in the recycler view using data binding, but initially we can use the XML of the imageadapter The data binding in but only updates the image in some places. According to our search in the Android Developer site, we are recommended to use the recycler view instead of our initial method before clicking any image

after clicking any image

The above method uses the data binding in the image adapter, as shown below

Image adapter

package tene.com.darxstudios;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;


public class ImageAdapter extends BaseAdapter {
    private static final String TAG = ImageAdapter.class.getName() ;
    Context mContext;
    List<Integer> cropsList1 = new ArrayList<Integer>();
    List<Integer> cropsList2 = new ArrayList<Integer>();
    User user0 = new User(),user1 = new User(),user2 = new     User(),user3 = new User(),user4 = new User();
    public ImageAdapter(Context mContext,List<Integer>     cropsList1,List<Integer> cropsList2){
        this.mContext = mContext;
        this.cropsList1 = cropsList1;
        this.cropsList2 = cropsList2;

    }
    @Override
    public int getCount() {
        return cropsList1.size();
    }

    @Override
    public Object getItem(int position) {
        return cropsList1.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position,View convertView,ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

//        View view = inflater.inflate(R.layout.item_view,null);

        final User user = new User();
        user.setFirstName(cropsList1.get(position));

        tene.com.darxstudios.ItemViewBinding binding =      DataBindingUtil.inflate(inflater,R.layout.item_view,parent,false);
        binding.setUser(user);

        View view = binding.getRoot();

        ImageView imageView = (ImageView)     view.findViewById(R.id.image);


//        if(position == 0){
//            user0 = user;
//        }else if(position == 1){
//            user1 = user;
//        }else if(position == 2){
//            user2 = user;
//        }else if(position == 3){
//            user3 = user;
//        }else if(position == 4){
//            user4 = user;
//        }

         imageView.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
              user.setFirstName(cropsList2.get(position));
//                user0.setFirstName(cropsList2.get(0));
//                user1.setFirstName(cropsList2.get(1));
//                user2.setFirstName(cropsList2.get(2));
//                user3.setFirstName(cropsList2.get(3));
//                user4.setFirstName(cropsList2.get(4));
            }
        });
        return view;
    }
}

item_ view. xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:bind="http://schemas.android.com/apk/res-auto"
     xmlns:android="http://schemas.android.com/apk/res/android">
    <data class=".ItemViewBinding">
        <variable  name="user" type="tene.com.darxstudios.User"/>
    </data>
<LinearLayout
    android:orientation="horizontal"     android:layout_width="wrap_content"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:id="@+id/image"
        firstName='@{user.firstName}'/>

</LinearLayout>
</layout>

We need to use the circulator view with data binding to update all images when we click any image at a time

Solution

Place the necessary logic in the onclicklistener in the onbindviewholder to change the image See the recyclerview tutorial to learn how to implement it The following code only handles onclicklistener

public class NumReelAdapter extends RecyclerView.Adapter<NumReelAdapter.recyclerViewHolder> {

List<NumReel> data = Collections.emptyList();
Context ctx;
int selectedPosition = 0;

onBindViewHolder

public void onBindViewHolder(final recyclerViewHolder holder,final int position) {
    final NumReel current = data.get(position);
    if(selectedPosition == position){
        // here highlighting the background. And set the inside color to green
        holder.itemView.setBackground(whatever_image_id);
    } else {
        //reset the color to white
        holder.itemView.setBackground(whatever_it_had_before);
    }
    holder.vItemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //refresh the adapter
            notifyItemChanged(selectedPosition);
            //change clicked position
            selectedPosition = position;
            //reset the adapter with new position highlighted
            notifyItemChanged(selectedPosition);
            if (numClickListener != null) {
                String vTag = v.getTag().toString();
                //numClickListener.repsClick(v,position); alternate method where data can't be sent
                numClickListener.numClick(vTag,current.num);
            }
        }
    });

Here, you can check any specific properties of the clicked image and make changes accordingly Therefore, in your case, you must check the name of the image Therefore, if you click chickpeas, the background becomes a rice field

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>