13

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>
Community
  • 1
  • 1
Timmiej93
  • 1,328
  • 1
  • 16
  • 35
  • 1
    You can get the object of view by position and make changes to the view accordingly.. check this link to know how to get the Item's view by position - http://stackoverflow.com/questions/24811536/android-listview-get-item-view-by-position – sanedroid Apr 28 '16 at 12:48

2 Answers2

11

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().

Timmiej93
  • 1,328
  • 1
  • 16
  • 35
  • 4
    Also this is helpful: mRecyclerView.findViewHolderForAdapterPosition(position); – DJphy Oct 27 '16 at 10:08
  • 7
    Wouldn't a better way to do this be to override the `onAttachedToRecyclerView(RecyclerView recyclerView)` method and inside that method set `mRecyclerView = recyclerView` ? – Vishnu M. Nov 27 '16 at 00:55
0

it would have been easy if you would have posted your adapter code too,

well when you click on some item it gives you its position and you can handle other items too by mItems.getAdapterPosition() +1 or -1

here is simple example

In your onClickListerner in ViewHolder all you need to do is

@Override
    public void onClick(View v) {

        // Do something with item at mItems.getAdapterPosition() -1

    }
Atiq
  • 14,435
  • 6
  • 54
  • 69
  • I'm not quite sure what you're calling `getAdaterPostin()` on.. I've tried calling it on pretty much everything in my code, but it doesn't work. – Timmiej93 Apr 28 '16 at 13:07
  • you call it inside your `recyclerview` and it does work, I have always used it, post your adapter code then I can help you – Atiq Apr 28 '16 at 13:11
  • @Timmiej93 Does your `onClick works? I mean have u tested it by showing simple toast? – Atiq Apr 28 '16 at 13:33
  • Yes, everything works just fine. I got an `onLongClick` and `onTouch` listener on the view as well in my code, all working fine. – Timmiej93 Apr 28 '16 at 13:34
  • then try displaying position of item by `stuffList.getAdapterPosition()` in toast, it should give u the current position – Atiq Apr 28 '16 at 13:36
  • It's not the position that I'm having issues with, I'm having trouble with getting an actual reference to the little blue divider bar that you can see, so I can set a visibility parameter on it. – Timmiej93 Apr 28 '16 at 13:38
  • bro, if you get the actual position of the view then we can handle the views with respect to that position by `-1`, `+1`, feel me? – Atiq Apr 28 '16 at 13:42
  • I **can** get the position of the view that I'm clicking (`onBindViewHolder` has a position variable), and I understand that `that view - 1` equals the view above the current one, but how do I get a reference to the `View` with id `SMVFDividerBar` then? – Timmiej93 Apr 28 '16 at 13:43