Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a reference to a ViewHolder, from inside another ViewHolder? (RecyclerView)

I have a RecyclerView with some view's/card's (let's call it a view for now), which all contain the same things, including a View that I'm using as a divider bar.

I want to be able to change the properties of this divider bar in the view above the current one. So say I click on the card that says test3, I want to be able to set the properties of the divider bar that's in the test2 view.

To clarify:

I only need a reference to the ViewHolder for the view above (or any other for that matter) the one I click, that's all. How should I do this?

enter image description here

public class StuffManagerAdapter extends RecyclerView.Adapter<StuffManagerAdapter.MyViewHolder> {

    private List<StuffManagerClass> stuffList;
    private Context context;
    private String TAG = "CStA";
    DBHandler dbHandler;
    ArrayList<String> tagArray = new ArrayList<>();
    RecyclerView mRecyclerView;

    public StuffManagerAdapter(List<StuffManagerClass> stuffList, Context context) {
        this.stuffList = stuffList;
        this.context = context;
        dbHandler = DBHandler.getInstance(context);
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView DeviceTV, NameTV, TagTV;
        private ImageButton editButton, deleteButton;
        public ImageView tagWarningImage;
        View view, bottomBar ,cover;

        public MyViewHolder(final View view) {
            super(view);
            this.view = view;
            DeviceTV = (TextView) view.findViewById(R.id.SMVFDeviceTextView);
            // etc., etc.

            editButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // do stuff
                }
            });
            deleteButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // do stuff
                }
            });
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_stuffmanagervariablefragment, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        final StuffManagerClass sc = stuffList.get(position);

        // Initialize card, set all textviews
        holder.DeviceTV.setText(sc.getDevice());
        holder.NameTV.setText(sc.getName());
        holder.TagTV.setText(sc.getTag());
        sc.setPosition(position);

        
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {    
                // This is where I would like to get a reference to the divider bar on the other view/card
            }
        });
    }

    @Override
    public int getItemCount() {
        return stuffList.size();
    }
}

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!-- TextViews and stuff -->

        <View
            android:id="@+id/SMVFDividerBar"
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_below="@+id/LL2"
            android:layout_centerHorizontal="true"
            android:layout_marginLeft="7dp"
            android:layout_marginRight="7dp"
            android:background="@color/colorPrimaryDark">

    </RelativeLayout>

</FrameLayout>
like image 768
Timmiej93 Avatar asked Apr 28 '16 12:04

Timmiej93


1 Answers

Don't know why I didn't realize this sooner, but it works.

First, create an instance variable (please correct me if this is not an instance variable).

public class StuffManagerAdapter extends 
                    RecyclerView.Adapter<StuffManagerAdapter.MyViewHolder> {

    RecyclerView mRecyclerView;

Then, assign this variable in the onCreateViewHolder()method like this:

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.thingy, parent, false);
    mRecyclerView = (RecyclerView) parent;
    return new MyViewHolder(itemView);
}

Then, in your onBindViewHolder() method, you can access the other ViewHolder's like this:

mRecyclerView.getChildAt(holder.getAdapterPosition-1);

You can set this to a variable, or call stuff like findViewById() on it directly.

Obviously, you can manually input any position you want in the getChildAt() statement, and you can find any object in the view by using findViewById().

like image 195
Timmiej93 Avatar answered Nov 10 '22 01:11

Timmiej93